简体   繁体   中英

closing and opening database connections?

In java application I need to check if data has been updated in my application and update the changes in other database. I have connection open to both database all the time till application terminates. Is it better to open the database connection at the interval of time or keep the connection open always?

any typical database application will use a pool of connections. Pooled connections stay open so that the application doesn't incur the cost of establishing a new connection every time it needs one. With pools even if you explicitly call close() on the connection from your code, it won't close. Only the pool manager can close such a connection.

It depends on how often do you use the connections and what is your execution environment. If you are using the connection after long interval then probably it is a good idea to close and reopen whenever you need. Again if you are using it is J2EE environment then as Dmitry said, you should be using the connection from the pool. When you close the pooled connection, it is not physically closed but returned to the pool for somebody to use it again. In a standalone J2SE environment it makes sense to close and recreate the connections whenever needed specially if you are not often using it. You can pool the connections in a J2SE environment too, if the connection is used often.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM