import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Types; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class PreparedStatementLeak { public static class MyThread extends Thread { public MyThread() { super(); } private int loop_; @Override public void run() { while (true) { try { Connection connection; connection = DriverManager .getConnection("jdbc:apache:commons:dbcp:/jts"); loop(connection); connection.close(); } catch (SQLException e1) { e1.printStackTrace(); } } } public void loop(Connection connection) { PreparedStatement pstmt = null; ResultSet rs = null; try { String sql = "SELECT COUNT(*) * " + (loop_++ % 1000) + " FROM EMP"; pstmt = connection.prepareStatement(sql); rs = pstmt.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); String[] columnNames = new String[columnCount]; int[] columnTypes = new int[columnCount]; for (int i = 0; i < columnCount; i++) { columnNames[i] = rsmd.getColumnName(i + 1); columnTypes[i] = rsmd.getColumnType(i + 1); } while (rs.next()) { for (int i = 0; i < columnNames.length; i++) { System.out.println(columnNames[i] + ":" + rs.getObject(columnNames[i])); } } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } } } public static void main(String[] args) { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Class.forName("org.apache.commons.dbcp.PoolingDriver"); } catch (Exception e) { System.out.println(e.getMessage()); return; } final int THREADS = 10; Thread[] threads = new Thread[THREADS]; for (int index = 0; index < THREADS; ++index) { threads[index] = new MyThread(); threads[index].start(); } while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }