1 5 package org.h2.test.db; 6 7 import java.sql.Connection ; 8 import java.sql.ResultSet ; 9 import java.sql.SQLException ; 10 import java.sql.Statement ; 11 12 import org.h2.api.DatabaseEventListener; 13 import org.h2.test.TestBase; 14 15 18 19 public class TestMultiConn extends TestBase implements DatabaseEventListener { 20 21 public void test() throws Exception { 22 testCommitRollback(); 23 testConcurrentOpen(); 24 testThreeThreads(); 25 } 26 27 private static int wait; 28 29 private void testThreeThreads() throws Exception { 30 deleteDb("multiConn"); 31 final Connection conn1 = getConnection("multiConn"); 32 final Connection conn2 = getConnection("multiConn"); 33 final Connection conn3 = getConnection("multiConn"); 34 conn1.setAutoCommit(false); 35 conn2.setAutoCommit(false); 36 conn3.setAutoCommit(false); 37 final Statement s1 = conn1.createStatement(); 38 final Statement s2 = conn2.createStatement(); 39 final Statement s3 = conn3.createStatement(); 40 s1.execute("CREATE TABLE TEST1(ID INT)"); 41 s2.execute("CREATE TABLE TEST2(ID INT)"); 42 s3.execute("CREATE TABLE TEST3(ID INT)"); 43 s1.execute("INSERT INTO TEST1 VALUES(1)"); 44 s2.execute("INSERT INTO TEST2 VALUES(2)"); 45 s3.execute("INSERT INTO TEST3 VALUES(3)"); 46 s1.execute("SET LOCK_TIMEOUT 1000"); 47 s2.execute("SET LOCK_TIMEOUT 1000"); 48 s3.execute("SET LOCK_TIMEOUT 1000"); 49 Thread t1 = new Thread (new Runnable () { 50 public void run() { 51 try { 52 s3.execute("INSERT INTO TEST2 VALUES(4)"); 53 conn3.commit(); 54 } catch(SQLException e) { 55 TestBase.logError("insert", e); 56 } 57 } 58 }); 59 t1.start(); 60 Thread.sleep(20); 61 Thread t2 = new Thread (new Runnable () { 62 public void run() { 63 try { 64 s2.execute("INSERT INTO TEST1 VALUES(5)"); 65 conn2.commit(); 66 } catch(SQLException e) { 67 TestBase.logError("insert", e); 68 } 69 } 70 }); 71 t2.start(); 72 Thread.sleep(20); 73 conn1.commit(); 74 t2.join(100); 75 t1.join(100); 76 ResultSet rs = s1.executeQuery("SELECT * FROM TEST1 ORDER BY ID"); 77 rs.next(); 78 check(rs.getInt(1), 1); 79 rs.next(); 80 check(rs.getInt(1), 5); 81 checkFalse(rs.next()); 82 conn1.close(); 83 conn2.close(); 84 conn3.close(); 85 } 86 87 private void testConcurrentOpen() throws Exception { 88 if(config.memory) { 89 return; 90 } 91 deleteDb("multiConn"); 92 Connection conn = getConnection("multiConn"); 93 conn.createStatement().execute("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR)"); 94 conn.createStatement().execute("INSERT INTO TEST VALUES(0, 'Hello'), (1, 'World')"); 95 conn.createStatement().execute("SHUTDOWN"); 96 conn.close(); 97 final String listener = getClass().getName(); 98 Runnable r = new Runnable () { 99 public void run() { 100 try { 101 Connection c1 = getConnection("multiConn;DATABASE_EVENT_LISTENER='"+listener+"';file_lock=socket"); 102 c1.close(); 103 } catch(Exception e) { 104 TestBase.logError("connect", e); 105 } 106 } 107 }; 108 Thread thread = new Thread (r); 109 thread.start(); 110 Thread.sleep(10); 111 Connection c2 = getConnection("multiConn;file_lock=socket"); 112 c2.close(); 113 thread.join(); 114 } 115 116 117 public void diskSpaceIsLow(long stillAvailable) throws SQLException { 118 } 119 120 public void exceptionThrown(SQLException e) { 121 } 122 123 public void setProgress(int state, String name, int x, int max) { 124 while(wait > 0) { 125 try { 126 Thread.sleep(wait); 127 wait = 0; 128 } catch (InterruptedException e) { 129 TestBase.logError("sleep", e); 130 } 131 } 132 } 133 134 public void closingDatabase() { 135 } 136 137 private void testCommitRollback() throws Exception { 138 deleteDb("multiConn"); 139 Connection c1 = getConnection("multiConn"); 140 Connection c2 = getConnection("multiConn"); 141 c1.setAutoCommit(false); 142 c2.setAutoCommit(false); 143 Statement s1 = c1.createStatement(); 144 s1.execute("DROP TABLE IF EXISTS MULTI_A"); 145 s1.execute("CREATE TABLE MULTI_A(ID INT, NAME VARCHAR(255))"); 146 s1.execute("INSERT INTO MULTI_A VALUES(0, '0-insert-A')"); 147 Statement s2 = c2.createStatement(); 148 s1.execute("DROP TABLE IF EXISTS MULTI_B"); 149 s1.execute("CREATE TABLE MULTI_B(ID INT, NAME VARCHAR(255))"); 150 s2.execute("INSERT INTO MULTI_B VALUES(0, '1-insert-B')"); 151 c1.commit(); 152 c2.rollback(); 153 s1.execute("INSERT INTO MULTI_A VALUES(1, '0-insert-C')"); 154 s2.execute("INSERT INTO MULTI_B VALUES(1, '1-insert-D')"); 155 c1.rollback(); 156 c2.commit(); 157 c1.close(); 158 c2.close(); 159 160 if(!config.memory) { 161 Connection conn = getConnection("multiConn"); 162 ResultSet rs; 163 rs = conn.createStatement().executeQuery("SELECT * FROM MULTI_A ORDER BY ID"); 164 rs.next(); 165 check(rs.getString("NAME"), "0-insert-A" ); 166 checkFalse(rs.next()); 167 rs = conn.createStatement().executeQuery("SELECT * FROM MULTI_B ORDER BY ID"); 168 rs.next(); 169 check(rs.getString("NAME"), "1-insert-D" ); 170 checkFalse(rs.next()); 171 conn.close(); 172 } 173 174 } 175 176 public void init(String url) { 177 } 178 179 } 180 | Popular Tags |