近期项目需要从虚拟机环境迁移到容器环境,其中有一个项目在迁移到容器环境之后的两天之内出现了2次“死锁(deadlock)”的问题,部分关键日志如下:
Found one JAVA-level deadlock:============================="DefaultMessageListenerContainer-9": waiting to lock monitor 0x00007fde3400bf38 (object 0x00000000dda358d0, a oracle.jdbc.driver.T4CConnection), which is held by "DefaultMessageListenerContainer-7""DefaultMessageListenerContainer-7": waiting to lock monitor 0x00007fdea000b478 (object 0x00000000dda35578, a oracle.jdbc.driver.T4CConnection), which is held by "DefaultMessageListenerContainer-9"Java stack information for the threads listed above:==================================================="DefaultMessageListenerContainer-9": at oracle.jdbc.oracore.OracleTypeADT.linearize(OracleTypeADT.java:1280) - waiting to lock <0x00000000dda358d0> (a oracle.jdbc.driver.T4CConnection) at oracle.sql.ArrayDescriptor.toBytes(ArrayDescriptor.java:653) at oracle.sql.ARRAY.toBytes(ARRAY.java:711) - locked <0x00000000dda35578> (a oracle.jdbc.driver.T4CConnection) at oracle.jdbc.driver.OraclePreparedStatement.setArrayCritical(OraclePreparedStatement.java:6049) at oracle.jdbc.driver.OraclePreparedStatement.setARRAYInternal(OraclePreparedStatement.java:6008) - locked <0x00000000dda35578> (a oracle.jdbc.driver.T4CConnection) at oracle.jdbc.driver.OraclePreparedStatement.setArrayInternal(OraclePreparedStatement.java:5963) at oracle.jdbc.driver.OracleCallableStatement.setArray(OracleCallableStatement.java:4833) at oracle.jdbc.driver.OraclePreparedStatementWrApper.setArray(OraclePreparedStatementWrapper.java:114)"DefaultMessageListenerContainer-7": at oracle.jdbc.oracore.OracleTypeADT.linearize(OracleTypeADT.java:1280) - waiting to lock <0x00000000dda35578> (a oracle.jdbc.driver.T4CConnection) at oracle.sql.ArrayDescriptor.toBytes(ArrayDescriptor.java:653) at oracle.sql.ARRAY.toBytes(ARRAY.java:711) - locked <0x00000000dda358d0> (a oracle.jdbc.driver.T4CConnection) at oracle.jdbc.driver.OraclePreparedStatement.setArrayCritical(OraclePreparedStatement.java:6049) at oracle.jdbc.driver.OraclePreparedStatement.setARRAYInternal(OraclePreparedStatement.java:6008) - locked <0x00000000dda358d0> (a oracle.jdbc.driver.T4CConnection) at oracle.jdbc.driver.OraclePreparedStatement.setArrayInternal(OraclePreparedStatement.java:5963) at oracle.jdbc.driver.OracleCallableStatement.setArray(OracleCallableStatement.java:4833) at oracle.jdbc.driver.OraclePreparedStatementWrapper.setArray(OraclePreparedStatementWrapper.java:114) at日志还是挺明显的,线程DefaultMessageListenerContainer-9获得了锁0x00000000dda35578,等待获取0x00000000dda358d0;而DefaultMessageListenerContainer-7正好相反,从而导致死锁;
问题分析以上的错误日志和Oracle的驱动类有关,所以猜测是驱动版本的问题,所以找相关人员分别拉取了虚拟机环境和容器环境的生产Oracle驱动jar包,结果如下:
#虚拟机[19:38:21 oracle@Tomcat-384 lib]$ ls -l ojdbc-1.4.jar-rw-r--r-- 1 oracle oinstall 1378346 Jul 3 2014 ojdbc-1.4.jar #容器[oracle@7f666c76b7-dx2gq lib]$ ls -l ojdbc6.jar-rw-r--r-- 1 oracle oinstall 2739670 Aug 11 2015 ojdbc6.jar两个环境使用了不同的版本,容器使用了高版本(11.2.0.4.0),虚拟机使用的是低版本(10.1.0.5.0);google查询了和Oracle驱动相关产生死锁的问题,查到了Oracle官方有如下文档:
Java-level deadlock with 11.2(https://support.oracle.com/knowledge/Middleware/1271651_1.html)
提供给我们的方案是“Upgraded the Oracle JDBC driver from 10.2 to 11.2.”,正好和我们遇到的情况相反,我们是高版本有问题,低版本没有问题,所以需要进一步分析;
源码分析首先找到相关的逻辑代码类,此处为了更好的看出问题,使用了如下的模拟类,大致如下:
//测试Dao,配置在spring下的单例public class TestDaoImpl { //共享的两个ArrayDescriptor private ArrayDescriptor param1Desc; private ArrayDescriptor param2Desc; private String param1; private String param2; private DataSource dataSource; public void callProc(Object param) { // 准备的两个ARRAY参数 ARRAY param1Array = null; ARRAY param2Array = null; CallableStatement callable = null; Connection conn = null; try { // 从连接池获取连接 conn = DataSourceUtils.getConnection(dataSource); param1Array = wrapProcParameter1(param, conn); param2Array = wrapProcParameter2(param, conn); callable = conn.prepareCall("{ call testProc " + "(?,?,?)}"); callable.setArray(1, param1Array); callable.setArray(2, param2Array); callable.execute(); } catch (Exception e) { // 异常处理 } finally { // 关闭处理 } } private ARRAY wrapProcParameter1(Object param, Connection conn) throws SQLException { if (null == this.param1Desc) { this.param1Desc = new ArrayDescriptor(this.param1, conn); } //省略 ARRAY array1 = new ARRAY(this.param1Desc, conn, param); return array1; } private ARRAY wrapProcParameter2(Object param, Connection conn) throws SQLException { if (null == this.param2Desc) { this.param2Desc = new ArrayDescriptor(this.param2, conn); } //省略 ARRAY array2 = new ARRAY(this.param2Desc, conn, param); return array2; }}
推荐阅读
- 一次性用品回收再利用 日用品库存回收公司
- 房屋家居风水摆设要求要谨记
- 逛中国茶膏博物馆记
- 密室逃脱一般多少钱一次,密室逃脱是越贵越好玩吗
- 大学本科学生登记表 梦到喂孩子奶奶水超多
- 从裸游云南看福村梅记事件营销
- 2022北京冬奥会是中国第几次举办奥运会,冬奥会是第一次在中国举办吗
- 如何在记事本中制作关机程序
- 《桃花源记》是恐怖故事
- 和风咏雪仰天绿芳茗迎春盖世香——记王章春先生和他的仰天雪绿茶