gpt4 book ai didi

java - Hibernate 4.0 的事务锁定问题

转载 作者:行者123 更新时间:2023-12-02 08:44:30 27 4
gpt4 key购买 nike

有时我会遇到锁定问题,例如:

java.sql.SQLTransactionRollbackException: A lock could not be obtained within the time requested

我将 Hibernate 与 C3p0 池结合使用,并将 Hibernate 配置为乐观锁定。

我还有一些绕过 Hibernate 并通过独立配置的 c3p0 池与数据库对话的代码。这纯粹是因为这段代码在我迁移到 Hibernate 之前就已经存在并且工作得很好,所以我当时认为没有必要更改它。

现在我想知道两个独立配置的 c3p0 池是否会导致问题。如果不是,我如何追踪这些异常的原因,我将池设置为 20 到 100 个连接,并且我最多只有 12 个并发线程,我认为当我完成它们时,我的所有事务/ session 都将被关闭。

编辑:现在有单个池,但仍然遇到问题,出现以下错误,但没有详细说明其原因,我注意到的一件事是它总是显示托管线程:3

Exception with lookup
12:42:36,627 WARN ThreadPoolAsynchronousRunner:608 - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1ff96a2 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
12:42:36,628 WARN ThreadPoolAsynchronousRunner:624 - com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@1ff96a2 -- APPARENT DEADLOCK!!! Complete Status:
Managed Threads: 3
Active Threads: 3
Active Tasks:
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StatementCloseTask@fdfb9a (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StatementCloseTask@914847 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StatementCloseTask@205390 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
Pending Tasks:
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StatementCloseTask@4e171b
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@ceeecb
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@19f7cec
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@1c299f9
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StmtAcquireTask@10ab38a
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StmtAcquireTask@1916a2f
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@1d23fbf
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StmtAcquireTask@573b7c
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@1027733
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@dfd9b0
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StmtAcquireTask@4cecbb
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StatementCloseTask@4a0d0b
com.mchange.v2.resourcepool.BasicResourcePool$1RefurbishCheckinResourceTask@19e809d
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StmtAcquireTask@10de0f8
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StmtAcquireTask@2ce568
Pool thread stack traces:
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,JAIKOZ Thread Group]
org.apache.derby.impl.jdbc.EmbedStatement.close(Unknown Source)
com.mchange.v1.db.sql.StatementUtils.attemptClose(StatementUtils.java:41)
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StatementCloseTask.run(GooGooStatementCache.java:404)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,JAIKOZ Thread Group]
org.apache.derby.impl.jdbc.EmbedStatement.close(Unknown Source)
com.mchange.v1.db.sql.StatementUtils.attemptClose(StatementUtils.java:41)
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StatementCloseTask.run(GooGooStatementCache.java:404)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,JAIKOZ Thread Group]
org.apache.derby.impl.jdbc.EmbedStatement.close(Unknown Source)
com.mchange.v1.db.sql.StatementUtils.attemptClose(StatementUtils.java:41)
com.mchange.v2.c3p0.stmt.GooGooStatementCache$1StatementCloseTask.run(GooGooStatementCache.java:404)
com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

可能是这个问题

https://forum.hibernate.org/viewtopic.php?p=2390809

最佳答案

鉴于SQLTransactionRollbackException是数据库级锁定失败,单独配置的c3p0池并不是导致此问题的原因。如果是这种情况,您将无法运行同一基于 Hibernate 的应用程序的两个实例。

这里的第一步应该是使用调试器在引发异常时停止。然后检查其他数据库连接池线程,看看它们是否正在对数据库执行任何操作。如果是,这将是第一个要查看的地方,因为您可能会遇到由数据库级锁引起的死锁。如果您可以在重现问题的同时减少池中的线程数,那么此步骤可能会更容易。

原因可能是另一个线程获得了数据库锁,然后没有释放。如果是这种情况,您将必须使用数据库工具找出抛出异常的线程无法获取哪些资源,然后尝试找出谁拥有该锁以及原因。

祝你好运。

关于java - Hibernate 4.0 的事务锁定问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10177959/

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