01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.db;
07:
08: import java.sql.Connection;
09: import java.sql.SQLException;
10: import java.sql.Statement;
11:
12: import org.h2.test.TestBase;
13:
14: /**
15: * Test for the exclusive mode.
16: */
17: public class TestExclusive extends TestBase {
18:
19: public void test() throws Exception {
20: deleteDb("exclusive");
21: Connection conn = getConnection("exclusive");
22: Statement stat = conn.createStatement();
23: stat.execute("set exclusive true");
24: try {
25: Connection conn2 = getConnection("exclusive");
26: conn2.close();
27: error();
28: } catch (SQLException e) {
29: checkNotGeneralException(e);
30: }
31:
32: stat.execute("set exclusive false");
33: Connection conn2 = getConnection("exclusive");
34: final Statement stat2 = conn2.createStatement();
35: stat.execute("set exclusive true");
36: final int[] state = new int[1];
37: Thread t = new Thread() {
38: public void run() {
39: try {
40: stat2.execute("select * from dual");
41: if (state[0] != 1) {
42: new Error("unexpected state: " + state[0])
43: .printStackTrace();
44: }
45: state[0] = 2;
46: } catch (Exception e) {
47: e.printStackTrace();
48: }
49: }
50: };
51: t.start();
52: state[0] = 1;
53: stat.execute("set exclusive false");
54: for (int i = 0; i < 20; i++) {
55: Thread.sleep(100);
56: if (state[0] == 2) {
57: break;
58: }
59: }
60: check(state[0], 2);
61: stat.execute("set exclusive true");
62: conn.close();
63:
64: // check that exclusive mode is off when disconnected
65: stat2.execute("select * from dual");
66: conn2.close();
67:
68: }
69:
70: }
|