1 21 22 package org.apache.derbyTesting.functionTests.tests.memory; 23 import java.sql.Connection ; 24 import java.sql.DriverManager ; 25 import java.sql.SQLException ; 26 import java.util.ArrayList ; 27 import java.util.Properties ; 28 29 35 public class ConnectionHandling { 36 37 public static void main(String [] args) throws Exception { 38 39 System.out.println("Test ConnectionHandling starting"); 40 41 42 new org.apache.derby.jdbc.EmbeddedDriver(); 43 44 Connection conn = DriverManager.getConnection("jdbc:derby:wombat;create=true"); 45 conn.close(); 46 conn = null; 47 48 ArrayList list = new ArrayList (); 49 list.ensureCapacity(30000); 50 51 Properties p = new Properties (); 52 53 while (true) { 54 Connection c; 55 try { 56 57 c = DriverManager.getConnection("jdbc:derby:wombat", p); 58 } catch (SQLException e) { 59 if ("08004".equals(e.getSQLState())) 60 System.out.println("FIRST OOME: " + e.getSQLState() + " " 61 + e.getMessage()); 62 else { 63 System.out.println("UNKNOWN ERROR " + e.getSQLState() + " " 64 + e.getMessage()); 65 e.printStackTrace(System.out); 66 } 67 break; 68 } catch (Throwable t) { 69 System.out.println("UNKNOWN ERROR " + t); 70 t.printStackTrace(System.out); 71 break; 72 } 73 list.add(c); 74 if ((list.size() % 1000) == 0) { 75 System.out.print("."); 76 } 77 } 78 79 System.out.println(""); 80 81 System.out.println(list.size() + " successful connections"); 82 83 list.ensureCapacity(list.size() + 500); 84 85 int fail_sqloome = 0; 87 int fail_sql = 0; 88 int fail_bad = 0; 89 int ok = 0; 90 for (int i = 0; i < 500; i++) 91 { 92 if (i == 300) 95 Thread.sleep(10000L); 96 try { 97 Connection c = DriverManager.getConnection("jdbc:derby:wombat", p); 98 list.add(c); 99 ok++; 100 } catch (SQLException e) { 101 if ("08004".equals(e.getSQLState())) 102 fail_sqloome++; 103 else { 104 fail_sql++; 105 System.out.println("UNKNOWN ERROR " + e.getSQLState() + " " + e.getMessage()); 106 e.printStackTrace(System.out); 107 } 108 } catch (Throwable t) { 109 fail_bad++; 110 System.out.println("UNKNOWN ERROR " + t); 111 t.printStackTrace(System.out); 112 } 113 } 114 115 System.out.println("OK : " + ok); 116 System.out.println("Failed 08004 : " + fail_sqloome); 117 System.out.println("Failed SQLException : " + fail_sql); 118 System.out.println("Failed Throwable : " + fail_bad); 119 120 System.out.println("closing connections : " + list.size()); 121 int alreadyClosed = 0; 122 for (int i = 0; i < list.size(); i++) 123 { 124 Connection c = (Connection ) list.get(i); 125 list.set(i, null); 126 if (c.isClosed()) 127 alreadyClosed++; 128 else 129 c.close(); 130 } 131 System.out.println("already closed : " + alreadyClosed); 132 } 133 } | Popular Tags |