Update of /cvsroot/squirrel-sql/sql12/app/src/net/sourceforge/squirrel_sql/client/session
In directory sfp-cvsdas-3.v30.ch3.sourceforge.com:/tmp/cvs-serv1839/app/src/net/sourceforge/squirrel_sql/client/session
Modified Files:
Session.java
Added Files:
SessionConnectionKeepAlive.java
Log Message:
Aliases now have the ability to setup keep-alive SQL statement and sleep time (in seconds) to prevent a connection from being disconnected while appearing to be idle.
Index: Session.java
===================================================================
RCS file: /cvsroot/squirrel-sql/sql12/app/src/net/sourceforge/squirrel_sql/client/session/Session.java,v
retrieving revision 1.69
retrieving revision 1.70
diff -C2 -d -r1.69 -r1.70
*** Session.java 13 Jul 2009 23:24:53 -0000 1.69
--- Session.java 19 Dec 2009 21:33:51 -0000 1.70
***************
*** 44,47 ****
--- 44,48 ----
import net.sourceforge.squirrel_sql.client.gui.db.ISQLAliasExt;
import net.sourceforge.squirrel_sql.client.gui.db.SQLAlias;
+ import net.sourceforge.squirrel_sql.client.gui.db.SQLAliasConnectionProperties;
import net.sourceforge.squirrel_sql.client.gui.desktopcontainer.ISessionWidget;
import net.sourceforge.squirrel_sql.client.gui.session.ObjectTreeInternalFrame;
***************
*** 59,63 ****
import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
import net.sourceforge.squirrel_sql.fw.persist.ValidationException;
! import net.sourceforge.squirrel_sql.fw.sql.*;
import net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter;
import net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter;
--- 60,72 ----
import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
import net.sourceforge.squirrel_sql.fw.persist.ValidationException;
! import net.sourceforge.squirrel_sql.fw.sql.IQueryTokenizer;
! import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
! import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
! import net.sourceforge.squirrel_sql.fw.sql.ISQLDriver;
! import net.sourceforge.squirrel_sql.fw.sql.QueryTokenizer;
! import net.sourceforge.squirrel_sql.fw.sql.SQLConnection;
! import net.sourceforge.squirrel_sql.fw.sql.SQLConnectionState;
! import net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData;
! import net.sourceforge.squirrel_sql.fw.sql.TokenizerSessPropsInteractions;
import net.sourceforge.squirrel_sql.fw.util.DefaultExceptionFormatter;
import net.sourceforge.squirrel_sql.fw.util.ExceptionFormatter;
***************
*** 152,155 ****
--- 161,166 ----
private DefaultExceptionFormatter formatter = new DefaultExceptionFormatter();
+ private SessionConnectionKeepAlive _sessionConnectionKeepAlive = null;
+
/**
* Create a new session.
***************
*** 231,237 ****
}
});
!
}
/**
* Close this session.
--- 242,267 ----
}
});
! startKeepAliveTaskIfNecessary();
}
+ private void startKeepAliveTaskIfNecessary() {
+ SQLAliasConnectionProperties connProps = _alias.getConnectionProperties();
+
+ if (connProps.isEnableConnectionKeepAlive()) {
+ String keepAliveSql = connProps.getKeepAliveSqlStatement();
+ long sleepMillis = connProps.getKeepAliveSleepTimeSeconds() * 1000;
+ _sessionConnectionKeepAlive = new SessionConnectionKeepAlive(_conn, sleepMillis, keepAliveSql,
+ _alias.getName());
+ _app.getThreadPool().addTask(_sessionConnectionKeepAlive,
+ "Session Connection Keep-Alive (" + _alias.getName() + ")");
+ }
+ }
+
+ private void stopKeepAliveTaskIfNecessary() {
+ if (_sessionConnectionKeepAlive != null) {
+ _sessionConnectionKeepAlive.setStopped(true);
+ }
+ }
+
/**
* Close this session.
***************
*** 249,252 ****
--- 279,283 ----
s_log.debug("Closing session: " + _id);
}
+ stopKeepAliveTaskIfNecessary();
_conn.removePropertyChangeListener(_connLis);
_connLis = null;
***************
*** 477,480 ****
--- 508,512 ----
if (_conn != null)
{
+ stopKeepAliveTaskIfNecessary();
try
{
***************
*** 531,534 ****
--- 563,567 ----
_msgHandler.showMessage(msg);
_app.getSessionManager().fireReconnected(this);
+ startKeepAliveTaskIfNecessary();
}
catch (Throwable t)
--- NEW FILE: SessionConnectionKeepAlive.java ---
/*
* Copyright (C) 2009 Rob Manning
* man...@us...
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package net.sourceforge.squirrel_sql.client.session;
import java.sql.Statement;
import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
import net.sourceforge.squirrel_sql.fw.sql.SQLUtilities;
import net.sourceforge.squirrel_sql.fw.util.Utilities;
import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
/**
* This class will loop continuously, pausing for a configurable amount of time and executing a configurable
* SQL statement against a given SQLConnection.
*/
public class SessionConnectionKeepAlive implements Runnable
{
/** Logger for this class. */
private static final ILogger s_log = LoggerController.createLogger(SessionConnectionKeepAlive.class);
private final long sleepMillis;
private final ISQLConnection sqlConn;
private final String sql;
private volatile boolean isStopped = false;
private final String aliasName;
public SessionConnectionKeepAlive(ISQLConnection con, long sleepMillis, String sql, String aliasName)
{
if (sleepMillis < 1000) {
throw new IllegalArgumentException("Sleep time must be at least 1000ms(1 second)");
}
this.sleepMillis = sleepMillis;
Utilities.checkNull("SessionConnectionKeepAlive", "con", con, "sql", sql);
sqlConn = con;
this.sql = sql;
this.aliasName = aliasName;
}
public void setStopped(boolean isStopped)
{
this.isStopped = isStopped;
}
@Override
public void run()
{
while (!isStopped)
{
Statement stmt = null;
try
{
stmt = sqlConn.createStatement();
if (s_log.isInfoEnabled()) {
s_log.info("SessionConnectionKeepAlive ("+aliasName+") running SQL: "+sql);
}
stmt.executeQuery(sql);
}
catch (Throwable t)
{
s_log.error("run: unexpected exception while executing sql (" + sql + "): " + t.getMessage(), t);
}
finally
{
SQLUtilities.closeStatement(stmt);
}
// Always sleep at the end of the loop. In case we are stopped, we want to know that
// immediately before executing the sql statement.
Utilities.sleep(sleepMillis);
}
}
}
|