001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.memory.ConnectionHandling
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.memory;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.SQLException;
027: import java.util.ArrayList;
028: import java.util.Properties;
029:
030: /**
031: * Test opening connections until a failure due to out of memory
032: * and then continue with 500 connection requests to see if the
033: * system reacts well of falls over.
034: *
035: */
036: public class ConnectionHandling {
037:
038: public static void main(String[] args) throws Exception {
039:
040: System.out.println("Test ConnectionHandling starting");
041:
042: new org.apache.derby.jdbc.EmbeddedDriver();
043:
044: Connection conn = DriverManager
045: .getConnection("jdbc:derby:wombat;create=true");
046: conn.close();
047: conn = null;
048:
049: ArrayList list = new ArrayList();
050: list.ensureCapacity(30000);
051:
052: Properties p = new Properties();
053:
054: while (true) {
055: Connection c;
056: try {
057:
058: c = DriverManager.getConnection("jdbc:derby:wombat", p);
059: } catch (SQLException e) {
060: if ("08004".equals(e.getSQLState()))
061: System.out.println("FIRST OOME: " + e.getSQLState()
062: + " " + e.getMessage());
063: else {
064: System.out.println("UNKNOWN ERROR "
065: + e.getSQLState() + " " + e.getMessage());
066: e.printStackTrace(System.out);
067: }
068: break;
069: } catch (Throwable t) {
070: System.out.println("UNKNOWN ERROR " + t);
071: t.printStackTrace(System.out);
072: break;
073: }
074: list.add(c);
075: if ((list.size() % 1000) == 0) {
076: System.out.print(".");
077: }
078: }
079:
080: System.out.println("");
081:
082: System.out.println(list.size() + " successful connections");
083:
084: list.ensureCapacity(list.size() + 500);
085:
086: // try to make 500 more connection requests.
087: int fail_sqloome = 0;
088: int fail_sql = 0;
089: int fail_bad = 0;
090: int ok = 0;
091: for (int i = 0; i < 500; i++) {
092: // Sleep for 10 secs as we know the implementation
093: // of the low meory watermark resets after 5 seconds.
094: if (i == 300)
095: Thread.sleep(10000L);
096: try {
097: Connection c = DriverManager.getConnection(
098: "jdbc:derby:wombat", p);
099: list.add(c);
100: ok++;
101: } catch (SQLException e) {
102: if ("08004".equals(e.getSQLState()))
103: fail_sqloome++;
104: else {
105: fail_sql++;
106: System.out.println("UNKNOWN ERROR "
107: + e.getSQLState() + " " + e.getMessage());
108: e.printStackTrace(System.out);
109: }
110: } catch (Throwable t) {
111: fail_bad++;
112: System.out.println("UNKNOWN ERROR " + t);
113: t.printStackTrace(System.out);
114: }
115: }
116:
117: System.out.println("OK : " + ok);
118: System.out.println("Failed 08004 : " + fail_sqloome);
119: System.out.println("Failed SQLException : " + fail_sql);
120: System.out.println("Failed Throwable : " + fail_bad);
121:
122: System.out.println("closing connections : " + list.size());
123: int alreadyClosed = 0;
124: for (int i = 0; i < list.size(); i++) {
125: Connection c = (Connection) list.get(i);
126: list.set(i, null);
127: if (c.isClosed())
128: alreadyClosed++;
129: else
130: c.close();
131: }
132: System.out.println("already closed : " + alreadyClosed);
133: }
134: }
|