When Spring catches an SQLException, does it close the prepared statement, result set, and/or connection before throwing it's own DataAccessException (runtime) exception?
I have a developer who wants to create an AOP aspect to catch these exceptions and log and/or close the connection.
@AfterThrowing(pointcut="dataAccessOperation()", throwing="exception")
public void doRecoveryActions(JoinPoint thisJoinPoint, DataAccessException exception) {
// log and/or close connection
}
In short yes it does close the connection.
you need not handle any database-related exceptions explicitly instead spring jdbc framework will handle it for you. all the exceptions thrown by the spring jdbc framework are subclasses of dataaccessexception which is a type of runtimeexception, so you need not handle it explicitly.
Spring Example JDBC Database Connection Pool JdbcTemplate requires a DataSource which is javax. sql. DataSource implementation and you can get this directly using spring bean configuration or by using JNDI if you are using the J2EE web server or application server for managing Connection Pool.
If you look inside the source, you will see every execute or query closes it resultSet and connection so you do not need to manually close them.
Yes.
That's the whole point of JdbcTemplate
- it handles all kinds of boilerplate actions including release of all resources. See 12. Data access with JDBC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With