001: /*
002: * KeepAliveDaemon.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db;
013:
014: import java.sql.SQLException;
015: import java.sql.Statement;
016: import workbench.log.LogMgr;
017: import workbench.util.ExceptionUtil;
018: import workbench.util.SqlUtil;
019: import workbench.util.WbThread;
020:
021: /**
022: *
023: * @author support@sql-workbench.net
024: */
025: public class KeepAliveDaemon implements Runnable {
026: private long idleTime;
027: private WbThread idleThread;
028: private boolean stopThread;
029: private WbConnection dbConnection;
030: private String sqlScript;
031: private long lastAction;
032:
033: public KeepAliveDaemon(long idle, WbConnection con, String sql) {
034: this .idleTime = idle;
035: this .dbConnection = con;
036: this .sqlScript = sql;
037: }
038:
039: public void startThread() {
040: this .shutdown();
041: LogMgr.logInfo("KeepAliveDaemon.startThread()",
042: "Initializing keep alive every "
043: + getTimeDisplay(idleTime) + " with sql: "
044: + this .sqlScript);
045: this .idleThread = new WbThread(this , "KeepAlive/"
046: + this .dbConnection.getId());
047: this .lastAction = 0;
048: this .stopThread = false;
049: this .idleThread.start();
050: }
051:
052: public void shutdown() {
053: if (this .idleThread != null) {
054: try {
055: this .stopThread = true;
056: this .dbConnection = null;
057: this .idleThread.interrupt();
058: } catch (Exception e) {
059: LogMgr.logWarning("KeepAliveThread.shutdown()",
060: "Error when stopping thread", e);
061: }
062: }
063: }
064:
065: public synchronized void setLastDbAction(long millis) {
066: this .lastAction = millis;
067: }
068:
069: public void run() {
070: while (!stopThread) {
071: if (this .dbConnection == null) {
072: stopThread = true;
073: break;
074: }
075:
076: long now = System.currentTimeMillis();
077:
078: try {
079: long newSleep = idleTime - (now - lastAction);
080: if (newSleep <= 0) {
081: newSleep = idleTime;
082: }
083: LogMgr.logDebug("KeepAliveDaemon.run()", Thread
084: .currentThread().getName()
085: + ": sleeping for " + newSleep + "ms");
086: Thread.sleep(idleTime);
087: } catch (InterruptedException e) {
088: if (!this .stopThread) {
089: LogMgr.logError("KeepAliveThread.run()", Thread
090: .currentThread().getName()
091: + ": Thread was interrupted!", e);
092: }
093: }
094:
095: now = System.currentTimeMillis();
096:
097: synchronized (this ) {
098: if (((now - lastAction) > idleTime)) {
099: runSqlScript();
100: this .lastAction = now;
101: }
102: }
103: }
104: }
105:
106: public static String getTimeDisplay(long millis) {
107: if (millis == 0)
108: return "";
109:
110: if (millis < 60 * 1000) {
111: return Long.toString((millis / 1000)) + "s";
112: }
113: return Long.toString((millis / (60 * 1000))) + "m";
114: }
115:
116: private void runSqlScript() {
117: if (this .dbConnection == null)
118: return;
119: if (this .dbConnection.isBusy())
120: return;
121: if (this .dbConnection.isClosed())
122: return;
123:
124: Statement stmt = null;
125: synchronized (this .dbConnection) {
126: try {
127: stmt = this .dbConnection.createStatement();
128: LogMgr.logInfo("KeepAliveThread.runSqlScript()", Thread
129: .currentThread().getName()
130: + " - executing SQL: " + this .sqlScript);
131: stmt.execute(sqlScript);
132: } catch (SQLException sql) {
133: LogMgr
134: .logError(
135: "KeepAliveThread.runSqlScript()",
136: Thread.currentThread().getName()
137: + ": SQL Error when running keep alive script: "
138: + ExceptionUtil.getDisplay(sql),
139: null);
140: } catch (Throwable e) {
141: LogMgr.logError("KeepAliveThread.runSqlScript()",
142: "Error when running keep alive script", e);
143: } finally {
144: SqlUtil.closeStatement(stmt);
145: }
146: }
147: }
148: }
|