1 6 package org.logicalcobwebs.dbscript; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import java.sql.Connection ; 12 import java.sql.SQLException ; 13 import java.sql.Statement ; 14 15 23 class Commander implements Runnable { 24 25 private static final Log LOG = LogFactory.getLog(Commander.class); 26 27 private ConnectionAdapterIF adapter; 28 29 private CommandIF command; 30 31 private CommandFilterIF commandFilter; 32 33 private boolean finished = false; 34 35 public Commander(ConnectionAdapterIF adapter, CommandIF commandIF, CommandFilterIF commandFilter) { 36 this.adapter = adapter; 37 this.command = commandIF; 38 this.commandFilter = commandFilter; 39 } 40 41 public void run() { 42 43 try { 44 boolean keepGoing = true; 45 for (int i = 0; i < command.getLoops(); i++) { 46 47 Connection connection = null; 48 49 try { 50 connection = adapter.getConnection(); 51 boolean executeCommand = true; 52 if (commandFilter != null) { 53 executeCommand = commandFilter.beforeCommand(connection, command); 54 } 55 56 if (executeCommand) { 57 execute(connection, command.getSql()); 58 } 59 60 if (commandFilter != null) { 61 commandFilter.afterCommand(connection, command); 62 } 63 64 } catch (SQLException e) { 65 if (commandFilter != null && !commandFilter.catchException(command, e)) { 66 keepGoing = false; 67 } 68 if (command.isIgnoreException()) { 69 } else if (command.isLogException()) { 71 LOG.debug("Ignoring exception in " + command.getName(), e); 72 } else { 73 LOG.error("Stopping command " + command.getName(), e); 74 keepGoing = false; 75 } 76 } finally { 77 try { 78 adapter.closeConnection(connection); 79 } catch (SQLException e) { 80 LOG.error("Closing connection for " + Thread.currentThread().getName(), e); 81 } 82 } 83 84 if (!keepGoing) { 85 break; 86 } 87 88 Thread.yield(); 91 } 92 } finally { 93 finished = true; 94 } 95 96 } 97 98 102 protected boolean isFinished() { 103 return finished; 104 } 105 106 112 private static final void execute(Connection connection, String sql) throws SQLException { 113 Statement statement = null; 114 try { 115 statement = connection.createStatement(); 116 statement.execute(sql); 117 } finally { 118 if (statement != null) { 119 try { 120 if (statement != null) { 121 statement.close(); 122 } 123 } catch (SQLException e) { 124 LOG.error("Couldn't close statement", e); 125 } 126 } 127 } 128 } 129 130 } 131 132 159 | Popular Tags |