1 5 package org.h2.tools; 6 7 import java.sql.Connection ; 8 import java.sql.SQLException ; 9 import java.util.LinkedList ; 10 11 import org.h2.message.Message; 12 13 class RunScriptThread extends Thread { 14 private int id; 15 private volatile boolean stop; 16 private Connection conn; 17 private LinkedList queue = new LinkedList (); 18 19 RunScriptThread(int id, Connection conn) { 20 this.id = id; 21 this.conn = conn; 22 } 23 24 void stopThread() { 25 this.stop = true; 26 } 27 28 void addStatement(String sql) { 29 synchronized(queue) { 30 queue.add(sql); 31 queue.notifyAll(); 32 } 33 } 34 35 void executeAll() { 36 while(true) { 37 synchronized(queue) { 38 if(queue.size() == 0) { 39 return; 40 } 41 try { 42 queue.wait(); 43 } catch (InterruptedException e) { 44 } 46 } 47 } 48 } 49 50 public void run() { 51 while(!stop) { 52 String sql; 53 synchronized(queue) { 54 while(queue.size() == 0) { 55 try { 56 queue.wait(); 57 } catch (InterruptedException e) { 58 } 60 } 61 sql = (String ) queue.removeFirst(); 62 queue.notifyAll(); 63 } 64 if(sql == null) { 65 continue; 66 } 67 try { 68 conn.createStatement().execute("/*"+id+"*/" + sql); 69 } catch(SQLException e) { 70 switch(e.getErrorCode()) { 71 case Message.LOCK_TIMEOUT_1: 72 case Message.TABLE_OR_VIEW_ALREADY_EXISTS_1: 73 break; 74 default: 75 e.printStackTrace(); 76 } 77 } 78 } 79 } 80 } 81 | Popular Tags |