简体   繁体   English

我应该关闭数据库连接吗?

[英]Should I close my database connections?

I was wondering: Should I close my database connection or leave it open in the following scenario: 我想知道:在以下情况下,我应该关闭数据库连接还是将其保持打开状态:

My application executes and after every 1-2 seconds it updates a table from the database. 我的应用程序执行后,每1-2秒就会从​​数据库中更新一个表。 This happens until the application is terminated. 直到应用程序终止为止。

Basically what would be more optimal and put less stress on the server, every time this is executed about 500 rows need to be updated with at least 11 fields each (at least 5500 fields combined). 基本上,这是最佳选择,并且对服务器的压力较小,每次执行此操作时,大约需要更新500行,每个行至少包含11个字段(合计至少5500个字段)。

I'm currently using the JDBC driver if it matters at all. 如果有什么关系,我目前正在使用JDBC驱动程序。

EDIT: Also, would it be more efficient to update certain rows or erase the entire table contents and re-write the updated data (Some rows may be completely different in the updated data). 编辑:此外,更新某些行或擦除整个表的内容并重新写入更新的数据(某些行的更新数据可能完全不同)会更有效吗?

You should use a connection pool for this. 您应该为此使用连接池。 Check this answer about connection pooling outside an application server. 检查有关应用程序服务器外部连接池的此答案

You have to consider dropped connections here as well as stress on the server. 您必须考虑此处断开的连接以及服务器的压力。 You would be better using a connection pool to manage your connections then you don't have this worry. 您最好使用连接池来管理连接,这样您就不必担心了。

Try out HikariCP for connection pooling. 试用HikariCP进行连接池。 Disclaimer: I am one of the authors. 免责声明:我是作者之一。

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

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