MySQL C3p0 明显死锁异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18100414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:27:55  来源:igfitidea点击:

C3p0 APPARENT DEADLOCK exception

mysqlhibernatec3p0database-deadlocks

提问by corderazo00

I keep getting this exception in my Tomcat log:

我在 Tomcat 日志中不断收到此异常:

com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@76b28200 -- APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks!
com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector run
WARNING: com.mchange.v2.async.ThreadPoolAsynchronousRunner$DeadlockDetector@76b28200 -- APPARENT DEADLOCK!!! Complete Status: 
    Managed Threads: 3
    Active Threads: 3
    Active Tasks: 
        com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@1201fd18 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1)
        com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@408f3be4 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0)
        com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@7ba516d8 (com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2)
    Pending Tasks: 
        com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@137efe53
        com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@766b0524
Pool thread stack traces:
    Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1,5,main]
        java.lang.Thread.sleep(Native Method)
        com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
        com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
    Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0,5,main]
        java.lang.Thread.sleep(Native Method)
        com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
        com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)
    Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
        java.lang.Thread.sleep(Native Method)
        com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1805)
        com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

I am using Hibernate 3.6.2 and C3P0 0.9.1.2 with MySQL. After a couple of hours of searching Google, this APPARENT DEADLOCK exception seems to be usually associated with prepared statement caching. This is my C3P0 configuration in my hibernate.cfg.xml:

我在 MySQL 中使用 Hibernate 3.6.2 和 C3P0 0.9.1.2。在谷歌搜索了几个小时后,这个 APPARENT DEADLOCK 异常似乎通常与准备好的语句缓存相关联。这是我在 hibernate.cfg.xml 中的 C3P0 配置:

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

I am not doing any statement caching whatsoever. Any hints of what is wrong here would be greatly appreciated.

我没有做任何语句缓存。任何关于这里有什么问题的提示将不胜感激。

采纳答案by Steve Waldman

The tasks that are dealocking are Connection acquisition tasks. That is, c3p0 is trying to acquire new Connections from your database, and those Connection acquisition attempts are taking a long time.

解除锁定的任务是连接获取任务。也就是说,c3p0 正在尝试从您的数据库中获取新的连接,而这些连接获取尝试需要很长时间。

The first thing I would do is upgrade to 0.9.2.1, which has a much improved means of performing a round of Connection acquisitions in situations where acquisition attempts sometimes fail.

我要做的第一件事是升级到 0.9.2.1,它具有在获取尝试有时失败的情况下执行一轮连接获取的大大改进的方法。

If that doesn't solve your problem, then you'll need to figure out why c3p0's attempts to acquire a Connection are hanging for long periods of time: neither succeeding nor failing with an Exception.

如果这不能解决您的问题,那么您需要弄清楚为什么 c3p0 获取连接的尝试会长时间挂起:既没有成功也没有失败并出现异常。

回答by Gus Vargas

In my very particular case the problem had to do with the configuration of the server where I deployed my application.

在我非常特殊的情况下,问题与我部署应用程序的服务器的配置有关。

It was only after printing out the stack trace of my application that it was hung because the oracle driver was waiting for a secure random number being generated:

只有在打印出我的应用程序的堆栈跟踪后,它才挂起,因为 oracle 驱动程序正在等待生成安全随机数:

Thread[com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#2,5,main]
  java.io.FileInputStream.readBytes(Native Method)
  java.io.FileInputStream.read(FileInputStream.java:255)
  sun.security.provider.SeedGenerator$URLSeedGenerator.getSeedBytes(SeedGenerator.java:539)
  sun.security.provider.SeedGenerator.generateSeed(SeedGenerator.java:144)
  sun.security.provider.SecureRandom$SeederHolder.(SecureRandom.java:203)
  sun.security.provider.SecureRandom.engineNextBytes(SecureRandom.java:221)
  java.security.SecureRandom.nextBytes(SecureRandom.java:468)
  oracle.security.o5logon.O5Logon.a(Unknown Source)
  oracle.security.o5logon.O5Logon.(Unknown Source)
  oracle.jdbc.driver.T4CTTIoauthenticate.(T4CTTIoauthenticate.java:582)
  oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:401)
  oracle.jdbc.driver.PhysicalConnection.(PhysicalConnection.java:553)
  oracle.jdbc.driver.T4CConnection.(T4CConnection.java:254)
  oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
  oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:528)
  com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
  com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
  com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
  com.mchange.v2.c3p0.impl.C3P0PooledConnectionPoolPooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
  com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
  com.mchange.v2.resourcepool.BasicResourcePool.access0(BasicResourcePool.java:32)
  com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
  com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

Then I had to learn and understand a bit about entropy noise and could realize, with a little help from my Unix friends, that one of the servers had no installed a package that generates the "noise".

然后我必须学习和了解一些关于熵噪声的知识,并且可以在我的 Unix 朋友的帮助下意识到其中一台服务器没有安装产生“噪声”的软件包。

As mentioned in the following post I ran the next commands: cat /proc/sys/kernel/random/entropy_avail 23 cat /proc/sys/kernel/random/poolsize 4096

如以下帖子所述,我运行了下一个命令: cat /proc/sys/kernel/random/entropy_avail 23 cat /proc/sys/kernel/random/poolsize 4096

So there where too few entropy available to generate a secure random number.

因此,可用于生成安全随机数的熵太少。

After installing the package cat /proc/sys/kernel/random/entropy_avail 4096 Afterwards I had no more apparent deadlocks, my application could get connections to the DB.

安装包 cat /proc/sys/kernel/random/entropy_avail 4096 之后,我没有更明显的死锁,我的应用程序可以连接到数据库。

http://www.usn-it.de/index.php/2009/02/20/oracle-11g-jdbc-driver-hangs-blocked-by-devrandom-entropy-pool-empty/

http://www.usn-it.de/index.php/2009/02/20/oracle-11g-jdbc-driver-hangs-blocked-by-devrandom-entropy-pool-empty/

回答by simple

Got a similar error like this before.

之前遇到过类似的错误。

It could also be caused by database server blocking your IP. Make sure your servers' IP are not blocked by the database server/cloud provider.

这也可能是由于数据库服务器阻止了您的 IP 造成的。确保您的服务器的 IP 未被数据库服务器/云提供商阻止。

In my case, was trying to insert data to the database in the cloud with multithreading, and it only works on the local, but not on a server (on a different network).

就我而言,我试图通过多线程将数据插入云中的数据库,它仅适用于本地,而不适用于服务器(在不同的网络上)。