1 24 25 package com.mckoi.jfccontrols; 26 27 import java.util.ArrayList ; 28 import java.awt.*; 29 import java.sql.*; 30 import javax.swing.*; 31 32 66 67 public class QueryAgent { 68 69 72 private Connection connection = null; 73 74 77 private SwingBlockUtil block_util; 78 79 82 private QueryThread query_thread; 83 84 88 private char query_finished = 'f'; 89 90 93 private SQLException sql_exception; 94 95 98 private ResultSet result_set; 99 100 103 public QueryAgent(Connection connection) { 104 this.connection = connection; 105 block_util = new SwingBlockUtil(); 106 query_thread = new QueryThread(); 107 query_thread.start(); 108 } 109 110 113 public Connection connection() { 114 return connection; 115 } 116 117 130 public ResultSet executeQuery(Query query) 131 throws SQLException, InterruptedException { 132 133 if (!SwingUtilities.isEventDispatchThread()) { 134 throw new Error ("Not on the event dispatcher."); 135 } 136 else if (query_finished != 'f') { 137 throw new Error ("Can't nest queries."); 141 } 142 143 PreparedStatement statement = 145 connection.prepareStatement(query.getString()); 146 for (int i = 0; i < query.parameterCount(); ++i) { 147 statement.setObject(i + 1, query.getParameter(i)); 148 } 149 150 query_thread.addStatement(statement); 151 query_finished = 'n'; 152 block_util.block(); 154 155 if (query_finished == 'e') { 156 SQLException e = sql_exception; 157 sql_exception = null; 158 query_finished = 'f'; 159 throw e; 160 } 161 else if (query_finished == 'r') { 162 ResultSet rs = result_set; 163 result_set = null; 164 query_finished = 'f'; 165 return rs; 166 } 167 else if (query_finished == 'c') { 168 query_finished = 'f'; 169 throw new InterruptedException ("Query Cancelled"); 170 } 171 else { 172 char old_state = query_finished; 173 query_finished = 'f'; 174 throw new Error ("Unknown query state: " + old_state); 175 } 176 177 } 178 179 184 public void cancelQuery() { 185 SwingUtilities.invokeLater(new Runnable () { 186 public void run() { 187 if (query_finished != 'f') { 188 query_finished = 'c'; 189 block_util.unblock(); 190 } 191 } 192 }); 193 } 194 195 198 private EventQueue eventQueue() { 199 return Toolkit.getDefaultToolkit().getSystemEventQueue(); 200 } 201 202 205 private boolean isQueryDone() { 206 return query_finished != 'n'; 207 } 208 209 215 private void notifyException( 216 final PreparedStatement statement, final SQLException e) { 217 SwingUtilities.invokeLater(new Runnable () { 218 public void run() { 219 if (query_finished != 'f') { 220 sql_exception = e; 221 query_finished = 'e'; 222 block_util.unblock(); 223 } 224 } 225 }); 226 } 227 228 234 private void notifyComplete( 235 final PreparedStatement statement, final ResultSet result_set) { 236 SwingUtilities.invokeLater(new Runnable () { 237 public void run() { 238 if (query_finished != 'f') { 239 QueryAgent.this.result_set = result_set; 240 query_finished = 'r'; 241 block_util.unblock(); 242 } 243 } 244 }); 245 } 246 247 249 252 private class QueryThread extends Thread { 253 254 private ArrayList statements = new ArrayList (); 256 257 private QueryThread() { 258 super(); 259 setDaemon(true); 260 setName("Mckoi - Query Agent"); 261 } 262 263 266 private synchronized void addStatement(PreparedStatement statement) { 267 statements.add(statement); 268 notifyAll(); 269 } 270 271 public void run() { 272 while (true) { 273 try { 274 PreparedStatement to_exec = null; 275 synchronized (this) { 276 while (statements.size() == 0) { 277 wait(); 278 } 279 to_exec = (PreparedStatement) statements.remove(0); 280 } 281 try { 282 ResultSet result_set = to_exec.executeQuery(); 284 notifyComplete(to_exec, result_set); 285 } 286 catch (SQLException e) { 287 notifyException(to_exec, e); 288 } 289 } 290 catch (Throwable e) { 291 System.err.println("Exception during QueryThread: " + 292 e.getMessage()); 293 e.printStackTrace(System.err); 294 } 295 } 296 } 297 298 } 299 300 302 private static QueryAgent query_agent = null; 303 304 307 public static void initAgent(Connection connection) { 308 if (query_agent == null) { 309 query_agent = new QueryAgent(connection); 310 } 311 } 312 313 316 public static QueryAgent getDefaultAgent() { 317 return query_agent; 318 } 319 320 335 public static ResultSet execute(Query query) 336 throws SQLException, InterruptedException { 337 return getDefaultAgent().executeQuery(query); 338 } 339 340 347 public static void cancel() { 348 getDefaultAgent().cancelQuery(); 349 } 350 351 } 352
| Popular Tags
|