001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.dbscript;
007:
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import java.sql.Connection;
012: import java.sql.SQLException;
013: import java.sql.Statement;
014:
015: /**
016: * A thread that can run a single command many times.
017: *
018: * @version $Revision: 1.7 $, $Date: 2006/01/18 14:40:05 $
019: * @author Bill Horsman (bill@logicalcobwebs.co.uk)
020: * @author $Author: billhorsman $ (current maintainer)
021: * @since Proxool 0.5
022: */
023: class Commander implements Runnable {
024:
025: private static final Log LOG = LogFactory.getLog(Commander.class);
026:
027: private ConnectionAdapterIF adapter;
028:
029: private CommandIF command;
030:
031: private CommandFilterIF commandFilter;
032:
033: private boolean finished = false;
034:
035: public Commander(ConnectionAdapterIF adapter, CommandIF commandIF,
036: CommandFilterIF commandFilter) {
037: this .adapter = adapter;
038: this .command = commandIF;
039: this .commandFilter = commandFilter;
040: }
041:
042: public void run() {
043:
044: try {
045: boolean keepGoing = true;
046: for (int i = 0; i < command.getLoops(); i++) {
047:
048: Connection connection = null;
049:
050: try {
051: connection = adapter.getConnection();
052: boolean executeCommand = true;
053: if (commandFilter != null) {
054: executeCommand = commandFilter.beforeCommand(
055: connection, command);
056: }
057:
058: if (executeCommand) {
059: execute(connection, command.getSql());
060: }
061:
062: if (commandFilter != null) {
063: commandFilter.afterCommand(connection, command);
064: }
065:
066: } catch (SQLException e) {
067: if (commandFilter != null
068: && !commandFilter
069: .catchException(command, e)) {
070: keepGoing = false;
071: }
072: if (command.isIgnoreException()) {
073: // Silent
074: } else if (command.isLogException()) {
075: LOG.debug("Ignoring exception in "
076: + command.getName(), e);
077: } else {
078: LOG.error("Stopping command "
079: + command.getName(), e);
080: keepGoing = false;
081: }
082: } finally {
083: try {
084: adapter.closeConnection(connection);
085: } catch (SQLException e) {
086: LOG.error("Closing connection for "
087: + Thread.currentThread().getName(), e);
088: }
089: }
090:
091: if (!keepGoing) {
092: break;
093: }
094:
095: // Give other threads a chance. (Hoping this will increase the chances
096: // of some sort of conflict)
097: Thread.yield();
098: }
099: } finally {
100: finished = true;
101: }
102:
103: }
104:
105: /**
106: * Is the thread running
107: * @return true if it's finished, else false
108: */
109: protected boolean isFinished() {
110: return finished;
111: }
112:
113: /**
114: * Execute and SQL statement
115: * @param connection used to execute statement
116: * @param sql the SQL to perform
117: * @throws java.sql.SQLException if anything goes wrong
118: */
119: private static final void execute(Connection connection, String sql)
120: throws SQLException {
121: Statement statement = null;
122: try {
123: statement = connection.createStatement();
124: statement.execute(sql);
125: } finally {
126: if (statement != null) {
127: try {
128: if (statement != null) {
129: statement.close();
130: }
131: } catch (SQLException e) {
132: LOG.error("Couldn't close statement", e);
133: }
134: }
135: }
136: }
137:
138: }
139:
140: /*
141: Revision history:
142: $Log: Commander.java,v $
143: Revision 1.7 2006/01/18 14:40:05 billhorsman
144: Unbundled Jakarta's Commons Logging.
145:
146: Revision 1.6 2003/03/03 11:12:03 billhorsman
147: fixed licence
148:
149: Revision 1.5 2003/02/19 15:14:20 billhorsman
150: fixed copyright (copy and paste error,
151: not copyright change)
152:
153: Revision 1.4 2003/02/06 17:41:02 billhorsman
154: now uses imported logging
155:
156: Revision 1.3 2003/01/15 00:08:15 billhorsman
157: check for commandFilter existence to avoid unnecessary
158: log clutter
159:
160: Revision 1.2 2002/11/09 15:59:18 billhorsman
161: fix doc
162:
163: Revision 1.1 2002/11/09 14:45:07 billhorsman
164: now threaded and better exception handling
165:
166: */
|