001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.db;
007:
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010: import java.sql.PreparedStatement;
011: import java.sql.ResultSet;
012: import java.sql.SQLException;
013: import java.sql.Statement;
014:
015: import org.h2.api.DatabaseEventListener;
016: import org.h2.test.TestBase;
017: import org.h2.tools.Restore;
018:
019: /**
020: * Tests opening and closing a database.
021: */
022: public class TestOpenClose extends TestBase implements
023: DatabaseEventListener {
024:
025: int nextId = 10;
026:
027: public static void main(String[] a) throws Exception {
028: new TestOpenClose().test();
029: }
030:
031: public void test() throws Exception {
032: testBackup(false);
033: testBackup(true);
034: testCase();
035: testReconnectFast();
036: }
037:
038: private void testBackup(boolean encrypt) throws Exception {
039: deleteDb(baseDir, "openClose");
040: String url;
041: if (encrypt) {
042: url = "jdbc:h2:" + baseDir + "/openClose;CIPHER=XTEA";
043: } else {
044: url = "jdbc:h2:" + baseDir + "/openClose";
045: }
046: org.h2.Driver.load();
047: Connection conn = DriverManager.getConnection(url, "sa",
048: "abc def");
049: Statement stat = conn.createStatement();
050: stat.execute("CREATE TABLE TEST(C CLOB)");
051: stat.execute("INSERT INTO TEST VALUES(SPACE(10000))");
052: stat.execute("BACKUP TO '" + baseDir + "/test.zip'");
053: conn.close();
054: deleteDb(baseDir, "openClose");
055: Restore.execute(baseDir + "/test.zip", baseDir, null, true);
056: conn = DriverManager.getConnection(url, "sa", "abc def");
057: stat = conn.createStatement();
058: ResultSet rs = stat.executeQuery("SELECT * FROM TEST");
059: rs.next();
060: check(rs.getString(1).length(), 10000);
061: checkFalse(rs.next());
062: conn.close();
063: }
064:
065: private void testReconnectFast() throws Exception {
066: deleteDb(baseDir, "openClose");
067: String url = "jdbc:h2:" + baseDir
068: + "/openClose;DATABASE_EVENT_LISTENER='"
069: + TestOpenClose.class.getName() + "'";
070: Connection conn = DriverManager.getConnection(url, "sa", "sa");
071: Statement stat = conn.createStatement();
072: try {
073: stat
074: .execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)");
075: stat.execute("SET MAX_MEMORY_UNDO 100000");
076: stat.execute("CREATE INDEX IDXNAME ON TEST(NAME)");
077: stat
078: .execute("INSERT INTO TEST SELECT X, X || ' Data' FROM SYSTEM_RANGE(1, 1000)");
079: } catch (SQLException e) {
080: // ok
081: }
082: stat.close();
083: conn.close();
084: conn = DriverManager.getConnection(url, "sa", "sa");
085: stat = conn.createStatement();
086: ResultSet rs = stat.executeQuery("SELECT * FROM DUAL");
087: if (rs.next()) {
088: rs.getString(1);
089: }
090: rs.close();
091: stat.close();
092: conn.close();
093: conn = DriverManager.getConnection(url, "sa", "sa");
094: stat = conn.createStatement();
095: // stat.execute("SET DB_CLOSE_DELAY 0");
096: stat.executeUpdate("SHUTDOWN");
097: stat.close();
098: conn.close();
099: }
100:
101: void testCase() throws Exception {
102: Class.forName("org.h2.Driver");
103: deleteDb(baseDir, "openClose");
104: final String url = "jdbc:h2:" + baseDir
105: + "/openClose;FILE_LOCK=NO";
106: Connection conn = DriverManager.getConnection(url, "sa", "");
107: conn.createStatement().execute("drop table employee if exists");
108: conn
109: .createStatement()
110: .execute(
111: "create table employee(id int primary key, name varchar, salary int)");
112: conn.close();
113: int len = this .getSize(200, 4000);
114: Thread[] threads = new Thread[len];
115: for (int i = 0; i < len; i++) {
116: threads[i] = new Thread() {
117: public void run() {
118: try {
119: Connection conn = DriverManager.getConnection(
120: url, "sa", "");
121: PreparedStatement prep = conn
122: .prepareStatement("insert into employee values(?, ?, 0)");
123: int id = getNextId();
124: prep.setInt(1, id);
125: prep.setString(2, "employee " + id);
126: prep.execute();
127: conn.close();
128: } catch (Throwable e) {
129: TestBase.logError("insert", e);
130: }
131: }
132: };
133: threads[i].start();
134: }
135: // for(int i=0; i<len; i++) {
136: // threads[i].start();
137: // }
138: for (int i = 0; i < len; i++) {
139: threads[i].join();
140: }
141: conn = DriverManager.getConnection(url, "sa", "");
142: ResultSet rs = conn.createStatement().executeQuery(
143: "select count(*) from employee");
144: rs.next();
145: check(rs.getInt(1), len);
146: conn.close();
147: }
148:
149: synchronized int getNextId() {
150: return nextId++;
151: }
152:
153: public void diskSpaceIsLow(long stillAvailable) throws SQLException {
154: throw new SQLException("unexpected");
155: }
156:
157: public void exceptionThrown(SQLException e, String sql) {
158: throw new Error("unexpected: " + e + " sql: " + sql);
159: }
160:
161: public void setProgress(int state, String name, int current, int max) {
162: String stateName;
163: switch (state) {
164: case STATE_SCAN_FILE:
165: stateName = "Scan " + name + " " + current + "/" + max;
166: if (current > 0) {
167: throw new Error("unexpected: " + stateName);
168: }
169: break;
170: case STATE_CREATE_INDEX:
171: stateName = "Create Index " + name + " " + current + "/"
172: + max;
173: if (!"SYS".equals(name)) {
174: throw new Error("unexpected: " + stateName);
175: }
176: break;
177: case STATE_RECOVER:
178: stateName = "Recover " + current + "/" + max;
179: break;
180: default:
181: stateName = "?";
182: }
183: // System.out.println(": " + stateName);
184: }
185:
186: public void closingDatabase() {
187: }
188:
189: public void init(String url) {
190: }
191:
192: public void opened() {
193: }
194:
195: }
|