aakoch
aakoch

Reputation: 1174

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

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
}

Upvotes: 12

Views: 27575

Answers (2)

zoidbeck
zoidbeck

Reputation: 4151

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.

Upvotes: 0

axtavt
axtavt

Reputation: 242786

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.

Upvotes: 21

Related Questions