001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.ShutdownDatabase
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.lang;
023:
024: /**
025: This test confirm
026: that no trouble happens when database , of which active connection exists with , was shut down.
027:
028:
029: Tested combinations for transaction of connection to a database that is shutted down is as next :
030:
031: With committed/rollbacked transatction :
032: <ul>
033: <li>
034: <ul>
035: <li> The only transaction was committed.
036: <li> The transaction was committed, and next transaction was committed.
037: <li> The transaction was rollbacked, and next transaction was commited.
038: </ul>
039: </li>
040:
041: <li>
042: <ul>
043: <li> The only transaction was rollbacked.
044: <li> The transaction was commited, and next transaction was rollbacked.
045: <li> The transaction was rollbacked, and next transaction was rollbacked.
046: </ul>
047: </li>
048: </ul>
049:
050: With not yet committed/rollbacked transaction :
051: <ul>
052: <li>
053: <ul>
054: <li> The only transaction was not committed/rollbacked.
055: <li> The transaction was committed, and next transaction was not committed/rollbacked yet.
056: <li> The transaction was rollbacked, and next transaction was not committed/rollbacked yet.
057: </ul>
058: </li>
059: </ul>
060:
061: @author Tomohito Nakayama
062: */
063:
064: import java.sql.Connection;
065: import java.sql.Statement;
066: import java.sql.PreparedStatement;
067: import java.sql.ResultSet;
068:
069: import java.io.IOException;
070: import java.sql.SQLException;
071:
072: import org.apache.derby.iapi.reference.SQLState;
073:
074: import org.apache.derbyTesting.functionTests.util.TestUtil;
075:
076: import org.apache.derby.iapi.error.StandardException;
077:
078: public class ShutdownDatabase {
079:
080: public static void main(String[] args) {
081: try {
082: // util.getPropertyArg(args);
083:
084: testShutDownWithCommitedTransaction();
085: testShutDownWithRollbackedTransaction();
086: testShutDownWithLeftTransaction();
087:
088: } catch (SQLException e) {
089: e.printStackTrace();
090:
091: } catch (Throwable t) {
092: t.printStackTrace();
093:
094: }
095:
096: }
097:
098: private static void testShutDownWithCommitedTransaction()
099: throws SQLException, ClassNotFoundException,
100: InstantiationException, IllegalAccessException {
101:
102: testOnlyTransactionWasCommited();
103: testTwiceCommited();
104: testOnceRollbackedAndCommited();
105:
106: }
107:
108: private static void testShutDownWithRollbackedTransaction()
109: throws SQLException, ClassNotFoundException,
110: InstantiationException, IllegalAccessException {
111:
112: testOnlyTransactionWasRollbacked();
113: testOnceCommitedAndRollbacked();
114: testTwiceRollbacked();
115:
116: }
117:
118: private static void testShutDownWithLeftTransaction()
119: throws SQLException, ClassNotFoundException,
120: InstantiationException, IllegalAccessException {
121:
122: testOnlyTransactionWasLeft();
123: testOnceCommitedAndLeft();
124: testOnceRollbackedAndLeft();
125:
126: }
127:
128: private static void testOnlyTransactionWasCommited()
129: throws SQLException, ClassNotFoundException,
130: InstantiationException, IllegalAccessException {
131:
132: final String dbname = "testOnlyTransactionWasCommitedDB";
133: Connection conn = null;
134:
135: try {
136: conn = openConnectionToNewDatabase(dbname);
137: createTestTable(conn);
138:
139: conn.setAutoCommit(false);
140: insertIntoTestTable(conn, 1, 1000);
141: conn.commit();
142:
143: shutdownDatabase(dbname);
144:
145: } catch (SQLException e) {
146: verifyShutdownError(e);
147: }
148:
149: conn = null;
150:
151: try {
152: conn = reopenConnectionToDatabase(dbname);
153: countRowInTestTable(conn);
154:
155: } finally {
156: if (conn != null) {
157: conn.close();
158: conn = null;
159: }
160: }
161:
162: }
163:
164: private static void testTwiceCommited() throws SQLException,
165: ClassNotFoundException, InstantiationException,
166: IllegalAccessException {
167:
168: final String dbname = "testTwiceCommitedDB";
169: Connection conn = null;
170:
171: try {
172: conn = openConnectionToNewDatabase(dbname);
173: createTestTable(conn);
174:
175: conn.setAutoCommit(false);
176: insertIntoTestTable(conn, 1, 1000);
177: conn.commit();
178: insertIntoTestTable(conn, 1001, 999);
179: conn.commit();
180:
181: shutdownDatabase(dbname);
182:
183: } catch (SQLException e) {
184: verifyShutdownError(e);
185: }
186:
187: conn = null;
188:
189: try {
190: conn = reopenConnectionToDatabase(dbname);
191: countRowInTestTable(conn);
192:
193: } finally {
194: if (conn != null) {
195: conn.close();
196: conn = null;
197: }
198: }
199:
200: }
201:
202: private static void testOnceRollbackedAndCommited()
203: throws SQLException, ClassNotFoundException,
204: InstantiationException, IllegalAccessException {
205:
206: final String dbname = "testOnceRollbackedAndCommitedDB";
207: Connection conn = null;
208:
209: try {
210: conn = openConnectionToNewDatabase(dbname);
211: createTestTable(conn);
212:
213: conn.setAutoCommit(false);
214: insertIntoTestTable(conn, 1, 1000);
215: conn.rollback();
216: insertIntoTestTable(conn, 1001, 999);
217: conn.commit();
218:
219: shutdownDatabase(dbname);
220:
221: } catch (SQLException e) {
222: verifyShutdownError(e);
223: }
224:
225: conn = null;
226:
227: try {
228: conn = reopenConnectionToDatabase(dbname);
229: countRowInTestTable(conn);
230:
231: } finally {
232: if (conn != null) {
233: conn.close();
234: conn = null;
235: }
236: }
237:
238: }
239:
240: private static void testOnlyTransactionWasRollbacked()
241: throws SQLException, ClassNotFoundException,
242: InstantiationException, IllegalAccessException {
243:
244: final String dbname = "testOnlyTransactionWasRollbackedDB";
245: Connection conn = null;
246:
247: try {
248: conn = openConnectionToNewDatabase(dbname);
249: createTestTable(conn);
250:
251: conn.setAutoCommit(false);
252: insertIntoTestTable(conn, 1, 1000);
253: conn.rollback();
254:
255: shutdownDatabase(dbname);
256:
257: } catch (SQLException e) {
258: verifyShutdownError(e);
259: }
260:
261: conn = null;
262:
263: try {
264: conn = reopenConnectionToDatabase(dbname);
265: countRowInTestTable(conn);
266:
267: } finally {
268: if (conn != null) {
269: conn.close();
270: conn = null;
271: }
272: }
273:
274: }
275:
276: private static void testOnceCommitedAndRollbacked()
277: throws SQLException, ClassNotFoundException,
278: InstantiationException, IllegalAccessException {
279:
280: final String dbname = "testOnceCommitedAndRollbackedDB";
281: Connection conn = null;
282:
283: try {
284: conn = openConnectionToNewDatabase(dbname);
285: createTestTable(conn);
286:
287: conn.setAutoCommit(false);
288: insertIntoTestTable(conn, 1, 1000);
289: conn.commit();
290: insertIntoTestTable(conn, 1001, 999);
291: conn.rollback();
292:
293: shutdownDatabase(dbname);
294:
295: } catch (SQLException e) {
296: verifyShutdownError(e);
297: }
298:
299: conn = null;
300:
301: try {
302: conn = reopenConnectionToDatabase(dbname);
303: countRowInTestTable(conn);
304:
305: } finally {
306: if (conn != null) {
307: conn.close();
308: conn = null;
309: }
310: }
311:
312: }
313:
314: private static void testTwiceRollbacked() throws SQLException,
315: ClassNotFoundException, InstantiationException,
316: IllegalAccessException {
317:
318: final String dbname = "testTwiceRollbackedDB";
319: Connection conn = null;
320:
321: try {
322: conn = openConnectionToNewDatabase(dbname);
323: createTestTable(conn);
324:
325: conn.setAutoCommit(false);
326: insertIntoTestTable(conn, 1, 1000);
327: conn.rollback();
328: insertIntoTestTable(conn, 1001, 999);
329: conn.rollback();
330:
331: shutdownDatabase(dbname);
332:
333: } catch (SQLException e) {
334: verifyShutdownError(e);
335: }
336:
337: conn = null;
338:
339: try {
340: conn = reopenConnectionToDatabase(dbname);
341: countRowInTestTable(conn);
342:
343: } finally {
344: if (conn != null) {
345: conn.close();
346: conn = null;
347: }
348: }
349:
350: }
351:
352: private static void testOnlyTransactionWasLeft()
353: throws SQLException, ClassNotFoundException,
354: InstantiationException, IllegalAccessException {
355:
356: final String dbname = "testOnlyTransactionWasLeftDB";
357: Connection conn = null;
358:
359: try {
360: conn = openConnectionToNewDatabase(dbname);
361: createTestTable(conn);
362:
363: conn.setAutoCommit(false);
364: insertIntoTestTable(conn, 1, 1000);
365:
366: shutdownDatabase(dbname);
367:
368: } catch (SQLException e) {
369: verifyShutdownError(e);
370: }
371:
372: conn = null;
373:
374: try {
375: conn = reopenConnectionToDatabase(dbname);
376: countRowInTestTable(conn);
377:
378: } finally {
379: if (conn != null) {
380: conn.close();
381: conn = null;
382: }
383: }
384:
385: }
386:
387: private static void testOnceCommitedAndLeft() throws SQLException,
388: ClassNotFoundException, InstantiationException,
389: IllegalAccessException {
390:
391: final String dbname = "testOnceCommitedAndLeftDB";
392: Connection conn = null;
393:
394: try {
395: conn = openConnectionToNewDatabase(dbname);
396: createTestTable(conn);
397:
398: conn.setAutoCommit(false);
399: insertIntoTestTable(conn, 1, 1000);
400: conn.commit();
401: insertIntoTestTable(conn, 1001, 999);
402:
403: shutdownDatabase(dbname);
404:
405: } catch (SQLException e) {
406: verifyShutdownError(e);
407: }
408:
409: conn = null;
410:
411: try {
412: conn = reopenConnectionToDatabase(dbname);
413: countRowInTestTable(conn);
414:
415: } finally {
416: if (conn != null) {
417: conn.close();
418: conn = null;
419: }
420: }
421:
422: }
423:
424: private static void testOnceRollbackedAndLeft()
425: throws SQLException, ClassNotFoundException,
426: InstantiationException, IllegalAccessException {
427:
428: final String dbname = "testOnceRollbackedAndLeftDB";
429: Connection conn = null;
430:
431: try {
432: conn = openConnectionToNewDatabase(dbname);
433: createTestTable(conn);
434:
435: conn.setAutoCommit(false);
436: insertIntoTestTable(conn, 1, 1000);
437: conn.rollback();
438: insertIntoTestTable(conn, 1001, 999);
439:
440: shutdownDatabase(dbname);
441:
442: } catch (SQLException e) {
443: verifyShutdownError(e);
444: }
445:
446: conn = null;
447:
448: try {
449: conn = reopenConnectionToDatabase(dbname);
450: countRowInTestTable(conn);
451:
452: } finally {
453: if (conn != null) {
454: conn.close();
455: conn = null;
456: }
457: }
458:
459: }
460:
461: private static Connection openConnectionToNewDatabase(
462: String databaseName)
463: // throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
464: throws SQLException {
465:
466: Connection conn = TestUtil.getConnection(databaseName,
467: "create=true");
468:
469: System.out.println("A connection to " + databaseName
470: + " was opened.");
471:
472: return conn;
473:
474: }
475:
476: private static Connection reopenConnectionToDatabase(
477: String databaseName)
478: // throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
479: throws SQLException {
480:
481: return TestUtil.getConnection(databaseName, null);
482:
483: }
484:
485: private static void shutdownDatabase(String databaseName)
486: throws SQLException, ClassNotFoundException,
487: InstantiationException, IllegalAccessException {
488:
489: TestUtil.getConnection(databaseName, "shutdown=true");
490:
491: System.out.println(databaseName + " was shut down.");
492:
493: }
494:
495: private static void createTestTable(Connection conn)
496: throws SQLException {
497:
498: Statement st = null;
499:
500: try {
501: st = conn.createStatement();
502: st.execute("create table " + "TEST_TABLE "
503: + "( TEST_COL integer )");
504:
505: } finally {
506: if (st != null) {
507: st.close();
508: st = null;
509: }
510: }
511: }
512:
513: private static void insertIntoTestTable(Connection conn, int val)
514: throws SQLException {
515:
516: PreparedStatement st = null;
517:
518: try {
519: st = conn.prepareStatement("insert into " + "TEST_TABLE "
520: + "( TEST_COL ) " + "values( ? )");
521: st.setInt(1, val);
522: st.execute();
523:
524: } finally {
525: if (st != null) {
526: st.close();
527: st = null;
528: }
529: }
530: }
531:
532: private static void insertIntoTestTable(Connection conn,
533: int initialval, int count) throws SQLException {
534:
535: for (int i = initialval; i < initialval + count; i++) {
536:
537: insertIntoTestTable(conn, i);
538:
539: }
540:
541: }
542:
543: private static void countRowInTestTable(Connection conn)
544: throws SQLException {
545:
546: Statement st = null;
547: ResultSet rs = null;
548:
549: try {
550: st = conn.createStatement();
551: rs = st.executeQuery("select " + "count(*) " + "from "
552: + "TEST_TABLE ");
553:
554: rs.next();
555: System.out.println(rs.getInt(1));
556:
557: } finally {
558:
559: if (rs != null) {
560: rs.close();
561: rs = null;
562: }
563:
564: if (st != null) {
565: st.close();
566: st = null;
567: }
568:
569: }
570: }
571:
572: private static void verifyShutdownError(SQLException e)
573: throws SQLException {
574:
575: if (!isShutdownError(e))
576: throw e;
577:
578: System.out.println("SQLException of shutting down was found.");
579:
580: }
581:
582: private static boolean isShutdownError(SQLException e) {
583: return e
584: .getSQLState()
585: .equals(
586: StandardException
587: .getSQLStateFromIdentifier(SQLState.SHUTDOWN_DATABASE));
588: }
589:
590: }
|