We have a java application where we are using Bonecp pooling library. Below is how we setup the pooling.
Then for insert and update queries we do this and below we make sure closed the statement.try { // setup the connection pool BoneCPConfig config = new BoneCPConfig(); config.setJdbcUrl("jdbc:mysql://**.**.**.**:3306/test1"); config.setUsername("******"); config.setPassword("*******"); config.setMinConnectionsPerPartition(5); config.setMaxConnectionsPerPartition(40); config.setPartitionCount(1); connectionPool = new BoneCP(config); // setup the connection pool } catch (SQLException e) { e.printStackTrace(System.out); }
Statement stmt1 = null; stmt1 = dbconn.createStatement(); String insertQuery3 =........ count = stmt9.executeUpdate(insertQuery3); try{ if ( stmt1!= null ){ stmt1.close(); } else{ System.out.println("No stm1 exist"); } }catch(SQLException ex){ System.out.println("SQLException has been caught for stmt1"); ex.printStackTrace(System.out); } For select we do this Statement stmt2 = null; stmt2 = dbconn.createStatement(); String selectQuery2= ......... ResultSet rs2 = stmt2.executeQuery(selectQuery2); if(rs2.next()) { } try{ if ( rs2!= null ){ rs2.close(); } else{ System.out.println("No rs2 exist"); } if ( stmt2!= null ){ stmt2.close(); } else{ System.out.println("No stm2 exist"); } }catch(SQLException ex){ System.out.println("SQLException has been caught for stmt2"); ex.printStackTrace(System.out); }
Thus we have ensured that every resultset and statement is closed immediately after its use. The problem now when we took few sample from MAT is show increasing memory usage as below.
Sample 1
21 instances of "com.mysql.jdbc.JDBC4Connection", loaded by "sun.misc.Launcher$ExtClassLoader @ 0xf000a4f0" occupy 11,793,200 (76.19%) bytes. Biggest instances: •com.mysql.jdbc.JDBC4Connection @ 0xf016d728 - 958,096 (6.19%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0094478 - 875,568 (5.66%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0201ba0 - 816,048 (5.27%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf04ab3f0 - 754,016 (4.87%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf006ee40 - 727,024 (4.70%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0076a90 - 663,872 (4.29%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf04ad490 - 618,200 (3.99%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf00b7bd0 - 616,608 (3.98%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0467bc0 - 612,544 (3.96%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf015fcf0 - 598,400 (3.87%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf021c830 - 584,992 (3.78%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0097818 - 561,224 (3.63%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf01a27c0 - 531,816 (3.44%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf00bea28 - 531,416 (3.43%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf01c2d80 - 522,184 (3.37%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf04693e0 - 482,992 (3.12%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf012b158 - 453,256 (2.93%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf147f438 - 424,656 (2.74%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf15ff7d0 - 187,008 (1.21%) bytes. Keywords com.mysql.jdbc.JDBC4Connection sun.misc.Launcher$ExtClassLoader @ 0xf000a4f0 Reference Pattern Class Name Shallow Heap Retained Heap Percentage class java.lang.Thread @ 0xf0003840 40 208 0.00
Sample 2
21 instances of "com.mysql.jdbc.JDBC4Connection", loaded by "sun.misc.Launcher$ExtClassLoader @ 0xf000a4f0" occupy 17,339,632 (80.79%) bytes. Biggest instances: •com.mysql.jdbc.JDBC4Connection @ 0xf016d728 - 1,228,048 (5.72%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0094478 - 1,144,056 (5.33%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0201ba0 - 1,126,120 (5.25%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf04ab3f0 - 1,074,552 (5.01%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf006ee40 - 993,912 (4.63%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0076a90 - 931,512 (4.34%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf015fcf0 - 930,952 (4.34%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf04ad490 - 918,176 (4.28%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf00bea28 - 874,512 (4.07%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0467bc0 - 846,368 (3.94%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf00b7bd0 - 838,448 (3.91%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf021c830 - 799,184 (3.72%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf0097818 - 791,256 (3.69%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf01a27c0 - 763,264 (3.56%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf01c2d80 - 745,088 (3.47%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf012b158 - 710,704 (3.31%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf04693e0 - 704,072 (3.28%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf147f438 - 680,400 (3.17%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf1603618 - 458,472 (2.14%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf148fd70 - 390,984 (1.82%) bytes. •com.mysql.jdbc.JDBC4Connection @ 0xf15ff7d0 - 389,552 (1.82%) bytes. Keywords com.mysql.jdbc.JDBC4Connection sun.misc.Launcher$ExtClassLoader @ 0xf000a4f0 Reference Pattern Class Name Shallow Heap Retained Heap Percentage class java.lang.Thread @ 0xf0003840 40 208 0.00%