Hibernate更改了一些代码行获取APPARENT DEADLOCKS

时间:2014-06-25 02:21:29

标签: java oracle hibernate exception

我使用Oracle数据库和我的java应用程序使用Hibernate 3.3.2与它进行交互。

我总是使用这样的代码连接到DB

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会话)

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();
            }

        }
    }

然后我得到了很多APPARENT DEADLOCKS。

[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>

我可以在我的代码中找到错误,但它会使&#34; APPARENT DEADLOCKS异常&#34; 我该怎么办?

1 个答案:

答案 0 :(得分:1)

什么是失败的是检查空闲连接是否仍然有效的测试线程。显然,默认查询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>