KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > tools > RunScriptThread


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.tools;
6
7 import java.sql.Connection JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.util.LinkedList JavaDoc;
10
11 import org.h2.message.Message;
12
13 class RunScriptThread extends Thread JavaDoc {
14     private int id;
15     private volatile boolean stop;
16     private Connection JavaDoc conn;
17     private LinkedList JavaDoc queue = new LinkedList JavaDoc();
18     
19     RunScriptThread(int id, Connection JavaDoc conn) {
20         this.id = id;
21         this.conn = conn;
22     }
23     
24     void stopThread() {
25         this.stop = true;
26     }
27     
28     void addStatement(String JavaDoc 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 JavaDoc e) {
44                     // ignore
45
}
46             }
47         }
48     }
49     
50     public void run() {
51         while(!stop) {
52             String JavaDoc sql;
53             synchronized(queue) {
54                 while(queue.size() == 0) {
55                     try {
56                         queue.wait();
57                     } catch (InterruptedException JavaDoc e) {
58                         // ignore
59
}
60                 }
61                 sql = (String JavaDoc) queue.removeFirst();
62                 queue.notifyAll();
63             }
64             if(sql == null) {
65                 continue;
66             }
67             try {
68                 conn.createStatement().execute("/*"+id+"*/" + sql);
69             } catch(SQLException JavaDoc 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