KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > resource > jdbc > JdbcConnection


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 1999 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: JdbcConnection.java 1447 2004-10-25 12:34:45Z pmlopes $
44  */

45 package org.openejb.resource.jdbc;
46
47 import java.sql.CallableStatement JavaDoc;
48 import java.sql.DatabaseMetaData JavaDoc;
49 import java.sql.PreparedStatement JavaDoc;
50 import java.sql.SQLException JavaDoc;
51 import java.sql.SQLWarning JavaDoc;
52 import java.sql.Statement JavaDoc;
53
54 public class JdbcConnection implements java.sql.Connection JavaDoc {
55     private java.sql.Connection JavaDoc physicalConn;
56     private JdbcManagedConnection managedConn;
57     protected boolean isClosed = false;
58
59     protected JdbcConnection(JdbcManagedConnection managedConn, java.sql.Connection JavaDoc physicalConn){
60         this.physicalConn = physicalConn;
61         this.managedConn = managedConn;
62     }
63     protected java.sql.Connection JavaDoc getPhysicalConnection(){
64         return physicalConn;
65     }
66     protected JdbcManagedConnection getManagedConnection(){
67         return managedConn;
68     }
69     /**
70     * Renders this conneciton invalid; unusable. Its called by the
71     * JdbcManagedConnection when its connectionClose() or cleanup()
72     * methods are invoked.
73     */

74     protected void invalidate(){
75         isClosed = true;
76         physicalConn = null;
77         managedConn = null;
78     }
79     protected void associate(JdbcManagedConnection mngdConn){
80         isClosed = false;
81         managedConn = mngdConn;
82         physicalConn = mngdConn.getSQLConnection();
83     }
84     public Statement JavaDoc createStatement() throws SQLException JavaDoc {
85         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
86         try{
87             synchronized(physicalConn){
88             return physicalConn.createStatement();
89             }
90         }catch(SQLException JavaDoc sqlE){
91             managedConn.connectionErrorOccurred(this, sqlE);
92             throw sqlE;
93         }
94     }
95     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql)
96         throws SQLException JavaDoc{
97         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
98         try{
99             synchronized(physicalConn){
100             return physicalConn.prepareStatement(sql);
101             }
102         }catch(SQLException JavaDoc sqlE){
103             managedConn.connectionErrorOccurred(this, sqlE);
104             throw sqlE;
105         }
106     }
107     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc{
108         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
109         try{
110             synchronized(physicalConn){
111             return physicalConn.prepareCall(sql);
112             }
113         }catch(SQLException JavaDoc sqlE){
114             managedConn.connectionErrorOccurred(this, sqlE);
115             throw sqlE;
116         }
117     }
118     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc{
119         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
120         try{
121             synchronized(physicalConn){
122             return physicalConn.nativeSQL(sql);
123             }
124         }catch(SQLException JavaDoc sqlE){
125             managedConn.connectionErrorOccurred(this, sqlE);
126             throw sqlE;
127         }
128     }
129     public void setAutoCommit(boolean autoCommit) throws SQLException JavaDoc{
130         throw new java.sql.SQLException JavaDoc("Method not supported. Commit is managed automatically by container provider");
131     }
132     public boolean getAutoCommit() throws SQLException JavaDoc{
133         throw new java.sql.SQLException JavaDoc("Method not supported. Commit is managed automatically by container provider");
134     }
135     public void commit() throws SQLException JavaDoc{
136         throw new java.sql.SQLException JavaDoc("Method not supported. Commit is managed automatically by container provider");
137     }
138     public void rollback() throws SQLException JavaDoc{
139         throw new java.sql.SQLException JavaDoc("Method not supported. Rollback is managed automatically by container provider");
140     }
141     public void close() throws SQLException JavaDoc{
142         if(isClosed)
143             return;
144         else{
145             // managed conneciton will call this object's invalidate() method which
146
// will set isClosed = true, and nullify references to the sqlConnection and managed connection.
147
managedConn.connectionClose(this);
148         }
149     }
150     public boolean isClosed() throws SQLException JavaDoc{
151         return isClosed;
152     }
153     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc{
154         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
155         try{
156             synchronized(physicalConn){
157             return physicalConn.getMetaData();
158             }
159         }catch(SQLException JavaDoc sqlE){
160             managedConn.connectionErrorOccurred(this, sqlE);
161             throw sqlE;
162         }
163     }
164     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc{
165         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
166         try{
167             synchronized(physicalConn){
168             physicalConn.setReadOnly(readOnly);
169             }
170         }catch(SQLException JavaDoc sqlE){
171             managedConn.connectionErrorOccurred(this, sqlE);
172             throw sqlE;
173         }
174     }
175     public boolean isReadOnly() throws SQLException JavaDoc{
176         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
177         try{
178             synchronized(physicalConn){
179             return physicalConn.isReadOnly();
180             }
181         }catch(SQLException JavaDoc sqlE){
182             managedConn.connectionErrorOccurred(this, sqlE);
183             throw sqlE;
184         }
185     }
186     public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc{
187         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
188         try{
189             synchronized(physicalConn){
190             physicalConn.setCatalog(catalog);
191             }
192         }catch(SQLException JavaDoc sqlE){
193             managedConn.connectionErrorOccurred(this, sqlE);
194             throw sqlE;
195         }
196     }
197     public String JavaDoc getCatalog() throws SQLException JavaDoc{
198         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
199         try{
200             synchronized(physicalConn){
201             return physicalConn.getCatalog();
202             }
203         }catch(SQLException JavaDoc sqlE){
204             managedConn.connectionErrorOccurred(this, sqlE);
205             throw sqlE;
206         }
207     }
208     public void setTransactionIsolation(int level) throws SQLException JavaDoc{
209         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
210         try{
211             synchronized(physicalConn){
212             physicalConn.setTransactionIsolation(level);
213             }
214         }catch(SQLException JavaDoc sqlE){
215             managedConn.connectionErrorOccurred(this, sqlE);
216             throw sqlE;
217         }
218     }
219     public int getTransactionIsolation() throws SQLException JavaDoc{
220         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
221         try{
222             synchronized(physicalConn){
223             return physicalConn.getTransactionIsolation();
224             }
225         }catch(SQLException JavaDoc sqlE){
226             managedConn.connectionErrorOccurred(this, sqlE);
227             throw sqlE;
228         }
229     }
230     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc{
231         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
232         try{
233             synchronized(physicalConn){
234             return physicalConn.getWarnings();
235             }
236         }catch(SQLException JavaDoc sqlE){
237             managedConn.connectionErrorOccurred(this, sqlE);
238             throw sqlE;
239         }
240     }
241     public void clearWarnings() throws SQLException JavaDoc{
242         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
243         try{
244             synchronized(physicalConn){
245             physicalConn.clearWarnings();
246             }
247         }catch(SQLException JavaDoc sqlE){
248             managedConn.connectionErrorOccurred(this, sqlE);
249             throw sqlE;
250         }
251     }
252     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency)
253       throws SQLException JavaDoc{
254         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
255         try{
256             synchronized(physicalConn){
257             return physicalConn.createStatement(resultSetType, resultSetConcurrency);
258             }
259         }catch(SQLException JavaDoc sqlE){
260             managedConn.connectionErrorOccurred(this, sqlE);
261             throw sqlE;
262         }
263     }
264     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType,int resultSetConcurrency)
265        throws SQLException JavaDoc{
266         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
267         try{
268             synchronized(physicalConn){
269             return physicalConn.prepareStatement(sql, resultSetType,resultSetConcurrency);
270             }
271         }catch(SQLException JavaDoc sqlE){
272             managedConn.connectionErrorOccurred(this, sqlE);
273             throw sqlE;
274         }
275     }
276     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType,int resultSetConcurrency) throws SQLException JavaDoc{
277         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
278         try{
279             synchronized(physicalConn){
280             return physicalConn.prepareCall(sql, resultSetType,resultSetConcurrency) ;
281             }
282         }catch(SQLException JavaDoc sqlE){
283             managedConn.connectionErrorOccurred(this, sqlE);
284             throw sqlE;
285         }
286     }
287     public java.util.Map JavaDoc getTypeMap() throws SQLException JavaDoc{
288         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
289         try{
290             synchronized(physicalConn){
291             return physicalConn.getTypeMap();
292             }
293         }catch(SQLException JavaDoc sqlE){
294             managedConn.connectionErrorOccurred(this, sqlE);
295             throw sqlE;
296         }
297     }
298     public void setTypeMap(java.util.Map JavaDoc map) throws SQLException JavaDoc{
299         if(isClosed) throw new SQLException JavaDoc("Connection is closed");
300         try{
301             synchronized(physicalConn){
302             physicalConn.setTypeMap(map);
303             }
304         }catch(SQLException JavaDoc sqlE){
305             managedConn.connectionErrorOccurred(this, sqlE);
306             throw sqlE;
307         }
308     }
309
310     /**
311      * JDBC 3
312      */

313     public void setHoldability(int holdability) throws java.sql.SQLException JavaDoc {
314     throw new SQLException JavaDoc("method setHoldability not implemented");
315     }
316     public int getHoldability() throws java.sql.SQLException JavaDoc{
317     throw new SQLException JavaDoc("method getHoldability not implemented");
318     }
319     public java.sql.Savepoint JavaDoc setSavepoint() throws java.sql.SQLException JavaDoc{
320     throw new SQLException JavaDoc("method not implemented");
321     }
322     public java.sql.Savepoint JavaDoc setSavepoint(String JavaDoc name) throws java.sql.SQLException JavaDoc{
323     throw new SQLException JavaDoc("method not implemented");
324     }
325     public void rollback(java.sql.Savepoint JavaDoc savepoint) throws java.sql.SQLException JavaDoc{
326     throw new SQLException JavaDoc("method not implemented");
327     }
328     public void releaseSavepoint(java.sql.Savepoint JavaDoc savepoint) throws java.sql.SQLException JavaDoc{
329     throw new SQLException JavaDoc("method not implemented");
330     }
331     public java.sql.Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.SQLException JavaDoc{
332     throw new SQLException JavaDoc("method not implemented");
333     }
334     public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.SQLException JavaDoc{
335     throw new SQLException JavaDoc("method not implemented");
336     }
337     public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws java.sql.SQLException JavaDoc{
338     throw new SQLException JavaDoc("method not implemented");
339     }
340     public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int[] columnIndexes) throws java.sql.SQLException JavaDoc{
341     throw new SQLException JavaDoc("method not implemented");
342     }
343     public java.sql.PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames) throws java.sql.SQLException JavaDoc{
344     throw new SQLException JavaDoc("method not implemented");
345     }
346     public java.sql.CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws java.sql.SQLException JavaDoc{
347     throw new SQLException JavaDoc("method not implemented");
348     }
349 }
350
351
352
353
354
Popular Tags