Revision: 6605
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6605&view=rev
Author: gerdwagner
Date: 2012-03-22 18:46:21 +0000 (Thu, 22 Mar 2012)
Log Message:
-----------
Bug fix: Connection/Login timeouts (menu File --> Global Preferences --> Tab SQL) as well as canceling the "Connect to" dialog now works cleanly.
Modified Paths:
--------------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/db/ConnectToAliasCallBack.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/db/ICompletionCallback.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/ConnectToAliasCommand.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/OpenConnectionCommand.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/Session.java
trunk/sql12/doc/src/main/resources/changes.txt
Added Paths:
-----------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/OpenConnectionCommandListener.java
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/db/ConnectToAliasCallBack.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/db/ConnectToAliasCallBack.java 2012-03-20 18:52:53 UTC (rev 6604)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/db/ConnectToAliasCallBack.java 2012-03-22 18:46:21 UTC (rev 6605)
@@ -1,9 +1,7 @@
package net.sourceforge.squirrel_sql.client.gui.db;
import net.sourceforge.squirrel_sql.client.IApplication;
-import net.sourceforge.squirrel_sql.client.mainframe.action.ConnectToAliasCommand;
import net.sourceforge.squirrel_sql.client.session.ISession;
-import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
import net.sourceforge.squirrel_sql.fw.sql.WrappedSQLException;
import net.sourceforge.squirrel_sql.fw.gui.ErrorDialog;
@@ -61,7 +59,7 @@
/**
* @see CompletionCallback#errorOccured(Throwable)
*/
- public void errorOccured(Throwable th)
+ public void errorOccured(Throwable th, boolean connectingHasBeenCanceledByUser)
{
if (th instanceof WrappedSQLException)
{
@@ -76,25 +74,41 @@
msg = s_stringMgr.getString("ConnectToAliasCommand.error.cantopen");
}
msg = _sqlAlias.getName() + ": " + msg;
- showErrorDialog(msg, th);
+ if (false == connectingHasBeenCanceledByUser)
+ {
+ showErrorDialog(msg, th);
+ }
+ else
+ {
+ s_log.error(msg, th);
+ }
}
else if (th instanceof ClassNotFoundException)
{
String msg = s_stringMgr.getString("ConnectToAliasCommand.error.driver", _sqlAlias.getName());
- showErrorDialog(msg, th);
+ if (false == connectingHasBeenCanceledByUser)
+ {
+ showErrorDialog(msg, th);
+ }
}
else if (th instanceof NoClassDefFoundError)
{
String msg = s_stringMgr.getString("ConnectToAliasCommand.error.driver", _sqlAlias.getName());
s_log.error(msg, th);
- showErrorDialog(msg, th);
+ if (false == connectingHasBeenCanceledByUser)
+ {
+ showErrorDialog(msg, th);
+ }
}
else
{
String msg = s_stringMgr.getString("ConnectToAliasCommand.error.unexpected", _sqlAlias.getName());
s_log.debug(th.getClass().getName());
s_log.error(msg, th);
- showErrorDialog(msg, th);
+ if (false == connectingHasBeenCanceledByUser)
+ {
+ showErrorDialog(msg, th);
+ }
}
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/db/ICompletionCallback.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/db/ICompletionCallback.java 2012-03-20 18:52:53 UTC (rev 6604)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/gui/db/ICompletionCallback.java 2012-03-22 18:46:21 UTC (rev 6605)
@@ -7,5 +7,5 @@
{
void connected(ISQLConnection conn);
void sessionCreated(ISession session);
- void errorOccured(Throwable th);
+ void errorOccured(Throwable th, boolean stopConnection);
}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/ConnectToAliasCommand.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/ConnectToAliasCommand.java 2012-03-20 18:52:53 UTC (rev 6604)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/ConnectToAliasCommand.java 2012-03-22 18:46:21 UTC (rev 6605)
@@ -152,7 +152,7 @@
/**
* Handler used for connection internal frame actions.
*/
- private static class SheetHandler implements ConnectionInternalFrame.IHandler, Runnable
+ private static class SheetHandler implements ConnectionInternalFrame.IHandler
{
/** The connection internal frame. */
private ConnectionInternalFrame _connSheet;
@@ -232,8 +232,7 @@
_user = user;
_password = password;
_props = props;
- //_app.getThreadPool().addTask(this);
- run();
+ doConnect();
}
/**
@@ -266,20 +265,44 @@
* Execute task. Connect to the alias with the information entered
* in the connection internal frame.
*/
- public void run()
- {
- SQLConnection conn = null;
- final IIdentifier driverID = _alias.getDriverIdentifier();
- final ISQLDriver sqlDriver = _app.getDataCache().getDriver(driverID);
+ public void doConnect()
+ {
+ final IIdentifier driverID = _alias.getDriverIdentifier();
+ final ISQLDriver sqlDriver = _app.getDataCache().getDriver(driverID);
- try
- {
- OpenConnectionCommand cmd = new OpenConnectionCommand(_app,
- _alias, _user, _password, _props);
- cmd.execute();
+ try
+ {
+ final OpenConnectionCommand cmd = new OpenConnectionCommand(_app, _alias, _user, _password, _props);
- if(_alias.isAutoLogon())
+ cmd.execute(new OpenConnectionCommandListener(){
+ @Override
+ public void openConnectionFinished(Throwable t)
+ {
+ afterExecuteFinished(sqlDriver, cmd, t);
+ }
+ });
+
+ }
+ catch (Throwable ex)
+ {
+ _connSheet.executed(false);
+ _callback.errorOccured(ex, _stopConnection);
+ }
+ }
+
+ private void afterExecuteFinished(ISQLDriver sqlDriver, OpenConnectionCommand cmd, Throwable t)
+ {
+ try
+ {
+ if(null != t)
{
+ throw t;
+ }
+
+
+ SQLConnection conn;
+ if (_alias.isAutoLogon())
+ {
// If the user checked Auto Logon but gave wrong username/password
// in the Alias definition. He will be asked to enter username/password again in an extra dialog.
// Here for convenience we transfer these data back into the Alias.
@@ -290,39 +313,36 @@
conn = cmd.getSQLConnection();
- synchronized (this)
- {
- if (_stopConnection)
- {
- if (conn != null)
- {
- closeConnection(conn);
- conn = null;
- }
- }
- else
- {
- // After this it can't be stopped anymore!
- _callback.connected(conn);
- if (_createSession)
- {
- createSession(sqlDriver, conn);
- }
- else
- {
- _connSheet.executed(true);
- }
- }
- }
- }
- catch (Throwable ex)
- {
- _connSheet.executed(false);
- _callback.errorOccured(ex);
- }
- }
+ if (_stopConnection)
+ {
+ if (conn != null)
+ {
+ closeConnection(conn);
+ conn = null;
+ }
+ }
+ else
+ {
+ // After this it can't be stopped anymore!
+ _callback.connected(conn);
+ if (_createSession)
+ {
+ createSession(sqlDriver, conn);
+ }
+ else
+ {
+ _connSheet.executed(true);
+ }
+ }
+ }
+ catch (Throwable th)
+ {
+ _connSheet.executed(false);
+ _callback.errorOccured(th, _stopConnection);
+ }
+ }
- private void closeConnection(ISQLConnection conn)
+ private void closeConnection(ISQLConnection conn)
{
if (conn != null)
{
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/OpenConnectionCommand.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/OpenConnectionCommand.java 2012-03-20 18:52:53 UTC (rev 6604)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/OpenConnectionCommand.java 2012-03-22 18:46:21 UTC (rev 6605)
@@ -18,7 +18,9 @@
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+import java.sql.DriverManager;
import java.sql.SQLException;
+import java.util.concurrent.*;
import net.sourceforge.squirrel_sql.fw.id.IIdentifier;
import net.sourceforge.squirrel_sql.fw.sql.ISQLAlias;
@@ -31,13 +33,16 @@
import net.sourceforge.squirrel_sql.fw.util.ICommand;
import net.sourceforge.squirrel_sql.client.IApplication;
+
+import javax.swing.*;
+
/**
* This <CODE>ICommand</CODE> allows the user to connect to
* an <TT>ISQLAlias</TT>.
*
* @author <A HREF="mailto:co...@us...">Colin Bell</A>
*/
-public class OpenConnectionCommand implements ICommand
+public class OpenConnectionCommand
{
/** Application API. */
private IApplication _app;
@@ -85,28 +90,93 @@
/**
* Display connection internal frame.
- */
- public void execute() throws BaseException
+ * @param openConnectionCommandListener
+ */
+ public void execute(final OpenConnectionCommandListener openConnectionCommandListener) throws BaseException
{
- _conn = null;
- final IIdentifier driverID = _sqlAlias.getDriverIdentifier();
- final ISQLDriver sqlDriver = _app.getDataCache().getDriver(driverID);
- final SQLDriverManager mgr = _app.getSQLDriverManager();
- try
- {
- _conn = mgr.getConnection(sqlDriver, _sqlAlias, _userName, _password, _props);
- }
- catch (SQLException ex)
- {
- throw new WrappedSQLException(ex);
- }
- catch (Throwable th)
- {
- throw new BaseException(th);
- }
+ ExecutorService executorService = Executors.newFixedThreadPool(2);
+
+ Runnable taskConnect = new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ executeConnect();
+ }
+ };
+
+ final Future future = executorService.submit(taskConnect);
+
+ Runnable taskAwaitConnect = new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ awaitConnection(future, openConnectionCommandListener);
+ }
+ };
+ executorService.submit(taskAwaitConnect);
}
- /**
+ public void executeAndWait()
+ {
+ executeConnect();
+ }
+
+ private void awaitConnection(Future future, final OpenConnectionCommandListener openConnectionCommandListener)
+ {
+ try
+ {
+ if (0 < DriverManager.getLoginTimeout())
+ {
+ future.get(DriverManager.getLoginTimeout(), TimeUnit.SECONDS);
+ }
+ else
+ {
+ future.get();
+ }
+
+ SwingUtilities.invokeLater(new Runnable()
+ {
+ public void run()
+ {
+ openConnectionCommandListener.openConnectionFinished(null);
+ }
+ });
+ }
+ catch (final Throwable t)
+ {
+ SwingUtilities.invokeLater(new Runnable()
+ {
+ public void run()
+ {
+ openConnectionCommandListener.openConnectionFinished(t);
+ }
+ });
+ }
+ }
+
+ private void executeConnect()
+ {
+ _conn = null;
+ final IIdentifier driverID = _sqlAlias.getDriverIdentifier();
+ final ISQLDriver sqlDriver = _app.getDataCache().getDriver(driverID);
+ final SQLDriverManager mgr = _app.getSQLDriverManager();
+ try
+ {
+ _conn = mgr.getConnection(sqlDriver, _sqlAlias, _userName, _password, _props);
+ }
+ catch (SQLException ex)
+ {
+ throw new RuntimeException(ex);
+ }
+ catch (Throwable th)
+ {
+ throw new RuntimeException(th);
+ }
+ }
+
+ /**
* Retrieve the newly opened connection.
*
* @return The <TT>SQLConnection</T>.
@@ -115,5 +185,4 @@
{
return _conn;
}
-
}
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/OpenConnectionCommandListener.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/OpenConnectionCommandListener.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/mainframe/action/OpenConnectionCommandListener.java 2012-03-22 18:46:21 UTC (rev 6605)
@@ -0,0 +1,12 @@
+package net.sourceforge.squirrel_sql.client.mainframe.action;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: gerd
+ * Date: 20.03.12
+ * Time: 22:43
+ */
+public interface OpenConnectionCommandListener
+{
+ void openConnectionFinished(Throwable t);
+}
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/Session.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/Session.java 2012-03-20 18:52:53 UTC (rev 6604)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/session/Session.java 2012-03-22 18:46:21 UTC (rev 6605)
@@ -50,6 +50,7 @@
import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
import net.sourceforge.squirrel_sql.client.gui.session.SessionPanel;
import net.sourceforge.squirrel_sql.client.mainframe.action.OpenConnectionCommand;
+import net.sourceforge.squirrel_sql.client.mainframe.action.OpenConnectionCommandListener;
import net.sourceforge.squirrel_sql.client.plugin.IPlugin;
import net.sourceforge.squirrel_sql.client.session.mainpanel.IMainPanelTab;
import net.sourceforge.squirrel_sql.client.session.parser.IParserEventsProcessor;
@@ -272,7 +273,11 @@
s_log.debug("Closing session: " + _id);
}
stopKeepAliveTaskIfNecessary();
- _conn.removePropertyChangeListener(_connLis);
+ if (null != _conn)
+ {
+ // _conn is null when session is closed after reconnect (ctrl t) failure.
+ _conn.removePropertyChangeListener(_connLis);
+ }
_connLis = null;
@@ -541,7 +546,7 @@
*/
public void reconnect()
{
- SQLConnectionState connState = new SQLConnectionState();
+ final SQLConnectionState connState = new SQLConnectionState();
if (_conn != null)
{
try
@@ -553,7 +558,7 @@
s_log.error("Unexpected SQLException", ex);
}
}
- OpenConnectionCommand cmd = new OpenConnectionCommand(_app, _alias,
+ final OpenConnectionCommand cmd = new OpenConnectionCommand(_app, _alias,
_user, _password, connState.getConnectionProperties());
try
{
@@ -569,7 +574,33 @@
}
try
{
- cmd.execute();
+ cmd.execute(new OpenConnectionCommandListener()
+ {
+ @Override
+ public void openConnectionFinished(Throwable t)
+ {
+ reconnectDone(connState, cmd, t);
+ }
+ });
+ }
+ catch (Throwable t)
+ {
+ final String msg = s_stringMgr.getString("Session.reconnError", _alias.getName());
+ _msgHandler.showErrorMessage(msg +"\n" + t.toString());
+ s_log.error(msg, t);
+ _app.getSessionManager().fireReconnectFailed(this);
+ }
+ }
+
+ private void reconnectDone(SQLConnectionState connState, OpenConnectionCommand cmd, Throwable t)
+ {
+ try
+ {
+ if(null != t)
+ {
+ throw t;
+ }
+
_conn = cmd.getSQLConnection();
if (connState != null)
{
@@ -581,11 +612,11 @@
_app.getSessionManager().fireReconnected(this);
startKeepAliveTaskIfNecessary();
}
- catch (Throwable t)
+ catch (Throwable th)
{
final String msg = s_stringMgr.getString("Session.reconnError", _alias.getName());
- _msgHandler.showErrorMessage(msg +"\n" + t.toString());
- s_log.error(msg, t);
+ _msgHandler.showErrorMessage(msg +"\n" + th.toString());
+ s_log.error(msg, th);
_app.getSessionManager().fireReconnectFailed(this);
}
}
@@ -1228,19 +1259,23 @@
/**
* @see net.sourceforge.squirrel_sql.client.session.ISession#createUnmanagedConnection()
*/
- public SQLConnection createUnmanagedConnection() {
- SQLConnectionState connState = new SQLConnectionState();
-
- OpenConnectionCommand cmd = new OpenConnectionCommand(_app, _alias,
- _user, _password, connState.getConnectionProperties());
- try {
- cmd.execute();
- } catch (BaseException e) {
- showErrorMessage(e);
- return null;
- }
-
- return cmd.getSQLConnection();
+ public SQLConnection createUnmanagedConnection()
+ {
+ SQLConnectionState connState = new SQLConnectionState();
+
+ OpenConnectionCommand cmd = new OpenConnectionCommand(_app, _alias,
+ _user, _password, connState.getConnectionProperties());
+ try
+ {
+ cmd.executeAndWait();
+ }
+ catch (Exception e)
+ {
+ showErrorMessage(e);
+ return null;
+ }
+
+ return cmd.getSQLConnection();
}
-
+
}
Modified: trunk/sql12/doc/src/main/resources/changes.txt
===================================================================
--- trunk/sql12/doc/src/main/resources/changes.txt 2012-03-20 18:52:53 UTC (rev 6604)
+++ trunk/sql12/doc/src/main/resources/changes.txt 2012-03-22 18:46:21 UTC (rev 6605)
@@ -61,6 +61,8 @@
Bug fixes:
+Connection/Login timeouts (menu File --> Global Preferences --> Tab SQL) as well as canceling the "Connect to" dialog now works cleanly.
+
When a Session tab is closed that is not the active tab
the Session menu used to be disabled and keyboard shortcuts didn't work anymore for the active Session.
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|