3

我有一些工作线程正在运行,使用 MySQL 和 mysql-connector-java-5.1.20。当我杀死一些 SQL 语句(使用 mysql 客户端的 kill “connection id”)时,java 线程挂起,这应该会引发一些异常。

jstack 打印:

    "quartzBase$child#45e3dd3c_Worker-3" prio=10 tid=0x00007f960004c800 nid=0x713d runnable [0x00007f943b3a0000]
   java.lang.Thread.State: RUNNABLE
        at java.net.PlainSocketImpl.socketAvailable(Native Method)
        at java.net.PlainSocketImpl.available(PlainSocketImpl.java:472)
        - locked <0x00007f9e11fe13a8> (a java.net.SocksSocketImpl)
        at java.net.SocketInputStream.available(SocketInputStream.java:217)
        at com.mysql.jdbc.util.ReadAheadInputStream.available(ReadAheadInputStream.java:232)
        at com.mysql.jdbc.MysqlIO.clearInputStream(MysqlIO.java:981)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2426)
        at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
        at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
        - locked <0x00007f9e17de2b50> (a com.mysql.jdbc.JDBC4Connection)
        at com.mysql.jdbc.ConnectionImpl.rollbackNoChecks(ConnectionImpl.java:4863)
        at com.mysql.jdbc.ConnectionImpl.rollback(ConnectionImpl.java:4749)
        - locked <0x00007f9e17de2b50> (a com.mysql.jdbc.JDBC4Connection)
        at org.apache.commons.dbcp.DelegatingConnection.rollback(DelegatingConnection.java:368)
        at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.rollback(PoolingDataSource.java:323)
        at org.hibernate.transaction.JDBCTransaction.rollbackAndResetAutoCommit(JDBCTransaction.java:217)
        at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:196)
        at org.springframework.orm.hibernate3.HibernateTransactionManager.doRollback(HibernateTransactionManager.java:676)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.processRollback(AbstractPlatformTransactionManager.java:845)
        at org.springframework.transaction.support.AbstractPlatformTransactionManager.rollback(AbstractPlatformTransactionManager.java:822)
        at org.springframework.transaction.interceptor.TransactionAspectSupport.completeTransactionAfterThrowing(TransactionAspectSupport.java:430)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:112)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at $Proxy1021.process(Unknown Source)

使用 jvmtop,我看到了这个:

JvmTop 0.8.0 alpha - 22:48:37,  amd64, 24 cpus, Linux 2.6.32-35, load avg 11.53
 http://code.google.com/p/jvmtop

 Profiling PID 27403: com.caucho.server.resin.Resin --root-dir 

  36.41% (     0.22s) com.mysql.jdbc.util.ReadAheadInputStream.available()
  33.42% (     0.20s) ....opensymphony.xwork2.conversion.impl.DefaultTypeConve()
  30.17% (     0.18s) com.mysql.jdbc.util.ReadAheadInputStream.fill()
   0.00% (     0.00s) com.rabbitmq.client.impl.Frame.readFrom()

工作线程永远不会接受新任务。

任何想法?

4

1 回答 1

0

根据 MySQL文档, “kill connection thread_id”应该终止与给定 thread_id 关联的连接。但看起来这并没有发生(在这种情况下,Java 线程将永远等待答案)。也许您可以使用某些网络工具(例如 netstat)验证连接是否实际关闭。

我之前遇到过挂起 MySQL 连接,不得不求助于使用socketTimeout JDBC 连接参数(但要小心:socketTimeout 需要大于完成最长运行查询所需的时间)。您还可以尝试将QueryTimeout用于准备好的语句。

于 2013-12-17T21:19:57.500 回答