1 5 package org.h2.test.synth; 6 7 import java.sql.Connection ; 8 import java.sql.PreparedStatement ; 9 import java.sql.ResultSet ; 10 import java.sql.Statement ; 11 import java.util.Random ; 12 13 import org.h2.test.TestBase; 14 15 public class TestThreads extends TestBase implements Runnable { 16 17 public TestThreads() { 18 19 } 20 21 public void test() throws Exception { 22 deleteDb("threads"); 23 Connection conn = getConnection("threads;MAX_LOG_SIZE=1"); 24 Statement stat = conn.createStatement(); 25 stat.execute("CREATE TABLE TEST_A(ID INT PRIMARY KEY, NAME VARCHAR)"); 26 stat.execute("CREATE TABLE TEST_B(ID INT PRIMARY KEY, NAME VARCHAR)"); 27 stat.execute("CREATE TABLE TEST_C(ID INT PRIMARY KEY, NAME VARCHAR)"); 28 int len = 1000; 29 insertRows(conn, "TEST_A", len); 30 insertRows(conn, "TEST_B", len); 31 insertRows(conn, "TEST_C", len); 32 maxId = len; 33 int threadCount = 4; 34 Thread [] threads = new Thread [threadCount]; 35 for(int i=0; i<threadCount; i++) { 36 String table = random.nextBoolean() ? null : getRandomTable(); 37 int op = random.nextInt(OP_TYPES); 38 op = i % 2 == 1 ? RECONNECT : CHECKPOINT; 39 threads[i] = new Thread (new TestThreads(this, op, table)); 40 } 41 for(int i=0; i<threadCount; i++) { 42 threads[i].start(); 43 } 44 Thread.sleep(10000); 45 stop = true; 46 for(int i=0; i<threadCount; i++) { 47 threads[i].join(); 48 } 49 conn.close(); 50 conn = getConnection("threads"); 51 checkTable(conn, "TEST_A"); 52 checkTable(conn, "TEST_B"); 53 checkTable(conn, "TEST_C"); 54 conn.close(); 55 } 56 57 private void insertRows(Connection conn, String tableName, int len) throws Exception { 58 PreparedStatement prep = conn.prepareStatement("INSERT INTO " +tableName+" VALUES(?, 'Hi')"); 59 for(int i=0; i<len; i++) { 60 prep.setInt(1, i); 61 prep.execute(); 62 } 63 } 64 65 private void checkTable(Connection conn, String tableName) throws Exception { 66 Statement stat = conn.createStatement(); 67 ResultSet rs = stat.executeQuery("SELECT * FROM "+tableName+" ORDER BY ID"); 68 while(rs.next()) { 69 int id = rs.getInt(1); 70 String name = rs.getString(2); 71 System.out.println("id="+id+" name="+name); 72 } 73 } 74 75 private int maxId = 1; 76 77 private volatile boolean stop; 78 private TestThreads master; 79 private int type; 80 private String table; 81 private Random random = new Random (); 82 83 private static final int INSERT=0, UPDATE=1, DELETE=2; 84 private static final int SELECT_ONE=3, SELECT_ALL=4, CHECKPOINT=5, RECONNECT=6; 85 private static final int OP_TYPES = RECONNECT+1; 86 87 private int getMaxId() { 88 return maxId; 89 } 90 91 private synchronized int incrementMaxId() { 92 return maxId++; 93 } 94 95 TestThreads(TestThreads master, int type, String table) { 96 this.master = master; 97 this.type = type; 98 this.table = table; 99 } 100 101 private String getRandomTable() { 102 return "TEST_" + (char)('A' + random.nextInt(3)); 103 } 104 105 public void run() { 106 try { 107 String t = table == null ? getRandomTable() : table; 108 Connection conn = master.getConnection("threads"); 109 Statement stat = conn.createStatement(); 110 ResultSet rs; 111 int max = master.getMaxId(); 112 int rid = random.nextInt(max); 113 for(int i=0; !master.stop; i++) { 114 switch(type) { 115 case INSERT: 116 max = master.incrementMaxId(); 117 stat.execute("INSERT INTO "+t+"(ID, NAME) VALUES("+max+", 'Hello')"); 118 break; 119 case UPDATE: 120 stat.execute("UPDATE "+t+" SET NAME='World "+rid+"' WHERE ID="+rid); 121 break; 122 case DELETE: 123 stat.execute("DELETE FROM "+t+" WHERE ID="+rid); 124 break; 125 case SELECT_ALL: 126 rs = stat.executeQuery("SELECT * FROM "+t+" ORDER BY ID"); 127 while(rs.next()) { 128 } 130 break; 131 case SELECT_ONE: 132 rs = stat.executeQuery("SELECT * FROM "+t+" WHERE ID=" + rid); 133 while(rs.next()) { 134 } 136 break; 137 case CHECKPOINT: 138 stat.execute("CHECKPOINT"); 139 break; 140 case RECONNECT: 141 conn.close(); 142 conn = master.getConnection("threads"); 143 break; 144 } 145 } 146 conn.close(); 147 } catch(Exception e) { 148 TestBase.logError("error", e); 149 } 150 } 151 152 } 153 | Popular Tags |