001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2006 Continuent, Inc.
004: * Contact: sequoia@continuent.org
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * Initial developer(s): Emmanuel Cecchet.
019: * Contributor(s): ______________________.
020: */package org.continuent.sequoia.controller.connection;
021:
022: import java.sql.Connection;
023: import java.sql.SQLException;
024: import java.util.LinkedList;
025: import java.util.List;
026:
027: /**
028: * This class defines a PooledConnection that is a connection in a pool with
029: * associated metadata.
030: *
031: * @author <a href="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
032: * @version 1.0
033: */
034: public class PooledConnection {
035: private Connection connection;
036: private boolean mustBeRenewed = false;
037: private int currentTransactionIsolation = org.continuent.sequoia.driver.Connection.DEFAULT_TRANSACTION_ISOLATION_LEVEL;
038: private int oldTransactionIsolation;
039:
040: /*
041: * List of temporary tables that were defined in this connection.
042: */
043: private List temporaryTables;
044:
045: /**
046: * Creates a new <code>PooledConnection</code> object
047: *
048: * @param c the real connection to the database
049: */
050: public PooledConnection(Connection c) {
051: this .connection = c;
052: temporaryTables = new LinkedList();
053: }
054:
055: /**
056: * Returns the real connection to the database
057: *
058: * @return Returns the connection.
059: */
060: public Connection getConnection() {
061: return connection;
062: }
063:
064: /**
065: * Returns true if the connection must be renewed.
066: *
067: * @return Returns true if the connection must be renewed.
068: */
069: boolean mustBeRenewed() {
070: return mustBeRenewed;
071: }
072:
073: /**
074: * Sets the mustBeRenewed value.
075: *
076: * @param mustBeRenewed true if the connection to the database must be
077: * renewed.
078: */
079: public void setMustBeRenewed(boolean mustBeRenewed) {
080: this .mustBeRenewed = mustBeRenewed;
081: }
082:
083: //
084: // Transaction isolation related functions
085: //
086:
087: /**
088: * Returns true if this connection is set to the default transaction
089: * isolation.
090: *
091: * @return true if default isolation is used
092: */
093: public boolean isDefaultTransactionIsolation() {
094: return currentTransactionIsolation == org.continuent.sequoia.driver.Connection.DEFAULT_TRANSACTION_ISOLATION_LEVEL;
095: }
096:
097: /**
098: * Restore the default transaction isolation that was recorded when
099: * setTransactionIsolation was called. This is a no-op if the transaction
100: * isolation of the connection is already set to the default.
101: *
102: * @throws SQLException if we failed to restore the transaction isolation on
103: * the connection
104: */
105: public final void restoreDefaultTransactionIsolation()
106: throws SQLException {
107: if (isDefaultTransactionIsolation())
108: return;
109: connection.setTransactionIsolation(oldTransactionIsolation);
110: currentTransactionIsolation = org.continuent.sequoia.driver.Connection.DEFAULT_TRANSACTION_ISOLATION_LEVEL;
111: }
112:
113: /**
114: * Sets the transaction isolation on the connection and record its previous
115: * isolation level.
116: *
117: * @param transactionIsolationLevel The transactionIsolation to set.
118: * @throws SQLException if we failed to get the current transaction isolation
119: * of the connection
120: */
121: public final void setTransactionIsolation(
122: int transactionIsolationLevel) throws SQLException {
123: oldTransactionIsolation = connection.getTransactionIsolation();
124: this .currentTransactionIsolation = transactionIsolationLevel;
125: connection.setTransactionIsolation(transactionIsolationLevel);
126: }
127:
128: /**
129: * Closes the underlying connection
130: *
131: * @throws SQLException in case of error
132: */
133: public void close() throws SQLException {
134: if (connection != null && !connection.isClosed()) {
135: connection.close();
136: connection = null;
137: }
138: }
139:
140: /*
141: * Temporary tables management
142: */
143: /**
144: * Adds a temporary table inside this connection's context
145: *
146: * @param temporaryTable the name of the table to add for this connection
147: */
148: public void addTemporaryTables(String temporaryTable) {
149: if (!existsTemporaryTable(temporaryTable))
150: temporaryTables.add(temporaryTable);
151: }
152:
153: /**
154: * Removes the list of temporary tables from this connection's context. This
155: * is used when releasing the connection to the pool.
156: */
157: public void removeAllTemporaryTables() {
158: temporaryTables.clear();
159: }
160:
161: /**
162: * Check if a temporary table exists inside this connection's context
163: *
164: * @param tableName name of the table that is looked for
165: * @return true if the table can be find inside this connection
166: */
167: public boolean existsTemporaryTable(String tableName) {
168: return temporaryTables.contains(tableName);
169: }
170:
171: }
|