001: package net.sourceforge.squirrel_sql.fw.sql;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.sql.SQLException;
022:
023: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
024: import net.sourceforge.squirrel_sql.fw.util.ISessionProperties;
025: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
026: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
027:
028: /**
029: * This class will save the state of an <TT>SQLConnection</TT> and
030: * can apply the saved state to another <TT>SQLConnection</TT>.
031: *
032: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
033: */
034: public class SQLConnectionState {
035: private final static ILogger s_log = LoggerController
036: .createLogger(SQLConnectionState.class);
037:
038: private Integer _transIsolation;
039: private String _catalog;
040: private boolean _autoCommit;
041: private SQLDriverPropertyCollection _connProps;
042:
043: public SQLConnectionState() {
044: super ();
045: }
046:
047: public void saveState(ISQLConnection conn,
048: ISessionProperties sessionProperties,
049: IMessageHandler msgHandler) throws SQLException {
050: if (conn == null) {
051: throw new IllegalArgumentException("SQLConnection == null");
052: }
053:
054: try {
055: _transIsolation = Integer.valueOf(conn
056: .getTransactionIsolation());
057: } catch (SQLException ex) {
058: String msg = "Error saving transaction isolation.\n"
059: + "This might happen when reconnecting a Session to restore a broken connection.\n"
060: + "The new connection will use the default transaction isolation.";
061:
062: s_log.error(msg, ex);
063: if (msgHandler == null) {
064: throw ex;
065: }
066: msgHandler.showErrorMessage(msg);
067: }
068:
069: try {
070: _catalog = conn.getCatalog();
071: } catch (SQLException ex) {
072: String msg = "Error saving current catalog.\n"
073: + "This might happen when reconnecting a Session to restore a broken connection.\n"
074: + "The new connection will use the default catalog.";
075:
076: s_log.error(msg, ex);
077: if (msgHandler == null) {
078: throw ex;
079: }
080: msgHandler.showErrorMessage(msg);
081: }
082:
083: try {
084: // In case the connection won't be able to tell its Auto Commit state,
085: // this is the best default we have.
086: _autoCommit = sessionProperties.getAutoCommit();
087:
088: _autoCommit = conn.getAutoCommit();
089: } catch (SQLException ex) {
090: String msg = "Error saving autocommit state.\n"
091: + "This might happen when reconnecting a Session to restore a broken connection.\n"
092: + "The new connection will use the autocommit state.";
093:
094: s_log.error(msg, ex);
095: if (msgHandler == null) {
096: throw ex;
097: }
098: msgHandler.showErrorMessage(msg);
099: }
100:
101: _connProps = conn.getConnectionProperties();
102: }
103:
104: public void restoreState(ISQLConnection conn) throws SQLException {
105: restoreState(conn, null);
106: }
107:
108: public void restoreState(ISQLConnection conn,
109: IMessageHandler msgHandler) throws SQLException {
110: if (conn == null) {
111: throw new IllegalArgumentException("SQLConnection == null");
112: }
113:
114: if (_transIsolation != null) {
115: try {
116: conn
117: .setTransactionIsolation(_transIsolation
118: .intValue());
119: } catch (SQLException ex) {
120: s_log
121: .error("Error restoring transaction isolation",
122: ex);
123: if (msgHandler == null) {
124: throw ex;
125: }
126: msgHandler.showErrorMessage(ex, null);
127: }
128: }
129:
130: if (_catalog != null) {
131: try {
132: conn.setCatalog(_catalog);
133: } catch (SQLException ex) {
134: s_log.error("Error restoring current catalog", ex);
135: if (msgHandler == null) {
136: throw ex;
137: }
138: msgHandler.showErrorMessage(ex, null);
139: }
140: }
141:
142: try {
143: conn.setAutoCommit(_autoCommit);
144: } catch (SQLException ex) {
145: s_log.error("Error restoring autocommit", ex);
146: if (msgHandler == null) {
147: throw ex;
148: }
149: msgHandler.showErrorMessage(ex, null);
150: }
151: }
152:
153: public SQLDriverPropertyCollection getConnectionProperties() {
154: return _connProps;
155: }
156:
157: public boolean getAutoCommit() {
158: return _autoCommit;
159: }
160: }
|