KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jdbc > ConnectionImpl


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ConnectionImpl.java,v 1.11 2005/05/04 14:07:50 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.jdbc;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import java.sql.CallableStatement JavaDoc;
31 import java.sql.Connection JavaDoc;
32 import java.sql.DatabaseMetaData JavaDoc;
33 import java.sql.PreparedStatement JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.sql.SQLWarning JavaDoc;
36 import java.sql.Statement JavaDoc;
37
38 import javax.resource.ResourceException JavaDoc;
39
40 import org.objectweb.jonas.resource.MCInfo;
41 import org.objectweb.jonas.resource.SQLManager;
42
43 import org.objectweb.util.monolog.api.BasicLevel;
44 import org.objectweb.util.monolog.api.Logger;
45
46 /**
47  * The class <b>Connection</b> provides the Connection implementation for
48  * encapsulating JDBC connections
49  * @author Eric hardesty
50 */

51 public class ConnectionImpl implements Connection JavaDoc {
52
53     public Logger trace = null;
54
55     ManagedConnectionImpl mc = null;
56
57     Connection JavaDoc connection = null;
58
59     PrintWriter JavaDoc pw = null;
60
61     long key = 0;
62
63     String JavaDoc user = "";
64
65     private SQLManager conman = null;
66
67     private MCInfo mci = null;
68
69     /**
70      * Traces are enabled ?
71      */

72     private final boolean isDebugging;
73
74     protected ConnectionImpl(ManagedConnectionImpl _mc, Connection JavaDoc _con, long _key, PrintWriter JavaDoc _pw) {
75         mc = _mc;
76         connection = _con;
77         key = _key;
78         pw = _pw;
79         trace = mc.trace;
80         isDebugging = trace.isLoggable(BasicLevel.DEBUG);
81         try {
82             user = mc.getMetaData().getUserName();
83         } catch (Exception JavaDoc ex) {
84         }
85     }
86
87     public void setJonasInfo(MCInfo _mci, SQLManager _conman) {
88         mci = _mci;
89         conman = _conman;
90     }
91
92     public boolean isPhysicallyClosed() throws SQLException JavaDoc {
93         if (isDebugging) {
94             trace.log(BasicLevel.DEBUG, "");
95         }
96         return (connection.isClosed());
97     }
98
99     // IMPLEMENTATION OF METHODS FROM THE java.sql.Connection INTERFACE //
100

101     public void clearWarnings() throws SQLException JavaDoc {
102         if (isDebugging) {
103             trace.log(BasicLevel.DEBUG, "");
104         }
105         try {
106             checkContext();
107         } catch (Exception JavaDoc e) {
108             trace.log(BasicLevel.ERROR, "checkContext error");
109             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
110         }
111         connection.clearWarnings();
112     }
113
114     public void close() throws SQLException JavaDoc {
115         if (isDebugging) {
116             trace.log(BasicLevel.DEBUG, "");
117         }
118         try {
119             closeConnectionImpl();
120         } catch (Exception JavaDoc e) {
121             trace.log(BasicLevel.ERROR, "error");
122             e.printStackTrace();
123             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
124         }
125     }
126
127     public void commit() throws SQLException JavaDoc {
128         if (isDebugging) {
129             trace.log(BasicLevel.DEBUG, "");
130         }
131         try {
132             checkContext();
133             connection.commit();
134         } catch (Exception JavaDoc e) {
135             trace.log(BasicLevel.ERROR, "error");
136             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
137         }
138     }
139
140     public Statement JavaDoc createStatement() throws SQLException JavaDoc {
141         if (isDebugging) {
142             trace.log(BasicLevel.DEBUG, "");
143         }
144         try {
145             checkContext();
146         } catch (Exception JavaDoc e) {
147             trace.log(BasicLevel.ERROR, "checkContext error", e, "ConnectionImpl", "createStatement");
148             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
149         }
150         return connection.createStatement();
151     }
152
153     public Statement JavaDoc createStatement(int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
154         if (isDebugging) {
155             trace.log(BasicLevel.DEBUG, "");
156         }
157         try {
158             checkContext();
159         } catch (Exception JavaDoc e) {
160             trace.log(BasicLevel.ERROR, "ConnectionImpl.createStatement: checkContext error");
161             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
162         }
163         return connection.createStatement(resultSetType, resultSetConcurrency);
164     }
165
166     public boolean getAutoCommit() throws SQLException JavaDoc {
167         if (isDebugging) {
168             trace.log(BasicLevel.DEBUG, "");
169         }
170         try {
171             checkContext();
172             return connection.getAutoCommit();
173         } catch (Exception JavaDoc e) {
174             trace.log(BasicLevel.ERROR, "ConnectionImpl.getAC error");
175             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
176         }
177     }
178
179     public String JavaDoc getCatalog() throws SQLException JavaDoc {
180         if (isDebugging) {
181             trace.log(BasicLevel.DEBUG, "");
182         }
183         try {
184             checkContext();
185         } catch (Exception JavaDoc e) {
186             trace.log(BasicLevel.ERROR, "ConnectionImpl.getCatalog: checkContext error");
187             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
188         }
189         return connection.getCatalog();
190     }
191
192     // JDK 1.4 SQL
193
public int getHoldability() throws SQLException JavaDoc {
194         if (isDebugging) {
195             trace.log(BasicLevel.DEBUG, "");
196         }
197         try {
198             checkContext();
199         } catch (Exception JavaDoc e) {
200             trace.log(BasicLevel.ERROR, "ConnectionImpl.getHoldability: checkContext error");
201             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
202         }
203         return connection.getHoldability();
204     }
205
206     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
207         if (isDebugging) {
208             trace.log(BasicLevel.DEBUG, "");
209         }
210         try {
211             checkContext();
212         } catch (Exception JavaDoc e) {
213             trace.log(BasicLevel.ERROR, "ConnectionImpl.getMetaData: checkContext error");
214             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
215         }
216         return connection.getMetaData();
217     }
218
219     public int getTransactionIsolation() throws SQLException JavaDoc {
220         if (isDebugging) {
221             trace.log(BasicLevel.DEBUG, "");
222         }
223         try {
224             checkContext();
225         } catch (Exception JavaDoc e) {
226             trace.log(BasicLevel.ERROR, "ConnectionImpl.getTransactionIsolation: checkContext error");
227             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
228         }
229         return connection.getTransactionIsolation();
230     }
231
232     public Map JavaDoc getTypeMap() throws SQLException JavaDoc {
233         if (isDebugging) {
234             trace.log(BasicLevel.DEBUG, "");
235         }
236         try {
237             checkContext();
238         } catch (Exception JavaDoc e) {
239             trace.log(BasicLevel.ERROR, "ConnectionImpl.getTypeMap: checkContext error");
240             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
241         }
242         return connection.getTypeMap();
243     }
244
245     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
246         if (isDebugging) {
247             trace.log(BasicLevel.DEBUG, "");
248         }
249         try {
250             checkContext();
251         } catch (Exception JavaDoc e) {
252             trace.log(BasicLevel.ERROR, "ConnectionImpl.getWarnings: checkContext error");
253             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
254         }
255         return connection.getWarnings();
256     }
257
258     public boolean isClosed() throws SQLException JavaDoc {
259         if (isDebugging) {
260             trace.log(BasicLevel.DEBUG, "");
261         }
262         return (key == 0);
263     }
264
265     public boolean isReadOnly() throws SQLException JavaDoc {
266         if (isDebugging) {
267             trace.log(BasicLevel.DEBUG, "");
268         }
269         try {
270             checkContext();
271         } catch (Exception JavaDoc e) {
272             trace.log(BasicLevel.ERROR, "ConnectionImpl.isReadOnly: checkContext error");
273             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
274         }
275         return connection.isReadOnly();
276     }
277
278     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException JavaDoc {
279         if (isDebugging) {
280             trace.log(BasicLevel.DEBUG, "");
281         }
282         try {
283             checkContext();
284         } catch (Exception JavaDoc e) {
285             trace.log(BasicLevel.ERROR, "ConnectionImpl.nativeSQL: checkContext error");
286             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
287         }
288         return connection.nativeSQL(sql);
289     }
290
291     public CallableStatement JavaDoc prepareCall(String JavaDoc sql) throws SQLException JavaDoc {
292         if (isDebugging) {
293             trace.log(BasicLevel.DEBUG, "");
294         }
295         try {
296             checkContext();
297         } catch (Exception JavaDoc e) {
298             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareCall: checkContext error");
299             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
300         }
301         return connection.prepareCall(sql);
302     }
303
304     public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int resultSetType, int resultSetConcurrency) throws SQLException JavaDoc {
305
306         if (isDebugging) {
307             trace.log(BasicLevel.DEBUG, "");
308         }
309         try {
310             checkContext();
311         } catch (Exception JavaDoc e) {
312             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareCall: checkContext error");
313             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
314         }
315         return connection.prepareCall(sql, resultSetType, resultSetConcurrency);
316     }
317
318     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql) throws SQLException JavaDoc {
319         if (isDebugging) {
320             trace.log(BasicLevel.DEBUG, "");
321         }
322         try {
323             checkContext();
324         } catch (Exception JavaDoc e) {
325             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error");
326             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
327         }
328         if (conman != null) {
329             return conman.getPStatement(mci, connection, user, sql);
330         } else {
331             return connection.prepareStatement(sql);
332         }
333     }
334
335     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int rsettype, int rsetconcurrency) throws SQLException JavaDoc {
336         if (isDebugging) {
337             trace.log(BasicLevel.DEBUG, "");
338         }
339         try {
340             checkContext();
341         } catch (Exception JavaDoc e) {
342             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error");
343             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
344         }
345         if (conman != null) {
346             return conman.getPStatement(mci, connection, user, sql, rsettype, rsetconcurrency);
347         } else {
348             return connection.prepareStatement(sql, rsettype, rsetconcurrency);
349         }
350     }
351
352     public void rollback() throws SQLException JavaDoc {
353         if (isDebugging) {
354             trace.log(BasicLevel.DEBUG, "");
355         }
356         try {
357             checkContext();
358             connection.rollback();
359         } catch (Exception JavaDoc e) {
360             trace.log(BasicLevel.ERROR, e.getMessage());
361             e.printStackTrace();
362             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
363         }
364     }
365
366     // JDK 1.4 SQL
367

368     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int rsettype, int rsetconcurrency, int holdability)
369             throws SQLException JavaDoc {
370         if (isDebugging) {
371             trace.log(BasicLevel.DEBUG, "");
372         }
373         try {
374             checkContext();
375         } catch (Exception JavaDoc e) {
376             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error");
377             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
378         }
379         if (conman != null) {
380             return conman.getPStatement(mci, connection, user, sql, rsettype, rsetconcurrency, holdability);
381         } else {
382             return connection.prepareStatement(sql, rsettype, rsetconcurrency, holdability);
383         }
384     }
385
386     // JDK 1.4 SQL
387

388     public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int[] t) throws SQLException JavaDoc {
389         if (isDebugging) {
390             trace.log(BasicLevel.DEBUG, "");
391         }
392         try {
393             checkContext();
394         } catch (Exception JavaDoc e) {
395             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error");
396             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
397         }
398         if (conman != null) {
399             return conman.getPStatement(mci, connection, user, sql, t);
400         } else {
401             return connection.prepareStatement(sql, t);
402         }
403     }
404
405     // JDK 1.4 SQL
406
public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, String JavaDoc[] t) throws SQLException JavaDoc {
407         if (isDebugging) {
408             trace.log(BasicLevel.DEBUG, "");
409         }
410         try {
411             checkContext();
412         } catch (Exception JavaDoc e) {
413             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error");
414             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
415         }
416         if (conman != null) {
417             return conman.getPStatement(mci, connection, user, sql, t);
418         } else {
419             return connection.prepareStatement(sql, t);
420         }
421     }
422
423     // JDK 1.4 SQL
424
public PreparedStatement JavaDoc prepareStatement(String JavaDoc sql, int autoGenKeys) throws SQLException JavaDoc {
425         if (isDebugging) {
426             trace.log(BasicLevel.DEBUG, "");
427         }
428         try {
429             checkContext();
430         } catch (Exception JavaDoc e) {
431             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error");
432             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
433         }
434         if (conman != null) {
435             return conman.getPStatement(mci, connection, user, sql, autoGenKeys);
436         } else {
437             return connection.prepareStatement(sql, autoGenKeys);
438         }
439     }
440
441     // JDK 1.4 SQL
442
public CallableStatement JavaDoc prepareCall(String JavaDoc sql, int rsettype, int rsetconcurrency, int holdability)
443             throws SQLException JavaDoc {
444         if (isDebugging) {
445             trace.log(BasicLevel.DEBUG, "");
446         }
447         try {
448             checkContext();
449         } catch (Exception JavaDoc e) {
450             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error");
451             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
452         }
453         return connection.prepareCall(sql, rsettype, rsetconcurrency, holdability);
454     }
455
456     // JDK 1.4 SQL
457
public Statement JavaDoc createStatement(int rsettype, int rsetconcurrency, int holdability) throws SQLException JavaDoc {
458         if (isDebugging) {
459             trace.log(BasicLevel.DEBUG, "");
460         }
461         try {
462             checkContext();
463         } catch (Exception JavaDoc e) {
464             trace.log(BasicLevel.ERROR, "ConnectionImpl.prepareStatement: checkContext error");
465             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
466         }
467         return connection.createStatement(rsettype, rsetconcurrency, holdability);
468     }
469
470     // JDK 1.4 SQL
471
public void releaseSavepoint(java.sql.Savepoint JavaDoc sp) throws SQLException JavaDoc {
472         if (isDebugging) {
473             trace.log(BasicLevel.DEBUG, "");
474         }
475         try {
476             checkContext();
477             connection.releaseSavepoint(sp);
478         } catch (Exception JavaDoc e) {
479             trace.log(BasicLevel.ERROR, e.getMessage());
480             e.printStackTrace();
481             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
482         }
483     }
484
485     // JDK 1.4 SQL
486
public void rollback(java.sql.Savepoint JavaDoc sp) throws SQLException JavaDoc {
487         if (isDebugging) {
488             trace.log(BasicLevel.DEBUG, "");
489         }
490         try {
491             checkContext();
492             connection.rollback(sp);
493         } catch (Exception JavaDoc e) {
494             trace.log(BasicLevel.ERROR, e.getMessage());
495             e.printStackTrace();
496             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
497         }
498     }
499
500     // JDK 1.4 SQL
501
public java.sql.Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
502         if (isDebugging) {
503             trace.log(BasicLevel.DEBUG, "");
504         }
505         try {
506             checkContext();
507             return connection.setSavepoint();
508         } catch (Exception JavaDoc e) {
509             trace.log(BasicLevel.ERROR, e.getMessage());
510             e.printStackTrace();
511             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
512         }
513     }
514
515     // JDK 1.4 SQL
516
public java.sql.Savepoint JavaDoc setSavepoint(String JavaDoc name) throws SQLException JavaDoc {
517         if (isDebugging) {
518             trace.log(BasicLevel.DEBUG, "");
519         }
520         try {
521             checkContext();
522             return connection.setSavepoint(name);
523         } catch (Exception JavaDoc e) {
524             trace.log(BasicLevel.ERROR, e.getMessage());
525             e.printStackTrace();
526             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
527         }
528     }
529
530     public void setAutoCommit(boolean isauto) throws SQLException JavaDoc {
531         if (isDebugging) {
532             trace.log(BasicLevel.DEBUG, "");
533         }
534         try {
535             checkContext();
536             connection.setAutoCommit(isauto);
537         } catch (Exception JavaDoc e) {
538             trace.log(BasicLevel.ERROR, "ConnectionImpl.setAC: error");
539             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
540         }
541     }
542
543     public void setCatalog(String JavaDoc catalog) throws SQLException JavaDoc {
544         if (isDebugging) {
545             trace.log(BasicLevel.DEBUG, "");
546         }
547         try {
548             checkContext();
549         } catch (Exception JavaDoc e) {
550             trace.log(BasicLevel.ERROR, "ConnectionImpl.setCatalog: checkContext error");
551             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
552         }
553         connection.setCatalog(catalog);
554     }
555
556     // JDK 1.4 SQL
557
public void setHoldability(int holdability) throws SQLException JavaDoc {
558         if (isDebugging) {
559             trace.log(BasicLevel.DEBUG, "");
560         }
561         try {
562             checkContext();
563         } catch (Exception JavaDoc e) {
564             trace.log(BasicLevel.ERROR, "ConnectionImpl.setCatalog: checkContext error");
565             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
566         }
567         connection.setHoldability(holdability);
568     }
569
570     public void setReadOnly(boolean readOnly) throws SQLException JavaDoc {
571         if (isDebugging) {
572             trace.log(BasicLevel.DEBUG, "");
573         }
574         try {
575             checkContext();
576         } catch (Exception JavaDoc e) {
577             trace.log(BasicLevel.ERROR, "ConnectionImpl.setReadOnly: checkContext error");
578             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
579         }
580         connection.setReadOnly(readOnly);
581     }
582
583     public void setTransactionIsolation(int level) throws SQLException JavaDoc {
584         if (isDebugging) {
585             trace.log(BasicLevel.DEBUG, "");
586         }
587         try {
588             checkContext();
589         } catch (Exception JavaDoc e) {
590             trace.log(BasicLevel.ERROR, "ConnectionImpl.setTransactionIsolation: checkContext error");
591             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
592         }
593         connection.setTransactionIsolation(level);
594     }
595
596     public void setTypeMap(Map JavaDoc map) throws SQLException JavaDoc {
597         if (isDebugging) {
598             trace.log(BasicLevel.DEBUG, "");
599         }
600         try {
601             checkContext();
602         } catch (Exception JavaDoc e) {
603             trace.log(BasicLevel.ERROR, "ConnectionImpl.setTypeMap: checkContext error");
604             throw new SQLException JavaDoc("JOnAS JDBC: " + e.getMessage());
605         }
606         connection.setTypeMap(map);
607     }
608
609     private void closeConnectionImpl() throws ResourceException JavaDoc {
610         if (key != 0) {
611             mc.close(this);
612             key = 0;
613         }
614     }
615
616     private void checkContext() throws Exception JavaDoc {
617         if (key == 0) {
618             throw new Exception JavaDoc("Connection is closed");
619         }
620         if (key != mc.getSignature()) {
621             if (mc.getSignature() == 0) {
622                 mc.setSignature(key);
623             } else {
624                 throw new Exception JavaDoc("Connection w/sig(" + key + ") is not currect active Connection ("
625                         + mc.getSignature() + ")");
626             }
627         }
628     }
629
630     public void setSignature(long sig) {
631         key = sig;
632     }
633 }
634
Popular Tags