Revision: 6359
http://squirrel-sql.svn.sourceforge.net/squirrel-sql/?rev=6359&view=rev
Author: wis775
Date: 2011-08-16 21:27:16 +0000 (Tue, 16 Aug 2011)
Log Message:
-----------
In the case of running out of memory, we try to close all result tabs, hoping, to free some memory.
This may help the user to save his work and restart SQuirreL by a clean way.
At the moment, only OutOfMemoryErrors within a AWT-Event will be catched.
Modified Paths:
--------------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/Main.java
trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/I18NStrings.properties
trunk/sql12/doc/src/main/resources/changes.txt
Added Paths:
-----------
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/IOutOfMemoryErrorHandler.java
trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandler.java
trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandlerTest.java
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/IOutOfMemoryErrorHandler.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/IOutOfMemoryErrorHandler.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/IOutOfMemoryErrorHandler.java 2011-08-16 21:27:16 UTC (rev 6359)
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2011 Stefan Willinger
+ * wi...@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;
+
+/**
+ * Interface of a handler, that should try to free some memory in the case of a {@link OutOfMemoryError}.
+ * @author Stefan Willinger
+ *
+ */
+public interface IOutOfMemoryErrorHandler {
+
+
+ /**
+ * To free some memory.
+ * The way this is done, depends on the implementation.
+ */
+ void handleOutOfMemoryError();
+}
Property changes on: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/IOutOfMemoryErrorHandler.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/Main.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/Main.java 2011-08-16 20:18:43 UTC (rev 6358)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/Main.java 2011-08-16 21:27:16 UTC (rev 6359)
@@ -1,5 +1,6 @@
package net.sourceforge.squirrel_sql.client;
+import net.sourceforge.squirrel_sql.client.session.ISession;
import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
import net.sourceforge.squirrel_sql.fw.util.log.SystemOutToLog;
@@ -36,6 +37,8 @@
/** Logger for this class. */
private static ILogger s_log;
+ private static Application application;
+
/**
* Default ctor. private as class should never be instantiated.
*/
@@ -86,12 +89,18 @@
EventQueue q = Toolkit.getDefaultToolkit().getSystemEventQueue();
q.push(new EventQueue()
{
+ OutOfMemoryErrorHandler oumErrorHandler = new OutOfMemoryErrorHandler();
protected void dispatchEvent(AWTEvent event)
{
try
{
super.dispatchEvent(event);
}
+ catch (OutOfMemoryError e) {
+ // We have to set the application by a lazy way, because it is created in a runnable.
+ oumErrorHandler.setApplication(application);
+ oumErrorHandler.handleOutOfMemoryError();
+ }
catch (Throwable t)
{
if (s_log.isDebugEnabled())
@@ -106,9 +115,12 @@
Runnable runnable = new Runnable()
{
- public void run()
+
+
+ public void run()
{
- new Application().startup();
+ application = new Application();
+ application.startup();
}
};
Added: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandler.java
===================================================================
--- trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandler.java (rev 0)
+++ trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandler.java 2011-08-16 21:27:16 UTC (rev 6359)
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2011 Stefan Willinger
+ * wi...@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;
+
+import net.sourceforge.squirrel_sql.client.session.ISession;
+import net.sourceforge.squirrel_sql.client.session.SessionManager;
+import net.sourceforge.squirrel_sql.fw.util.StringManager;
+import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
+import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
+import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
+
+/**
+ * Handler, that tries to free some memory. Mostly, if a
+ * {@link OutOfMemoryError} occurs,the user not be able to save his work,
+ * because the GUI is no longer responsible. To avoid this, we try to free some
+ * memory by closing all SQL result tabs. Under normal conditions, the SQL
+ * result tabs are components, which uses the most memory. So we have a high
+ * chance to free enough memory to keep the GUI responsible, that the user can
+ * save his work and restart SQuirrel. The scope is not, to protect the user
+ * from getting a {@link OutOfMemoryError}.
+ *
+ * @author Stefan Willinger
+ *
+ */
+public class OutOfMemoryErrorHandler implements IOutOfMemoryErrorHandler{
+
+ private static final ILogger log = LoggerController.createLogger(OutOfMemoryErrorHandler.class);
+
+ private static final StringManager stringMgr = StringManagerFactory
+ .getStringManager(OutOfMemoryErrorHandler.class);
+
+ interface i18n {
+ String message = stringMgr.getString("OutOfMemoryErrorHandler.message");
+ }
+
+
+ /**
+ * The application
+ */
+ private IApplication application;
+
+ /**
+ * Default Constructor. You have to set {@link #application} manually.
+ */
+ public OutOfMemoryErrorHandler() {
+ super();
+ }
+
+ /**
+ * Constructor setting the {@link #application}
+ *
+ * @param application
+ */
+ public OutOfMemoryErrorHandler(IApplication application) {
+ super();
+ setApplication(application);
+ }
+
+
+ /**
+ * Gets the application
+ *
+ * @return the application
+ */
+ public IApplication getApplication() {
+ return application;
+ }
+
+ /**
+ * Sets the application.
+ *
+ * @param application
+ * the application to set
+ * @throws IllegalArgumentException
+ * if application is null;
+ */
+ public void setApplication(IApplication application) {
+ if (application == null) {
+ throw new IllegalArgumentException("application must not be null");
+ }
+ this.application = application;
+ }
+
+ /**
+ * To free some memory, close all SQL result-tabs of all current sessions.
+ * This may free some memory.
+ * @see net.sourceforge.squirrel_sql.client.IOutOfMemoryErrorHandler#handleOutOfMemoryError()
+ */
+ public synchronized void handleOutOfMemoryError() {
+
+ SessionManager sessionManager = application.getSessionManager();
+
+ // All sessions, the user has opened
+ ISession[] sessions = sessionManager.getConnectedSessions();
+
+ if (sessions.length != 0) {
+ for (ISession session : sessions) {
+ closeResultTabs(session);
+ }
+ showMessage(sessionManager);
+ } else {
+ log.info("A OutOfMemoryError occured, but there are no sessions connected - so we cann't free memory.");
+ }
+
+ }
+
+ /**
+ * Inform the user, that a {@link OutOfMemoryError} had occurred.
+ *
+ * @param sessionManager
+ * the sessionManager
+ */
+ private void showMessage(SessionManager sessionManager) {
+ ISession activeSession = sessionManager.getActiveSession();
+ if (activeSession != null) {
+ log.info(i18n.message);
+ activeSession.showErrorMessage(i18n.message);
+ application.showErrorDialog(i18n.message);
+ } else {
+ log.info("A OutOfMemoryError occured, but there are no active session!");
+ }
+ }
+
+ /**
+ * Close all result tabs of the session.
+ *
+ * @param session
+ * the session, where to close the result tabs.
+ */
+ private void closeResultTabs(ISession session) {
+ session.getSessionInternalFrame().getSQLPanelAPI().closeAllSQLResultTabs();
+ }
+
+}
Property changes on: trunk/sql12/app/src/main/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandler.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified: trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/I18NStrings.properties
===================================================================
--- trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/I18NStrings.properties 2011-08-16 20:18:43 UTC (rev 6358)
+++ trunk/sql12/app/src/main/resources/net/sourceforge/squirrel_sql/client/I18NStrings.properties 2011-08-16 21:27:16 UTC (rev 6359)
@@ -53,3 +53,4 @@
Version.copyright=Copyright (c) 2001-2011 \nColin Bell, Gerd Wagner, Rob Manning and others
Version.website=http://www.squirrelsql.org
+OutOfMemoryErrorHandler.message=Due running out of memory, we have closed all result tabs of all sessions. Please save your work and restart SQuirreL SQL.
Added: trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandlerTest.java
===================================================================
--- trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandlerTest.java (rev 0)
+++ trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandlerTest.java 2011-08-16 21:27:16 UTC (rev 6359)
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2011 Stefan Willinger
+ * wi...@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;
+
+import net.sourceforge.squirrel_sql.BaseSQuirreLJUnit4TestCase;
+import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
+import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
+import net.sourceforge.squirrel_sql.client.session.ISession;
+import net.sourceforge.squirrel_sql.client.session.SessionManager;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author Stefan Willinger
+ *
+ */
+public class OutOfMemoryErrorHandlerTest extends BaseSQuirreLJUnit4TestCase {
+
+ private OutOfMemoryErrorHandler classUnderTest = null;
+
+ private static final String expectedErrorMessage = OutOfMemoryErrorHandler.i18n.message;
+
+
+ IApplication mockApplication;
+ SessionManager mockSessionManager;
+ ISession mockSession;
+ ISQLPanelAPI mockSQLPanelApi;
+ SessionInternalFrame sessionInternalFrame;
+ /**
+ * @throws java.lang.Exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ mockSession = mockHelper.createMock(ISession.class);
+ mockApplication = mockHelper.createMock(IApplication.class);
+ sessionInternalFrame = mockHelper.createMock(SessionInternalFrame.class);
+ mockSQLPanelApi = mockHelper.createMock(ISQLPanelAPI.class);
+ mockSessionManager = mockHelper.createMock(SessionManager.class);
+
+
+ ISession[] connectedSessions = new ISession[]{mockSession};
+ EasyMock.expect(mockApplication.getSessionManager()).andStubReturn(mockSessionManager);
+ EasyMock.expect(mockSessionManager.getConnectedSessions()).andStubReturn(connectedSessions);
+ EasyMock.expect(mockSessionManager.getActiveSession()).andStubReturn(mockSession);
+ EasyMock.expect(mockSession.getSessionInternalFrame()).andStubReturn(sessionInternalFrame);
+ EasyMock.expect(sessionInternalFrame.getSQLPanelAPI()).andStubReturn(mockSQLPanelApi);
+
+ // Make sure, the Result-Tabs are closed
+ mockSQLPanelApi.closeAllSQLResultTabs();
+ EasyMock.expectLastCall();
+
+ // Make sure, we show the right message
+ mockSession.showErrorMessage(expectedErrorMessage);
+ EasyMock.expectLastCall();
+
+ // Make sure, we show a error dialog.
+ mockApplication.showErrorDialog(expectedErrorMessage);
+ EasyMock.expectLastCall();
+
+ }
+
+ /**
+ * Ensures, that the handler closes all ResultTabs of all sessions.
+ */
+ @Test
+ public void testHandlingOutOfMemory() {
+ classUnderTest = new OutOfMemoryErrorHandler(mockApplication);
+ mockHelper.replayAll();
+ classUnderTest.handleOutOfMemoryError();
+ }
+
+ /**
+ * Ensures, that the application must be set via the setter.
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testSetApplicationNotNull() {
+ classUnderTest = new OutOfMemoryErrorHandler();
+ classUnderTest.setApplication(null);
+ }
+
+ /**
+ * Ensures, that the application must be set via the constructor.
+ */
+ @Test(expected=IllegalArgumentException.class)
+ public void testApplicationNotNull() {
+ classUnderTest = new OutOfMemoryErrorHandler(null);
+ }
+
+}
Property changes on: trunk/sql12/app/src/test/java/net/sourceforge/squirrel_sql/client/OutOfMemoryErrorHandlerTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified: trunk/sql12/doc/src/main/resources/changes.txt
===================================================================
--- trunk/sql12/doc/src/main/resources/changes.txt 2011-08-16 20:18:43 UTC (rev 6358)
+++ trunk/sql12/doc/src/main/resources/changes.txt 2011-08-16 21:27:16 UTC (rev 6359)
@@ -7,6 +7,8 @@
Enhancements:
+In the case of running out of memory, we try to close all result tabs, hoping, to free some memory.
+ This may help the user to save his work and restart SQuirreL by a clean way.
SQL Scripts Plugin: Added the capability to "store the execution result of an SQL query into file". This
allows the user to store the result of the selected SQL directly into a file, instead of displaying it in
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|