KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > memory > ConnectionHandling


1 /*
2
3 Derby - Class org.apache.derbyTesting.functionTests.tests.memory.ConnectionHandling
4
5 Licensed to the Apache Software Foundation (ASF) under one or more
6 contributor license agreements. See the NOTICE file distributed with
7 this work for additional information regarding copyright ownership.
8 The ASF licenses this file to You under the Apache License, Version 2.0
9 (the "License"); you may not use this file except in compliance with
10 the License. You may obtain a copy of the License at
11
12    http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 */

21
22 package org.apache.derbyTesting.functionTests.tests.memory;
23 import java.sql.Connection JavaDoc;
24 import java.sql.DriverManager JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Properties JavaDoc;
28
29 /**
30  * Test opening connections until a failure due to out of memory
31  * and then continue with 500 connection requests to see if the
32  * system reacts well of falls over.
33  *
34  */

35 public class ConnectionHandling {
36
37  public static void main(String JavaDoc[] args) throws Exception JavaDoc {
38
39         System.out.println("Test ConnectionHandling starting");
40
41
42         new org.apache.derby.jdbc.EmbeddedDriver();
43         
44         Connection JavaDoc conn = DriverManager.getConnection("jdbc:derby:wombat;create=true");
45         conn.close();
46         conn = null;
47         
48         ArrayList JavaDoc list = new ArrayList JavaDoc();
49         list.ensureCapacity(30000);
50         
51         Properties JavaDoc p = new Properties JavaDoc();
52         
53         while (true) {
54             Connection JavaDoc c;
55             try {
56
57                 c = DriverManager.getConnection("jdbc:derby:wombat", p);
58             } catch (SQLException JavaDoc 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 JavaDoc 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         // try to make 500 more connection requests.
86
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             // Sleep for 10 secs as we know the implementation
93
// of the low meory watermark resets after 5 seconds.
94
if (i == 300)
95                 Thread.sleep(10000L);
96             try {
97                   Connection JavaDoc c = DriverManager.getConnection("jdbc:derby:wombat", p);
98                   list.add(c);
99                   ok++;
100             } catch (SQLException JavaDoc 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 JavaDoc 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 JavaDoc c = (Connection JavaDoc) 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