问题
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?
回答1:
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.
回答2:
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.
回答3:
No need to close the connection manually. Spring container itself to take of the operation. Kindly refer this spring url,
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html
回答4:
We can also close connection while using jdbcTemplate
, in some cases it is compulsory to close connection after executing a query otherwise you'll get connection issue. For more details visit Close connection in jdbc template
jdbcTemplate.getDataSource().getConnection().close();
来源:https://stackoverflow.com/questions/20419785/does-springs-jdbctemplate-close-the-connection-after-query-timeout