连接泄漏与try-with-resources和HikariCP

时间:2015-07-22 12:04:03

标签: java openjdk hikaricp try-with-resources

以下代码触发连接泄漏警告。我使用的是OpenJDK 1.7.0_80和HikariCP 2.2.5(也可以使用最新的HikariCP 2.3.9重现)。我错过了什么吗?

[Test]
class NumbersTests
{
     Numbers numbers = null; //Double click on Numbers and press Alt-F12, the class will appear below it.
     private void TestSumTwoNumbers() {...}
}

UPDATE :略微更改return语句可以消除泄漏:

dataType: "xml"

1 个答案:

答案 0 :(得分:4)

似乎问题来自于AspectJ 1.7.2中的错误。它产生了以下字节码:

ireturn

注意第45行,close()跳过两个 private boolean shouldNotLeakConnection(com.zaxxer.hikari.HikariDataSource); Code: 0: ldc #115 // String INSERT INTO error_logs (description) values (?) 2: astore_2 3: aconst_null 4: astore_3 5: aconst_null 6: astore 4 8: aload_1 9: invokevirtual #117 // Method com/zaxxer/hikari/HikariDataSource.getConnection:()Ljava/sql/Connection; 12: astore 5 14: aload 5 16: aload_2 17: invokeinterface #121, 2 // InterfaceMethod java/sql/Connection.prepareStatement:(Ljava/lang/String;)Ljava/sql/PreparedStatement; 22: astore 6 24: aload 6 26: iconst_1 27: ldc #127 // String description 29: invokeinterface #129, 3 // InterfaceMethod java/sql/PreparedStatement.setString:(ILjava/lang/String;)V 34: aload 6 36: invokeinterface #135, 1 // InterfaceMethod java/sql/PreparedStatement.executeUpdate:()I 41: ifeq 48 44: iconst_1 45: goto 49 48: iconst_0 49: aload 6 51: ifnull 61 54: aload 6 56: invokeinterface #139, 1 // InterfaceMethod java/sql/PreparedStatement.close:()V 61: aload 5 63: ifnull 73 66: aload 5 68: invokeinterface #142, 1 // InterfaceMethod java/sql/Connection.close:()V 73: ireturn 74: astore_3 75: aload 6 77: ifnull 87 80: aload 6 82: invokeinterface #139, 1 // InterfaceMethod java/sql/PreparedStatement.close:()V 87: aload_3 88: athrow 89: astore 4 91: aload_3 92: ifnonnull 101 95: aload 4 97: astore_3 98: goto 113 101: aload_3 102: aload 4 104: if_acmpeq 113 107: aload_3 108: aload 4 110: invokevirtual #143 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 113: aload 5 115: ifnull 125 118: aload 5 120: invokeinterface #142, 1 // InterfaceMethod java/sql/Connection.close:()V 125: aload_3 126: athrow 127: astore 4 129: aload_3 130: ifnonnull 139 133: aload 4 135: astore_3 136: goto 151 139: aload_3 140: aload 4 142: if_acmpeq 151 145: aload_3 146: aload 4 148: invokevirtual #143 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 151: aload_3 152: athrow 153: astore_3 154: new #25 // class java/lang/RuntimeException 157: dup 158: aload_3 159: invokespecial #27 // Method java/lang/RuntimeException."<init>":(Ljava/lang/Throwable;)V 162: athrow Exception table: from to target type 24 49 74 any 73 74 74 any 14 61 89 any 73 89 89 any 8 127 127 any 3 73 153 Class java/sql/SQLException 74 153 153 Class java/sql/SQLException 方法。

使用AspectJ 1.8.6,生成正确的字节码:

ireturn

请注意,在第45行,gotoclose()替换,而users[0][customfields][0][type] ^ 没有跳过两个data: { 'wstoken': '**************', 'moodlewsrestformat': 'json', 'wsfunction': 'core_user_update_users', 'users': [{ id:'2328', firstname: document.getElementById('name').value, lastname: document.getElementById('surname').value, customfields: [ { type:'stopcity', value: document.getElementById('city1').value }, { type: 'postalcode', value: document.getElementById('postc').value }, { type: 'province', value: document.getElementById('prov').value }, { type: 'stopcountry', value: document.getElementById('country2').value }, { type: 'addressline1', value: document.getElementById('1').value }, { type: 'addressline2', value: document.getElementById('2').value }, { type: 'phonemobile', value: $("#mobile").attr('value') }, { type: 'phonework', value: $("#work").attr('value') } ] }] } 方法。

感谢@brettw帮助我缩小问题范围。