I have set query timeout (getJdbcTemplate().setQueryTimeout(5)) in method with insert statement. What will happen after query timeout, does jdbc template close my connection?
-
Probably yes. JdbcTemplate takes care for getting and closing the connections. – Evgeni Dimitrov Dec 06 '13 at 09:02
4 Answers
In short yes it does close the connection. The long answer it depends.
When you don't have a Spring managed transaction then yes the JdbcTemplate
will call the close()
method on the Connection
. However if there was already a connection available due to Springs transaction management closing the connection will be handled by Springs transaction support, which in turn also will call close()
on the Connection
.
The only difference is when the connection is closed but close()
will be called.
If the connection will be actually closed depends on which DataSource
is used, in general when using a connection pool the connection will be returned to the pool instead of actually closing the connection.

- 115,695
- 22
- 220
- 224
-
is it the `close()` method that's called always called? Because in my program I need Spring to not close the connection so if it's guranteed to be `close()` I can override that method. – Sach Dec 31 '14 at 09:18
-
As stated it depends. If you have a specific question please ask a question instead of doing if by comments. – M. Deinum Dec 31 '14 at 09:32
Yes it does.
And if the connection was obtained from connection pool, it won't actually close the connection, rather will send it back to the pool.

- 1,940
- 14
- 27
No need to close the connection manually. Spring container itself to take care of the operation. Kindly refer to this spring documentation:
https://docs.spring.io/spring-framework/docs/current/reference/html/data-access.html#jdbc

- 14,222
- 20
- 104
- 125

- 51
- 1
- 1
By reviewing the source code of JdbcTemplate, there is a method called execute
, which is the infrastructure of some other query method, such as queryForObject
, queryForList
and so on :
@Nullable
private <T> T execute(StatementCallback<T> action, boolean closeResources) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Connection con = DataSourceUtils.getConnection(obtainDataSource());
Statement stmt = null;
try {
stmt = con.createStatement();
applyStatementSettings(stmt);
T result = action.doInStatement(stmt);
handleWarnings(stmt);
return result;
}
catch (SQLException ex) {
// Release Connection early, to avoid potential connection pool deadlock
// in the case when the exception translator hasn't been initialized yet.
String sql = getSql(action);
JdbcUtils.closeStatement(stmt);
stmt = null;
DataSourceUtils.releaseConnection(con, getDataSource());
con = null;
throw translateException("StatementCallback", sql, ex);
}
finally {
if (closeResources) {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
}
Apparently, when execute get called, the database connection would be release in the finally block , as a result, it`s unnecessary to close the connection after query timeout.

- 1,652
- 18
- 27