Does Spring's JdbcTemplate close the connection if an exception is thrown?

时光总嘲笑我的痴心妄想 提交于 2019-12-30 06:08:42

问题


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
}

回答1:


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.




回答2:


I think your developer should take a look at springs transaction managemant capabilities. You can use AOP to advice logging, rollback behavior and even retry or other exception handling actions to react completly declarative.



来源:https://stackoverflow.com/questions/5096899/does-springs-jdbctemplate-close-the-connection-if-an-exception-is-thrown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!