如果抛出异常,Spring的JdbcTemplate会关闭连接吗? [英] Does Spring's JdbcTemplate close the connection if an exception is thrown?

查看:729
本文介绍了如果抛出异常,Spring的JdbcTemplate会关闭连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当Spring捕获一个SQLException时,是否会在抛出自己的DataAccessException(运行时)异常之前关闭预准备语句,结果集和/或连接?

When Spring catches an SQLException, does it close the prepared statement, result set, and/or connection before throwing it's own DataAccessException (runtime) exception?

我有想要创建AOP方面以捕获这些异常并记录和/或关闭连接的开发人员。

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
}


推荐答案

是。

这是 JdbcTemplate 的重点 - 它处理各种样板操作,包括释放所有资源。请参见 12。使用JDBC进行数据访问

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.

这篇关于如果抛出异常,Spring的JdbcTemplate会关闭连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆