001: package net.sourceforge.squirrel_sql.client.session;
002:
003: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
004: import net.sourceforge.squirrel_sql.fw.util.StringManager;
005: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
006: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
007: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
008:
009: import java.sql.Statement;
010:
011: public class CancelStatementThread extends Thread {
012: private static final StringManager s_stringMgr = StringManagerFactory
013: .getStringManager(CancelStatementThread.class);
014:
015: private static final ILogger s_log = LoggerController
016: .createLogger(CancelStatementThread.class);
017:
018: private Statement _stmt;
019: private IMessageHandler _messageHandler;
020: private boolean _threadFinished;
021: private boolean _joinReturned;
022:
023: public CancelStatementThread(Statement stmt,
024: IMessageHandler messageHandler) {
025: _stmt = stmt;
026: _messageHandler = messageHandler;
027: }
028:
029: public void tryCancel() {
030: try {
031: start();
032: join(1500);
033:
034: synchronized (this ) {
035: _joinReturned = true;
036: if (false == _threadFinished) {
037: // i18n[CancelStatementThread.cancelTimedOut=Failed to cancel statement within one second. Perhaps your driver/database does not support cancelling statements. If cancelling succeeds later you'll get a further messages.]
038: String msg = s_stringMgr
039: .getString("CancelStatementThread.cancelTimedOut");
040: _messageHandler.showErrorMessage(msg);
041: s_log.error(msg);
042: }
043: }
044: } catch (InterruptedException e) {
045: throw new RuntimeException(e);
046: }
047: }
048:
049: public void run() {
050: String msg;
051:
052: boolean cancelSucceeded = false;
053: boolean closeSucceeded = false;
054:
055: try {
056: if (_stmt != null) {
057: _stmt.cancel();
058: }
059: cancelSucceeded = true;
060: } catch (Throwable t) {
061: // i18n[CancelStatementThread.cancelFailed=Failed to cancel statement. Perhaps the driver/RDDBMS does not support cancelling statements. See logs for further details ({0})]
062: msg = s_stringMgr.getString(
063: "CancelStatementThread.cancelFailed", t);
064: _messageHandler.showErrorMessage(msg);
065: s_log.error(msg, t);
066: }
067:
068: try {
069: // give the ResultSetReader some time to realize that the user requested
070: // cancel and stop fetching results. This allows us to stop the query
071: // processing gracefully.
072: Thread.sleep(500);
073: if (_stmt != null) {
074: _stmt.close();
075: }
076: closeSucceeded = true;
077: } catch (Throwable t) {
078: // i18n[CancelStatementThread.closeFailed=Failed to close statement. Propably the driver/RDDBMS does not support canceling statements. See logs for further details ({0})]
079: msg = s_stringMgr.getString(
080: "CancelStatementThread.closeFailed", t);
081: _messageHandler.showErrorMessage(msg);
082: s_log.error(msg, t);
083: }
084:
085: synchronized (this ) {
086:
087: if (cancelSucceeded && closeSucceeded) {
088: if (_joinReturned) {
089: // i18n[CancelStatementThread.cancelSucceededLate=Canceling statement succeeded now. But took longer than one second.]
090: msg = s_stringMgr
091: .getString("CancelStatementThread.cancelSucceededLate");
092: _messageHandler.showMessage(msg);
093: } else {
094: // i18n[CancelStatementThread.cancelSucceeded=The database has been asked to cancel the statment.]
095: msg = s_stringMgr
096: .getString("CancelStatementThread.cancelSucceeded");
097: _messageHandler.showMessage(msg);
098: }
099: }
100:
101: _threadFinished = true;
102: }
103:
104: }
105: }
|