简体   繁体   中英

Should I close connection in each method with JDBC?

I have a java web server with a lot of methods querying each my mysql database through JDBC. At the end of each method called, the Statement and the Resultset are automatically closed. Nevertheless, I am never closing the Connection since the whole application is using the very same connection to the database.

Here is thus my questions :

  • Am I doing good regarding the connection or should I create and close a connection in each method (meaning almost after each query) ?

  • In case I create a connection for each method, won't this be really expensive for my server low ressources ?

PS : None of the already asked questions on JDBC that I found was tackling this issue.

MadProgrammer and Steve Atkinson 's comments under the main questions helped me. Actually having a pool of connections is a better practice than having to open and close a connection for each queries or using the very same connection for the whole application.

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