Java Spring 的 JdbcTemplate 是否在查询超时后关闭连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20419785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Spring's JdbcTemplate close the connection after query timeout?
提问by user3073662
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?
我在带有插入语句的方法中设置了查询超时(getJdbcTemplate().setQueryTimeout(5))。查询超时后会发生什么,jdbc 模板会关闭我的连接吗?
采纳答案by M. Deinum
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
.
当你没有一个Spring管理的事务则是在JdbcTemplate
将调用close()
的方法Connection
。但是,如果已经有一个连接可用由于泉事务管理关闭连接将通过弹簧事务支持,这反过来也将调用处理close()
上Connection
。
The only difference is when the connection is closed but close()
will be called.
唯一的区别是连接关闭但close()
会被调用。
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.
如果连接将实际关闭取决于使用的DataSource
是哪个,通常在使用连接池时,连接将返回到池中,而不是实际关闭连接。
回答by Abhishek Anand
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.
如果连接是从连接池中获得的,它实际上不会关闭连接,而是将其发送回连接池。
回答by Selvakumar Sundaramoorthy
No need to close the connection manually. Spring container itself to take of the operation. Kindly refer this spring url,
无需手动关闭连接。Spring 容器本身采取的操作。请参考这个春天的网址,
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html
http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/jdbc.html
回答by Sanjeev Lahariya
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
,在某些情况下,执行查询后必须关闭连接,否则会出现连接问题。有关更多详细信息,请访问jdbc 模板中的关闭连接
jdbcTemplate.getDataSource().getConnection().close();