保持数据库连接打开或在需要时重新打开它?

时间:2021-11-26 12:50:20

I know this sort of question has been asked for a few times, I read some of them but did not get any smarter. My Java application is connecting to a database server via JDBC through a SSH Tunnel. The tunnel is opened once at beginning. When starting I opened the database connection everytime it is used. Due to changes in the app I needed the connection opened on startup and decided to keep it open until my application is closed. When I close the app I sometimes, not always, get following error:

我知道这类问题已经被问过好几次了,我读过其中的一些,但并没有变得更聪明。我的Java应用程序通过SSH隧道连接到数据库JDBC服务器。隧道一开始就开了。当开始时,每次使用数据库连接时,我都会打开它。由于应用程序的变化,我需要在启动时打开连接,并决定保持打开状态,直到我的应用程序关闭为止。当我关闭应用程序时,我有时(并非总是)会得到以下错误:

 - Could not retrieve transation read-only status server

 java.sql.SQLException: Could not retrieve transation read-only status server
 Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

 The last packet successfully received from the server was 369.901 milliseconds ago.  The last packet sent successfully to the server was 8 milliseconds ago.
 Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.

Could this be because of the always open database connection? I test the application only for short times. It will run on 4 computers all day long. Can I expect this error more often then? The connection is used every few minutes, so it should be more performant to keep it open, but maybe during break it is not used for half an hour.

这可能是因为始终打开的数据库连接吗?我只测试应用程序的短时间。它将在4台电脑上运行一整天。那么我能更经常地预料到这个错误吗?这种连接每隔几分钟就会被使用一次,所以保持它的打开应该更有效果,但是也许在休息的时候它不会被使用半个小时。

What would you recommend me to do? Always reopen the connection or keep it like it is and find a workaround when I get this error? Do you maybe have another idea why this error appears?

你建议我做什么?总是重新打开连接,或者像它一样保留它,当我得到这个错误时找到一个变通方法?你是否知道为什么会出现这个错误?

Just ask if you need more error log, database code or whatever you need.

只需询问是否需要更多的错误日志、数据库代码或其他您需要的。

Thanks!

谢谢!

1 个解决方案

#1


2  

I'm going to say yes, your issue is probably due to the fact that you have a persistent connection open.

我想说,是的,你的问题可能是由于你有一个持久的连接是开放的。

I took over a website a while ago and the guy before me had the same idea: Open a single connection, send the queries through it when needed, and never close it. A month after I took over the site, the database wouldn't return any more query results just like this.

不久前我接手了一个网站,在我之前的那个人也有同样的想法:打开一个连接,在需要的时候通过它发送查询,永远不要关闭它。在我接手这个网站一个月后,数据库就不会再像这样返回任何查询结果。

As a general rule of thumb and for good programming practice, always clean up after yourself. If you're not using a variable, set it to null and delete it. Not using a connection? Terminate it. This is way less prominent in Java than it is in C++ as Java does all the cleaning up for you for the most part.

作为一般的经验法则和良好的编程实践,经常清理你自己。如果不使用变量,将其设置为null并删除。不使用一个连接吗?终止它。这一点在Java中不如在c++中那么突出,因为Java在大多数情况下为您做了所有的清理工作。

#1


2  

I'm going to say yes, your issue is probably due to the fact that you have a persistent connection open.

我想说,是的,你的问题可能是由于你有一个持久的连接是开放的。

I took over a website a while ago and the guy before me had the same idea: Open a single connection, send the queries through it when needed, and never close it. A month after I took over the site, the database wouldn't return any more query results just like this.

不久前我接手了一个网站,在我之前的那个人也有同样的想法:打开一个连接,在需要的时候通过它发送查询,永远不要关闭它。在我接手这个网站一个月后,数据库就不会再像这样返回任何查询结果。

As a general rule of thumb and for good programming practice, always clean up after yourself. If you're not using a variable, set it to null and delete it. Not using a connection? Terminate it. This is way less prominent in Java than it is in C++ as Java does all the cleaning up for you for the most part.

作为一般的经验法则和良好的编程实践,经常清理你自己。如果不使用变量,将其设置为null并删除。不使用一个连接吗?终止它。这一点在Java中不如在c++中那么突出,因为Java在大多数情况下为您做了所有的清理工作。