KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > component > jdbcpool > JConnection


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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: JConnection.java 1022 2006-08-04 11:02:28Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.component.jdbcpool;
27
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.Savepoint JavaDoc;
37 import java.sql.Statement JavaDoc;
38
39 import org.objectweb.easybeans.log.JLog;
40 import org.objectweb.easybeans.log.JLogFactory;
41
42 /**
43  * This class represent a connection linked to the physical and XA connections.
44  * All errors are reported to the managed connection.
45  * This connection is returned to the client.
46  * @author Philippe Durieux
47  * @author Florent Benoit
48  */

49 public class JConnection implements Connection JavaDoc {
50
51     /**
52      * Logger used for debug.
53      */

54     private static JLog logger = JLogFactory.getLog(JConnection.class);
55
56     /**
57      * JDBC connection provided by the DriverManager.
58      */

59     private Connection JavaDoc physicalConnection = null;
60
61     /**
62      * XA connection which receive events.
63      */

64     private JManagedConnection xaConnection = null;
65
66     /**
67      * Buils a Connection (viewed by the user) which rely on a Managed
68      * connection and a physical connection.
69      * @param xaConnection the XA connection.
70      * @param physicalConnection the connection to the database.
71      */

72     public JConnection(final JManagedConnection xaConnection, final Connection JavaDoc physicalConnection) {
73         this.xaConnection = xaConnection;
74         this.physicalConnection = physicalConnection;
75     }
76
77     /**
78      * Gets the physical connection to the database.
79      * @return physical connection to the database
80      */

81     public Connection JavaDoc getConnection() {
82         return physicalConnection;
83     }
84
85     /**
86      * {@inheritDoc}
87      */

88     public Statement JavaDoc createStatement() throws SQLException JavaDoc {
89         try {
90             return physicalConnection.createStatement();
91         } catch (SQLException JavaDoc e) {
92             xaConnection.notifyError(e);
93             throw e;
94         }
95     }
96
97     /**
98      * {@inheritDoc}
99      */

100     public PreparedStatement JavaDoc prepareStatement(final String JavaDoc sql) throws SQLException JavaDoc {
101         try {
102             // Ask the Managed Connection to find one in the pool, if possible.
103
return xaConnection.prepareStatement(sql);
104         } catch (SQLException JavaDoc e) {
105             xaConnection.notifyError(e);
106             throw e;
107         }
108     }
109
110     /**
111      * {@inheritDoc}
112      */

113     public CallableStatement JavaDoc prepareCall(final String JavaDoc sql) throws SQLException JavaDoc {
114         try {
115             return physicalConnection.prepareCall(sql);
116         } catch (SQLException JavaDoc e) {
117             xaConnection.notifyError(e);
118             throw e;
119         }
120     }
121
122     /**
123      * {@inheritDoc}
124      */

125     public String JavaDoc nativeSQL(final String JavaDoc sql) throws SQLException JavaDoc {
126         try {
127             return physicalConnection.nativeSQL(sql);
128         } catch (SQLException JavaDoc e) {
129             xaConnection.notifyError(e);
130             throw e;
131         }
132     }
133
134     /**
135      * @return true if the connection to the database is closed or not.
136      * @throws SQLException if a database access error occurs
137      */

138     public boolean isPhysicallyClosed() throws SQLException JavaDoc {
139         return physicalConnection.isClosed();
140     }
141
142     /**
143      * {@inheritDoc}
144      */

145     public boolean isClosed() throws SQLException JavaDoc {
146
147         // TODO : This should return the status of the connection viewed from
148
// the user,
149
// not the physical connection!
150
try {
151             return physicalConnection.isClosed();
152         } catch (SQLException JavaDoc e) {
153             xaConnection.notifyError(e);
154             throw e;
155         }
156     }
157
158     /**
159      * {@inheritDoc}
160      */

161     public DatabaseMetaData JavaDoc getMetaData() throws SQLException JavaDoc {
162         try {
163             return physicalConnection.getMetaData();
164         } catch (SQLException JavaDoc e) {
165             xaConnection.notifyError(e);
166             throw e;
167         }
168     }
169
170     /**
171      * {@inheritDoc}
172      */

173     public void setReadOnly(final boolean readOnly) throws SQLException JavaDoc {
174         try {
175             physicalConnection.setReadOnly(readOnly);
176         } catch (SQLException JavaDoc e) {
177             xaConnection.notifyError(e);
178             throw e;
179         }
180     }
181
182     /**
183      * {@inheritDoc}
184      */

185     public boolean isReadOnly() throws SQLException JavaDoc {
186         try {
187             return physicalConnection.isReadOnly();
188         } catch (SQLException JavaDoc e) {
189             xaConnection.notifyError(e);
190             throw e;
191         }
192     }
193
194     /**
195      * {@inheritDoc}
196      */

197     public void setCatalog(final String JavaDoc catalog) throws SQLException JavaDoc {
198         try {
199             physicalConnection.setCatalog(catalog);
200         } catch (SQLException JavaDoc e) {
201             xaConnection.notifyError(e);
202             throw e;
203         }
204     }
205
206     /**
207      * {@inheritDoc}
208      */

209     public String JavaDoc getCatalog() throws SQLException JavaDoc {
210         try {
211             return physicalConnection.getCatalog();
212         } catch (SQLException JavaDoc e) {
213             xaConnection.notifyError(e);
214             throw e;
215         }
216     }
217
218     /**
219      * Trigger an event to the listener.
220      * @exception SQLException if a database access error occurs
221      */

222     public void close() throws SQLException JavaDoc {
223         xaConnection.notifyClose();
224     }
225
226     /**
227      * {@inheritDoc}
228      */

229     public void setTransactionIsolation(final int level) throws SQLException JavaDoc {
230         try {
231             physicalConnection.setTransactionIsolation(level);
232         } catch (SQLException JavaDoc e) {
233             xaConnection.notifyError(e);
234             throw e;
235         }
236     }
237
238     /**
239      * {@inheritDoc}
240      */

241     public int getTransactionIsolation() throws SQLException JavaDoc {
242         try {
243             return physicalConnection.getTransactionIsolation();
244         } catch (SQLException JavaDoc e) {
245             xaConnection.notifyError(e);
246             throw e;
247         }
248     }
249
250     /**
251      * {@inheritDoc}
252      */

253     public SQLWarning JavaDoc getWarnings() throws SQLException JavaDoc {
254         try {
255             return physicalConnection.getWarnings();
256         } catch (SQLException JavaDoc e) {
257             xaConnection.notifyError(e);
258             throw e;
259         }
260     }
261
262     /**
263      * {@inheritDoc}
264      */

265     public void clearWarnings() throws SQLException JavaDoc {
266         try {
267             physicalConnection.clearWarnings();
268         } catch (SQLException JavaDoc e) {
269             xaConnection.notifyError(e);
270             throw e;
271         }
272     }
273
274     /**
275      * In a JDBC-XA driver, Connection.commit is only called if we are outside a
276      * global transaction. {@inheritDoc}
277      */

278     public void commit() throws SQLException JavaDoc {
279         try {
280             physicalConnection.commit();
281         } catch (SQLException JavaDoc e) {
282             xaConnection.notifyError(e);
283             throw e;
284         }
285     }
286
287     /**
288      * In a JDBC-XA driver, Connection.rollback is only called if we are outside
289      * a global transaction. {@inheritDoc}
290      */

291     public void rollback() throws SQLException JavaDoc {
292         try {
293             physicalConnection.rollback();
294         } catch (SQLException JavaDoc e) {
295             xaConnection.notifyError(e);
296             throw e;
297         }
298     }
299
300     /**
301      * In a JDBC-XA driver, autocommit is false if we are in a global Tx.
302      * {@inheritDoc}
303      */

304     @SuppressWarnings JavaDoc("boxing")
305     public void setAutoCommit(final boolean autoCommit) throws SQLException JavaDoc {
306         try {
307             physicalConnection.setAutoCommit(autoCommit);
308         } catch (SQLException JavaDoc e) {
309             logger.error("setAutoCommit( {0} ) failed: ", autoCommit, e);
310             xaConnection.notifyError(e);
311             throw e;
312         }
313     }
314
315     /**
316      * In a JDBC-XA driver, autocommit is false if we are in a global Tx.
317      * {@inheritDoc}
318      */

319     public boolean getAutoCommit() throws SQLException JavaDoc {
320         try {
321             return physicalConnection.getAutoCommit();
322         } catch (SQLException JavaDoc e) {
323             xaConnection.notifyError(e);
324             throw e;
325         }
326     }
327
328     /**
329      * {@inheritDoc}
330      */

331     public Statement JavaDoc createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException JavaDoc {
332         try {
333             return physicalConnection.createStatement(resultSetType, resultSetConcurrency);
334         } catch (SQLException JavaDoc e) {
335             xaConnection.notifyError(e);
336             throw e;
337         }
338     }
339
340     /**
341      * {@inheritDoc}
342      */

343     public Map JavaDoc<String JavaDoc, Class JavaDoc<?>> getTypeMap() throws SQLException JavaDoc {
344         try {
345             return physicalConnection.getTypeMap();
346         } catch (SQLException JavaDoc e) {
347             xaConnection.notifyError(e);
348             throw e;
349         }
350     }
351
352     /**
353      * {@inheritDoc}
354      */

355     public void setTypeMap(final Map JavaDoc map) throws SQLException JavaDoc {
356         try {
357             setTypeMap(map);
358         } catch (SQLException JavaDoc e) {
359             xaConnection.notifyError(e);
360             throw e;
361         }
362     }
363
364     /**
365      * {@inheritDoc}
366      */

367     public PreparedStatement JavaDoc prepareStatement(final String JavaDoc sql, final int resultSetType, final int resultSetConcurrency)
368             throws SQLException JavaDoc {
369         try {
370             return xaConnection.prepareStatement(sql, resultSetType, resultSetConcurrency);
371         } catch (SQLException JavaDoc e) {
372             xaConnection.notifyError(e);
373             throw e;
374         }
375     }
376
377     /**
378      * {@inheritDoc}
379      */

380     public CallableStatement JavaDoc prepareCall(final String JavaDoc sql, final int resultSetType, final int resultSetConcurrency)
381             throws SQLException JavaDoc {
382         try {
383             return physicalConnection.prepareCall(sql, resultSetType, resultSetConcurrency);
384         } catch (SQLException JavaDoc e) {
385             xaConnection.notifyError(e);
386             throw e;
387         }
388     }
389
390     /**
391      * {@inheritDoc}
392      */

393     public Statement JavaDoc createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability)
394             throws SQLException JavaDoc {
395         try {
396             return physicalConnection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
397         } catch (SQLException JavaDoc e) {
398             xaConnection.notifyError(e);
399             throw e;
400         }
401     }
402
403     /**
404      * {@inheritDoc}
405      */

406     public int getHoldability() throws SQLException JavaDoc {
407         try {
408             return physicalConnection.getHoldability();
409         } catch (SQLException JavaDoc e) {
410             xaConnection.notifyError(e);
411             throw e;
412         }
413     }
414
415     /**
416      * {@inheritDoc}
417      */

418     public CallableStatement JavaDoc prepareCall(final String JavaDoc sql, final int resultSetType, final int resultSetConcurrency,
419             final int resultSetHoldability) throws SQLException JavaDoc {
420         try {
421             return physicalConnection.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
422         } catch (SQLException JavaDoc e) {
423             xaConnection.notifyError(e);
424             throw e;
425         }
426     }
427
428     /**
429      * {@inheritDoc}
430      */

431     public PreparedStatement JavaDoc prepareStatement(final String JavaDoc sql, final int autoGeneratedKeys) throws SQLException JavaDoc {
432         try {
433             return physicalConnection.prepareStatement(sql, autoGeneratedKeys);
434         } catch (SQLException JavaDoc e) {
435             xaConnection.notifyError(e);
436             throw e;
437         }
438     }
439
440     /**
441      * {@inheritDoc}
442      */

443     public PreparedStatement JavaDoc prepareStatement(final String JavaDoc sql, final int resultSetType, final int resultSetConcurrency,
444             final int resultSetHoldability) throws SQLException JavaDoc {
445         try {
446             return physicalConnection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
447         } catch (SQLException JavaDoc e) {
448             xaConnection.notifyError(e);
449             throw e;
450         }
451     }
452
453     /**
454      * {@inheritDoc}
455      */

456     public PreparedStatement JavaDoc prepareStatement(final String JavaDoc sql, final int[] columnIndexes) throws SQLException JavaDoc {
457         try {
458             return physicalConnection.prepareStatement(sql, columnIndexes);
459         } catch (SQLException JavaDoc e) {
460             xaConnection.notifyError(e);
461             throw e;
462         }
463     }
464
465     /**
466      * {@inheritDoc}
467      */

468     public PreparedStatement JavaDoc prepareStatement(final String JavaDoc sql, final String JavaDoc[] columnNames) throws SQLException JavaDoc {
469         try {
470             return physicalConnection.prepareStatement(sql, columnNames);
471         } catch (SQLException JavaDoc e) {
472             xaConnection.notifyError(e);
473             throw e;
474         }
475     }
476
477     /**
478      * {@inheritDoc}
479      */

480     public void releaseSavepoint(final Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
481         try {
482             physicalConnection.releaseSavepoint(savepoint);
483         } catch (SQLException JavaDoc e) {
484             xaConnection.notifyError(e);
485             throw e;
486         }
487     }
488
489     /**
490      * {@inheritDoc}
491      */

492     public void rollback(final Savepoint JavaDoc savepoint) throws SQLException JavaDoc {
493         try {
494             physicalConnection.rollback(savepoint);
495         } catch (SQLException JavaDoc e) {
496             xaConnection.notifyError(e);
497             throw e;
498         }
499     }
500
501     /**
502      * {@inheritDoc}
503      */

504     public void setHoldability(final int holdability) throws SQLException JavaDoc {
505         try {
506             physicalConnection.setHoldability(holdability);
507         } catch (SQLException JavaDoc e) {
508             xaConnection.notifyError(e);
509             throw e;
510         }
511     }
512
513     /**
514      * {@inheritDoc}
515      */

516     public Savepoint JavaDoc setSavepoint() throws SQLException JavaDoc {
517         try {
518             return physicalConnection.setSavepoint();
519         } catch (SQLException JavaDoc e) {
520             xaConnection.notifyError(e);
521             throw e;
522         }
523     }
524
525     /**
526      * {@inheritDoc}
527      */

528     public java.sql.Savepoint JavaDoc setSavepoint(final String JavaDoc name) throws SQLException JavaDoc {
529         try {
530             return physicalConnection.setSavepoint(name);
531         } catch (SQLException JavaDoc e) {
532             xaConnection.notifyError(e);
533             throw e;
534         }
535     }
536
537 }
538
Popular Tags