gpt4 book ai didi

java - Hibernate 更改某行代码得到明显的死锁

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:02 26 4
gpt4 key购买 nike

我使用 Oracle 数据库,我的 java 应用程序使用 Hibernate 3.3.2 与之交互。

我总是使用这样的代码连接到数据库

public void method() {
Session session = HibernateSessionFactory.getSessionFactory().openSession();
String sqlSelect = "SELECT sql ...";
Transaction tran = session.beginTransaction();
SQLQuery sqlSelectQuery = session.createSQLQuery(sqlSelect).;
sqlSelectQuery.setString("PARAM", param);
boolean ok = true;
try {
List list = sqlSelectQuery.list();
if (list.size() > 0) {
String updateSql = "UPDATE ....";
SQLQuery updateQuery = session.createSQLQuery(updateSql);
updateQuery.setString("PARAM", param);
updateQuery.executeUpdate();
}
} catch (Exception ex) {
ok = false;
tran.rollback();
ex.printStackTrace()
}
try {
if (ok) {
tran.commit();
}
session.close();
} catch (Exception ex) {
ex.printStackTrace()
}
}

它工作完美,永远不会出错。

但是当我更改为:(仅添加 try、catch,最后完全关闭 hibernate session )

public void method() {
Session session = null
try{
session = HibernateSessionFactory.getSessionFactory().openSession();
String sqlSelect = "SELECT sql ...";
Transaction tran = session.beginTransaction();
SQLQuery sqlSelectQuery = session.createSQLQuery(sqlSelect).;
sqlSelectQuery.setString("PARAM", param);
boolean ok = true;
try {
List list = sqlSelectQuery.list();
if (list.size() > 0) {
String updateSql = "UPDATE ....";
SQLQuery updateQuery = session.createSQLQuery(updateSql);
updateQuery.setString("PARAM", param);
updateQuery.executeUpdate();
}
} catch (Exception ex) {
ok = false;
tran.rollback();
ex.printStackTrace()
}
try {
if (ok) {
tran.commit();
}
session.close();
} catch (Exception ex) {
ex.printStackTrace()
}
}catch (HibernateException hibernateException) {
hibernateException.printStackTrace();

} finally {
if ((session != null) && (session.isOpen())) {
session.close();
}

}
}

然后我得到了很多明显的死锁。

[Timer-2] 24/06/2014 21:33:11  WARN ThreadPoolAsynchronousRunner: Task com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@1e093f32 (in deadlocked PoolThread) failed to complete in maximum time 60000ms. Trying interrupt().
[Timer-2] 24/06/2014 21:33:11 WARN ThreadPoolAsynchronousRunner: Task com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@7113fb05 (in deadlocked PoolThread) failed to complete in maximum time 60000ms. Trying interrupt().
[Timer-2] 24/06/2014 21:33:11 WARN ThreadPoolAsynchronousRunner: Task com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@56c53f42 (in deadlocked PoolThread) failed to complete in maximum time 60000ms. Trying interrupt().
[Timer-2] 24/06/2014 21:33:11 WARN ThreadPoolAsynchronousRunner: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@47ecfc46 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
[Timer-2] 24/06/2014 21:33:11 WARN ThreadPoolAsynchronousRunner: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@47ecfc46 -- APPARENT DEADLOCK!!! Complete Status:
Managed Threads: 3
Active Threads: 3
Active Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@72937d83 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@31155e39 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@71dd095b (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
Pending Tasks:
com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@66916fc3
com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask@56409fd0
com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@2cbc3ced
com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@7a44028c
com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@703740a
com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@73b7401e
com.mchange.v2.resourcepool.BasicResourcePool$1DestroyResourceTask@3d2acec
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@7c56bbe9
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@224afdc5
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@695aa219
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@3d484d58
com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@55cdebd6
Pool thread stack traces:
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:129)
oracle.net.ns.Packet.receive(Unknown Source)
oracle.net.ns.DataPacket.receive(Unknown Source)
oracle.net.ns.NetInputStream.getNextPacket(Unknown Source)
oracle.net.ns.NetInputStream.read(Unknown Source)
oracle.net.ns.NetInputStream.read(Unknown Source)
oracle.net.ns.NetInputStream.read(Unknown Source)
oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:971)
oracle.jdbc.driver.T4CMAREngine.unmarshalSB1(T4CMAREngine.java:941)
oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:432)
oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:181)
oracle.jdbc.driver.T4CPreparedStatement.execute_for_describe(T4CPreparedStatement.java:420)
oracle.jdbc.driver.OracleStatement.execute_maybe_describe(OracleStatement.java:896)
oracle.jdbc.driver.T4CPreparedStatement.execute_maybe_describe(T4CPreparedStatement.java:452)
oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:986)
oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888)
oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:2929)
oracle.jdbc.OracleDatabaseMetaData.getTables(OracleDatabaseMetaData.java:2311)
com.mchange.v2.c3p0.impl.DefaultConnectionTester.activeCheckConnectionNoQuery(DefaultConnectionTester.java:185)
com.mchange.v2.c3p0.impl.DefaultConnectionTester.activeCheckConnection(DefaultConnectionTester.java:62)
com.mchange.v2.c3p0.AbstractConnectionTester.activeCheckConnection(AbstractConnectionTester.java:67)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.testPooledConnection(C3P0PooledConnectionPool.java:368)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.refurbishIdleResource(C3P0PooledConnectionPool.java:310)
com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask.run(BasicResourcePool.java:1999)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:129)
oracle.net.ns.Packet.receive(Unknown Source)
oracle.net.ns.DataPacket.receive(Unknown Source)
oracle.net.ns.NetInputStream.getNextPacket(Unknown Source)
oracle.net.ns.NetInputStream.read(Unknown Source)
oracle.net.ns.NetInputStream.read(Unknown Source)
oracle.net.ns.NetInputStream.read(Unknown Source)
oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:971)
oracle.jdbc.driver.T4CMAREngine.unmarshalSB1(T4CMAREngine.java:941)
oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:432)
oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:181)
oracle.jdbc.driver.T4CPreparedStatement.execute_for_describe(T4CPreparedStatement.java:420)
oracle.jdbc.driver.OracleStatement.execute_maybe_describe(OracleStatement.java:896)
oracle.jdbc.driver.T4CPreparedStatement.execute_maybe_describe(T4CPreparedStatement.java:452)
oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:986)
oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888)
oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:2929)
oracle.jdbc.OracleDatabaseMetaData.getTables(OracleDatabaseMetaData.java:2311)
com.mchange.v2.c3p0.impl.DefaultConnectionTester.activeCheckConnectionNoQuery(DefaultConnectionTester.java:185)
com.mchange.v2.c3p0.impl.DefaultConnectionTester.activeCheckConnection(DefaultConnectionTester.java:62)
com.mchange.v2.c3p0.AbstractConnectionTester.activeCheckConnection(AbstractConnectionTester.java:67)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.testPooledConnection(C3P0PooledConnectionPool.java:368)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.refurbishIdleResource(C3P0PooledConnectionPool.java:310)
com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask.run(BasicResourcePool.java:1999)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
java.net.SocketInputStream.socketRead0(Native Method)
java.net.SocketInputStream.read(SocketInputStream.java:129)
oracle.net.ns.Packet.receive(Unknown Source)
oracle.net.ns.DataPacket.receive(Unknown Source)
oracle.net.ns.NetInputStream.getNextPacket(Unknown Source)
oracle.net.ns.NetInputStream.read(Unknown Source)
oracle.net.ns.NetInputStream.read(Unknown Source)
oracle.net.ns.NetInputStream.read(Unknown Source)
oracle.jdbc.driver.T4CMAREngine.unmarshalUB1(T4CMAREngine.java:971)
oracle.jdbc.driver.T4CMAREngine.unmarshalSB1(T4CMAREngine.java:941)
oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:432)
oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:181)
oracle.jdbc.driver.T4CPreparedStatement.execute_for_describe(T4CPreparedStatement.java:420)
oracle.jdbc.driver.OracleStatement.execute_maybe_describe(OracleStatement.java:896)
oracle.jdbc.driver.T4CPreparedStatement.execute_maybe_describe(T4CPreparedStatement.java:452)
oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:986)
oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888)
oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:2929)
oracle.jdbc.OracleDatabaseMetaData.getTables(OracleDatabaseMetaData.java:2311)
com.mchange.v2.c3p0.impl.DefaultConnectionTester.activeCheckConnectionNoQuery(DefaultConnectionTester.java:185)
com.mchange.v2.c3p0.impl.DefaultConnectionTester.activeCheckConnection(DefaultConnectionTester.java:62)
com.mchange.v2.c3p0.AbstractConnectionTester.activeCheckConnection(AbstractConnectionTester.java:67)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.testPooledConnection(C3P0PooledConnectionPool.java:368)
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.refurbishIdleResource(C3P0PooledConnectionPool.java:310)
com.mchange.v2.resourcepool.BasicResourcePool$AsyncTestIdleResourceTask.run(BasicResourcePool.java:1999)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

这是我的 hibernate 配置 c3p0

<property name="connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.timeout">5</property>
<property name="hibernate.c3p0.max_statements">0</property>
<property name="hibernate.c3p0.idle_test_period">60</property>

我可以在我的代码中找到错误,但它会导致“明显的死锁异常”我能做什么?

最佳答案

失败的是检查空闲连接是否仍然有效的测试线程。显然,默认查询 getTables() 只是获取架构中所有表的列表,正在超时。这是否是数据库崩溃很难说,但是你没有通过 5 秒的超时来帮助事情,这导致你每 5 秒关闭并重新打开所有 5 个连接。将其设置为合理的值,例如 1800。您也可以尝试设置不同的连接测试,或者完全关闭连接测试。

此配置关闭空闲测试并将超时设置得更高:

<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">50</property>
<property name="hibernate.c3p0.timeout">1800</property>
<property name="hibernate.c3p0.max_statements">0</property>

关于java - Hibernate 更改某行代码得到明显的死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24399211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com