简体   繁体   English

是否应该使用JDBC关闭每种方法的连接?

[英]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. 我有一个Java Web服务器,其中包含许多通过JDBC查询每个mysql数据库的方法。 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. 但是,由于整个应用程序都使用与数据库相同的连接,所以我永远不会关闭Connection。

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. PS:我发现关于JDBC的所有已问问题都没有解决此问题。

MadProgrammer and Steve Atkinson 's comments under the main questions helped me. MadProgrammer和Steve Atkinson在主要问题下的评论对我有所帮助。 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. 实际上,与必须为每个查询打开和关闭连接或为整个应用程序使用完全相同的连接相比,拥有连接池是一种更好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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