Does Spring's JdbcTemplate close the connection after query timeout?

我的未来我决定 提交于 2019-12-30 03:55:29

问题


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

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