I'm writing some methods to deal with database operations. Each method first gets a connection, do the operations, and close the connection at end.
I wonder if Spring AOP can help handling the connection acquiring and closing. Specifically I want something like:
@Aspect
@Component
public class ConnAspect {
@Around("@annotation(connHandle)")
public void handleConnection(ProceedingJoinPoint pjp, ConnHandle connHandle) throws Throwable {
Connection conn = datasource.getConnection();
pjp.proceed(); // can pjp get variable conn?
conn.close();
}
}
@Component
public class DbOperation {
@ConnHandle
public void operation1(...) {
... // do some operation with conn
}
...
}
Is it possible to do so? Or should I turn to other solutions? Thanks for any hints and answers.