简体   繁体   English

如何正确管理MySQL连接?

[英]How to manage MySQL Connections properly?

I am working on a server-application written in java. 我正在用Java编写的服务器应用程序。 There are up to 30 client requests per second, for every request a specific mysql table entry gets updated. 每秒最多有30个客户端请求,对于每个请求,特定的mysql表条目都会更新。

All the server threads use a single mySQL Connection, they obtain it from a singleton class. 所有服务器线程都使用单个mySQL Connection,它们从单例类中获取它。 Then the server thread creates a Statement and executes a update query. 然后,服务器线程创建一个Statement并执行更新查询。

Although I close all the created Statements after execution, the server stops updating the mySQL table after some hours. 尽管执行后我关闭了所有创建的Statements,但是服务器在几个小时后停止更新mySQL表。

What could be wrong? 有什么事吗 Is this setup a misconcept? 这是一个误解吗?

Perhaps, your connection was closed due to timeout. 也许是由于超时而关闭了您的连接。 You can make use of Connection#isValid in order to verify whether or not your connection is still open. 您可以使用Connection#isValid来验证您的连接是否仍处于打开状态。

You may take a look at Connection timeout for DriverManager getConnection . 您可以看看DriverManager getConnection的连接超时 There are several good tips listed. 列出了几个不错的技巧。

Also, You may consider the use of Connection Pool in your code. 另外,您可以考虑在代码中使用连接池 Apache commons is a good place to start. Apache Commons是一个很好的起点。

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

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