KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > connection > PooledConnection


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2006 Continuent, Inc.
4  * Contact: sequoia@continuent.org
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Initial developer(s): Emmanuel Cecchet.
19  * Contributor(s): ______________________.
20  */

21
22 package org.continuent.sequoia.controller.connection;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27 /**
28  * This class defines a PooledConnection that is a connection in a pool with
29  * associated metadata.
30  *
31  * @author <a HREF="mailto:emmanuel.cecchet@continuent.com">Emmanuel Cecchet</a>
32  * @version 1.0
33  */

34 public class PooledConnection
35 {
36   private Connection JavaDoc connection;
37   private boolean mustBeRenewed = false;
38   private int currentTransactionIsolation = org.continuent.sequoia.driver.Connection.DEFAULT_TRANSACTION_ISOLATION_LEVEL;
39   private int oldTransactionIsolation;
40
41   /**
42    * Creates a new <code>PooledConnection</code> object
43    *
44    * @param c the real connection to the database
45    */

46   public PooledConnection(Connection JavaDoc c)
47   {
48     this.connection = c;
49   }
50
51   /**
52    * Returns the real connection to the database
53    *
54    * @return Returns the connection.
55    */

56   public Connection JavaDoc getConnection()
57   {
58     return connection;
59   }
60
61   /**
62    * Returns true if the connection must be renewed.
63    *
64    * @return Returns true if the connection must be renewed.
65    */

66   boolean mustBeRenewed()
67   {
68     return mustBeRenewed;
69   }
70
71   /**
72    * Sets the mustBeRenewed value.
73    *
74    * @param mustBeRenewed true if the connection to the database must be
75    * renewed.
76    */

77   void setMustBeRenewed(boolean mustBeRenewed)
78   {
79     this.mustBeRenewed = mustBeRenewed;
80   }
81
82   //
83
// Transaction isolation related functions
84
//
85

86   /**
87    * Returns true if this connection is set to the default transaction
88    * isolation.
89    *
90    * @return true if default isolation is used
91    */

92   public boolean isDefaultTransactionIsolation()
93   {
94     return currentTransactionIsolation == org.continuent.sequoia.driver.Connection.DEFAULT_TRANSACTION_ISOLATION_LEVEL;
95   }
96
97   /**
98    * Restore the default transaction isolation that was recorded when
99    * 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() throws SQLException JavaDoc
106   {
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(int transactionIsolationLevel)
122       throws SQLException JavaDoc
123   {
124     oldTransactionIsolation = connection.getTransactionIsolation();
125     this.currentTransactionIsolation = transactionIsolationLevel;
126     connection.setTransactionIsolation(transactionIsolationLevel);
127   }
128
129 }
130
Popular Tags