Oracle DB:java.sql.SQLException:已关闭连接

时间:2023-01-01 22:48:55

Reasons for java.sql.SQLException: Closed Connection from Oracle??

java.sql.SQLException的原因:Oracle的封闭连接?

java.sql.SQLException: Closed Connection at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208) at oracle.jdbc.driver.PhysicalConnection.commit(PhysicalConnection.java:1131) at oracle.jdbc.OracleConnectionWrapper.commit(OracleConnectionWrapper.java:117)

java.sql.SQLException:oracle.jdbc.driver.DatabaseError上的oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)处于oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)的已关闭连接.throwSqlException(DatabaseError.java:208)位于oracle.jdbc.OracleConnectionWrapper.com的oracle.jdbc.driver.PhysicalConnection.commit(PhysicalConnection.java:1131)(OracleConnectionWrapper.java:117)

We are getting this error from the fail over database connection. We use the same code for other databases as well. But seeing this issue with only one of the databases. Is this because the connection might have timeout due to long inactivity period and we are trying to use that? Pls let me know if you need more details...

我们从故障转移数据库连接中收到此错误。我们也为其他数据库使用相同的代码。但只与其中一个数据库一起看这个问题。这是因为由于长时间不活动而连接可能会超时,我们正在尝试使用它吗?请告诉我您是否需要更多详细信息......

AbandonedConnectionTimeout set to 15 mins InactivityTimeout set to 30 mins

AbandonedConnectionTimeout设置为15分钟InactivityTimeout设置为30分钟

2 个解决方案

#1


35  

It means the connection was successfully established at some point, but when you tried to commit right there, the connection was no longer open. The parameters you mentioned sound like connection pool settings. If so, they're unrelated to this problem. The most likely cause is a firewall between you and the database that is killing connections after a certain amount of idle time. The most common fix is to make your connection pool run a validation query when a connection is checked out from it. This will immediately identify and evict dead connnections, ensuring that you only get good connections out of the pool.

这意味着在某些时候成功建立了连接,但是当您尝试在那里提交时,连接不再打开。您提到的参数听起来像连接池设置。如果是这样,他们就与这个问题无关。最可能的原因是您和数据库之间的防火墙在一定量的空闲时间后终止连接。最常见的修复方法是在从中检出连接时使连接池运行验证查询。这将立即识别并驱逐死亡连接,确保您只能从池中获得良好的连接。

#2


8  

You have to validate the connection.

您必须验证连接。

If you use Oracle it is likely that you use Oracle´s Universal Connection Pool. The following assumes that you do so.

如果您使用Oracle,则很可能使用Oracle的通用连接池。以下假定您这样做。

The easiest way to validate the connection is to tell Oracle that the connection must be validated while borrowing it. This can be done with

验证连接的最简单方法是告诉Oracle必须在借用连接时验证连接。这可以用来完成

pool.setValidateConnectionOnBorrow(true);

But it works only if you hold the connection for a short period. If you borrow the connection for a longer time, it is likely that the connection gets broken while you hold it. In that case you have to validate the connection explicitly with

但只有在短时间内保持连接时才有效。如果您长时间借用连接,则在保持连接时可能会断开连接。在这种情况下,您必须明确验证连接

if (connection == null || !((ValidConnection) connection).isValid())

See the Oracle documentation for further details.

有关更多详细信息,请参阅Oracle文档。

#1


35  

It means the connection was successfully established at some point, but when you tried to commit right there, the connection was no longer open. The parameters you mentioned sound like connection pool settings. If so, they're unrelated to this problem. The most likely cause is a firewall between you and the database that is killing connections after a certain amount of idle time. The most common fix is to make your connection pool run a validation query when a connection is checked out from it. This will immediately identify and evict dead connnections, ensuring that you only get good connections out of the pool.

这意味着在某些时候成功建立了连接,但是当您尝试在那里提交时,连接不再打开。您提到的参数听起来像连接池设置。如果是这样,他们就与这个问题无关。最可能的原因是您和数据库之间的防火墙在一定量的空闲时间后终止连接。最常见的修复方法是在从中检出连接时使连接池运行验证查询。这将立即识别并驱逐死亡连接,确保您只能从池中获得良好的连接。

#2


8  

You have to validate the connection.

您必须验证连接。

If you use Oracle it is likely that you use Oracle´s Universal Connection Pool. The following assumes that you do so.

如果您使用Oracle,则很可能使用Oracle的通用连接池。以下假定您这样做。

The easiest way to validate the connection is to tell Oracle that the connection must be validated while borrowing it. This can be done with

验证连接的最简单方法是告诉Oracle必须在借用连接时验证连接。这可以用来完成

pool.setValidateConnectionOnBorrow(true);

But it works only if you hold the connection for a short period. If you borrow the connection for a longer time, it is likely that the connection gets broken while you hold it. In that case you have to validate the connection explicitly with

但只有在短时间内保持连接时才有效。如果您长时间借用连接,则在保持连接时可能会断开连接。在这种情况下,您必须明确验证连接

if (connection == null || !((ValidConnection) connection).isValid())

See the Oracle documentation for further details.

有关更多详细信息,请参阅Oracle文档。