1 5 package org.h2.test.db; 6 7 import java.sql.*; 8 9 import org.h2.api.DatabaseEventListener; 10 import org.h2.test.TestBase; 11 12 public class TestOpenClose extends TestBase implements DatabaseEventListener { 13 14 int nextId = 10; 15 16 public static void main(String [] a) throws Exception { 17 new TestOpenClose().test(); 18 } 19 20 public void test() throws Exception { 21 testCase(); 22 testReconnectFast(); 23 } 24 25 private void testReconnectFast() throws Exception { 26 deleteDb(BASE_DIR, "openClose"); 27 String url = "jdbc:h2:"+BASE_DIR+"/openClose;DATABASE_EVENT_LISTENER='" + TestOpenClose.class.getName()+"'"; 28 Connection conn = DriverManager.getConnection(url, "sa", "sa"); 29 Statement stat = conn.createStatement(); 30 try { 31 stat.execute("CREATE TABLE TEST(ID IDENTITY, NAME VARCHAR)"); 32 stat.execute("SET MAX_MEMORY_UNDO 100000"); 33 stat.execute("CREATE INDEX IDXNAME ON TEST(NAME)"); 34 stat.execute("INSERT INTO TEST SELECT X, X || ' Data' FROM SYSTEM_RANGE(1, 1000)"); 35 } catch (SQLException e) { 36 } 38 stat.close(); 39 conn.close(); 40 conn = DriverManager.getConnection(url, "sa", "sa"); 41 stat = conn.createStatement(); 42 ResultSet rs = stat.executeQuery("SELECT * FROM DUAL"); 43 if (rs.next()) { 44 rs.getString(1); 45 } 46 rs.close(); 47 stat.close(); 48 conn.close(); 49 conn = DriverManager.getConnection(url, "sa", "sa"); 50 stat = conn.createStatement(); 51 stat.executeUpdate("SHUTDOWN"); 53 stat.close(); 54 conn.close(); 55 } 56 57 void testCase() throws Exception { 58 Class.forName("org.h2.Driver"); 59 deleteDb(BASE_DIR, "openClose"); 60 final String url = "jdbc:h2:"+BASE_DIR+"/openClose;FILE_LOCK=NO"; 61 Connection conn = DriverManager.getConnection(url, "sa", ""); 62 conn.createStatement().execute("drop table employee if exists"); 63 conn.createStatement().execute("create table employee(id int primary key, name varchar, salary int)"); 64 conn.close(); 65 int len = this.getSize(200, 4000); 66 Thread [] threads = new Thread [len]; 67 for(int i=0; i<len; i++) { 68 threads[i] = new Thread () { 69 public void run() { 70 try { 71 Connection conn = DriverManager.getConnection(url, "sa", ""); 72 PreparedStatement prep = conn.prepareStatement("insert into employee values(?, ?, 0)"); 73 int id = getNextId(); 74 prep.setInt(1, id); 75 prep.setString(2, "emp " + id); 76 prep.execute(); 77 conn.close(); 78 } catch(Throwable e) { 79 TestBase.logError("insert", e); 80 } 81 } 82 }; 83 threads[i].start(); 84 } 85 for(int i=0; i<len; i++) { 89 threads[i].join(); 90 } 91 conn = DriverManager.getConnection(url, "sa", ""); 92 ResultSet rs = conn.createStatement().executeQuery("select count(*) from employee"); 93 rs.next(); 94 check(rs.getInt(1), len); 95 conn.close(); 96 } 97 98 synchronized int getNextId() { 99 return nextId++; 100 } 101 102 public void diskSpaceIsLow(long stillAvailable) throws SQLException { 103 throw new SQLException("unexpected"); 104 } 105 106 public void exceptionThrown(SQLException e) { 107 throw new Error ("unexpected: " + e); 108 } 109 110 public void setProgress(int state, String name, int current, int max) { 111 String stateName; 112 switch(state) { 113 case STATE_SCAN_FILE: 114 stateName = "Scan " + name + " " + current + "/" + max; 115 if(current > 0) { 116 throw new Error ("unexpected: " + stateName); 117 } 118 break; 119 case STATE_CREATE_INDEX: 120 stateName = "Create Index " + name + " " + current + "/" + max; 121 if(!"SYS".equals(name)) { 122 throw new Error ("unexpected: " + stateName); 123 } 124 break; 125 case STATE_RECOVER: 126 stateName = "Recover " + current + "/" + max; 127 break; 128 default: 129 stateName = "?"; 130 } 131 } 133 134 public void closingDatabase() { 135 } 136 137 public void init(String url) { 138 } 139 140 } 141 | Popular Tags |