开发者

Do you need to close the connection you get from jdbc connection pool? [duplicate]

开发者 https://www.devze.com 2023-02-26 20:06 出处:网络
This question already has answers here: 开发者_StackOverflow中文版 Closing JDBC Connections in Pool
This question already has answers here: 开发者_StackOverflow中文版 Closing JDBC Connections in Pool (3 answers) Closed 2 years ago.

Suppose I have the following code

DataSource source = (DataSource) (new InitialContext()).lookup("jdbc/myName"); 
Connection connnection = source.getConnection() 

//use the connection to do some database operations...

at the end, should I still call

connection.close() to release the resource?

If the connection is from a connection pool, if I don't do anything, the connection should be automatically returned to the pool, right?

On the other hand, if I close it, is there gonna be any adverse effect on the connection pool (ie, after several calls, there won't be any connection left in the pool?)


The answer is yes, and check Closing JDBC Connections in Pool and JDBC Connection Pooling Best Practices for more information.


Yes, you should be closing the connection, if only for quick recovery by the pool.

This article http://www.javaranch.com/journal/200601/JDBCConnectionPooling.html advises placement of a .close() call in a finally block.

0

精彩评论

暂无评论...
验证码 换一张
取 消