KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > gjc > spi > ConnectionHolder


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 package com.sun.gjc.spi;
25
26 import java.sql.*;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import com.sun.enterprise.util.i18n.StringManager;
31 import com.sun.gjc.common.DataSourceObjectBuilder;
32 import javax.resource.ResourceException JavaDoc;
33
34 /**
35  * Holds the java.sql.Connection object, which is to be
36  * passed to the application program.
37  *
38  * @version 1.0, 02/07/23
39  * @author Binod P.G
40  */

41 public class ConnectionHolder implements Connection{
42
43     protected Connection con;
44     
45     protected ManagedConnection mc;
46     
47     protected boolean wrappedAlready = false;
48     
49     protected boolean isClosed = false;
50     
51     protected boolean valid = true;
52     
53     protected boolean active = false;
54
55     private javax.resource.spi.LazyAssociatableConnectionManager JavaDoc lazyAssocCm_;
56     private javax.resource.spi.LazyEnlistableConnectionManager JavaDoc lazyEnlistCm_;
57
58     private javax.resource.spi.ConnectionRequestInfo JavaDoc cxReqInfo_;
59     
60     private javax.resource.spi.ManagedConnectionFactory JavaDoc mcf_;
61
62     public static enum ConnectionType { LAZY_ENLISTABLE, LAZY_ASSOCIATABLE, STANDARD };
63
64     private ConnectionType myType_ = ConnectionType.STANDARD;
65
66     /**
67      * The active flag is false when the connection handle is
68      * created. When a method is invoked on this object, it asks
69      * the ManagedConnection if it can be the active connection
70      * handle out of the multiple connection handles. If the
71      * ManagedConnection reports that this connection handle
72      * can be active by setting this flag to true via the setActive
73      * function, the above method invocation succeeds; otherwise
74      * an exception is thrown.
75      */

76     
77     static protected StringManager sm = StringManager.getManager(
78             DataSourceObjectBuilder.class);
79
80
81     /**
82      * Constructs a Connection holder.
83      *
84      * @param con <code>java.sql.Connection</code> object.
85      */

86     public ConnectionHolder(Connection con, ManagedConnection mc,
87         javax.resource.spi.ConnectionRequestInfo JavaDoc cxRequestInfo ) {
88         this.con = con;
89         this.mc = mc;
90         mcf_ = mc.mcf;
91         cxReqInfo_ = cxRequestInfo;
92     }
93         
94     /**
95      * Returns the actual connection in this holder object.
96      *
97      * @return Connection object.
98      */

99     Connection getConnection() {
100         return con;
101     }
102     
103     /**
104      * Sets the flag to indicate that, the connection is wrapped already or not.
105      *
106      * @param wrapFlag
107      */

108     void wrapped(boolean wrapFlag){
109         this.wrappedAlready = wrapFlag;
110     }
111     
112     /**
113      * Returns whether it is wrapped already or not.
114      *
115      * @return wrapped flag.
116      */

117     boolean isWrapped(){
118         return wrappedAlready;
119     }
120     
121     /**
122      * Returns the <code>ManagedConnection</code> instance responsible
123      * for this connection.
124      *
125      * @return <code>ManagedConnection</code> instance.
126      */

127     ManagedConnection getManagedConnection() {
128         return mc;
129     }
130     
131     /**
132      * Replace the actual <code>java.sql.Connection</code> object with the one
133      * supplied. Also replace <code>ManagedConnection</code> link.
134      *
135      * @param con <code>Connection</code> object.
136      * @param mc <code> ManagedConnection</code> object.
137      */

138     void associateConnection(Connection con, ManagedConnection mc) {
139         this.mc = mc;
140         this.con = con;
141     }
142     
143     /**
144      * Clears all warnings reported for the underlying connection object.
145      *
146      * @throws SQLException In case of a database error.
147      */

148     public void clearWarnings() throws SQLException{
149     checkValidity();
150         con.clearWarnings();
151     }
152     
153     /**
154      * Closes the logical connection.
155      *
156      * @throws SQLException In case of a database error.
157      */

158     public void close() throws SQLException{
159         isClosed = true;
160         if ( mc != null ) {
161             //mc might be null if this is a lazyAssociatable connection
162
//and has not been associated yet or has been disassociated
163
mc.connectionClosed(null, this);
164         }
165     }
166     
167     /**
168      * Invalidates this object.
169      */

170     public void invalidate() {
171         valid = false;
172     }
173     
174     /**
175      * Closes the physical connection involved in this.
176      *
177      * @throws SQLException In case of a database error.
178      */

179     void actualClose() throws SQLException{
180         con.close();
181     }
182     
183     /**
184      * Commit the changes in the underlying Connection.
185      *
186      * @throws SQLException In case of a database error.
187      */

188     public void commit() throws SQLException {
189     checkValidity();
190         con.commit();
191     }
192     
193     /**
194      * Creates a statement from the underlying Connection
195      *
196      * @return <code>Statement</code> object.
197      * @throws SQLException In case of a database error.
198      */

199     public Statement createStatement() throws SQLException {
200     checkValidity();
201         jdbcPreInvoke();
202         return con.createStatement();
203     }
204     
205     /**
206      * Creates a statement from the underlying Connection.
207      *
208      * @param resultSetType Type of the ResultSet
209      * @param resultSetConcurrency ResultSet Concurrency.
210      * @return <code>Statement</code> object.
211      * @throws SQLException In case of a database error.
212      */

213     public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
214     checkValidity();
215         jdbcPreInvoke();
216         return con.createStatement(resultSetType, resultSetConcurrency);
217     }
218     
219     /**
220      * Creates a statement from the underlying Connection.
221      *
222      * @param resultSetType Type of the ResultSet
223      * @param resultSetConcurrency ResultSet Concurrency.
224      * @param resultSetHoldability ResultSet Holdability.
225      * @return <code>Statement</code> object.
226      * @throws SQLException In case of a database error.
227      */

228     public Statement createStatement(int resultSetType, int resultSetConcurrency,
229                          int resultSetHoldabilty) throws SQLException {
230     checkValidity();
231         jdbcPreInvoke();
232         return con.createStatement(resultSetType, resultSetConcurrency,
233                        resultSetHoldabilty);
234     }
235  
236     /**
237      * Retrieves the current auto-commit mode for the underlying <code> Connection</code>.
238      *
239      * @return The current state of connection's auto-commit mode.
240      * @throws SQLException In case of a database error.
241      */

242     public boolean getAutoCommit() throws SQLException {
243     checkValidity();
244         return con.getAutoCommit();
245     }
246  
247     /**
248      * Retrieves the underlying <code>Connection</code> object's catalog name.
249      *
250      * @return Catalog Name.
251      * @throws SQLException In case of a database error.
252      */

253     public String JavaDoc getCatalog() throws SQLException {
254     checkValidity();
255         return con.getCatalog();
256     }
257     
258     /**
259      * Retrieves the current holdability of <code>ResultSet</code> objects created
260      * using this connection object.
261      *
262      * @return holdability value.
263      * @throws SQLException In case of a database error.
264      */

265     public int getHoldability() throws SQLException {
266     checkValidity();
267         return con.getHoldability();
268     }
269     
270     /**
271      * Retrieves the <code>DatabaseMetaData</code>object from the underlying
272      * <code> Connection </code> object.
273      *
274      * @return <code>DatabaseMetaData</code> object.
275      * @throws SQLException In case of a database error.
276      */

277     public DatabaseMetaData getMetaData() throws SQLException {
278     checkValidity();
279         return con.getMetaData();
280     }
281  
282     /**
283      * Retrieves this <code>Connection</code> object's current transaction isolation level.
284      *
285      * @return Transaction level
286      * @throws SQLException In case of a database error.
287      */

288     public int getTransactionIsolation() throws SQLException {
289     checkValidity();
290         return con.getTransactionIsolation();
291     }
292     
293     /**
294      * Retrieves the <code>Map</code> object associated with
295      * <code> Connection</code> Object.
296      *
297      * @return TypeMap set in this object.
298      * @throws SQLException In case of a database error.
299      */

300     public Map JavaDoc getTypeMap() throws SQLException {
301     checkValidity();
302         return con.getTypeMap();
303     }
304     
305     /**
306      * Retrieves the the first warning reported by calls on the underlying
307      * <code>Connection</code> object.
308      *
309      * @return First <code> SQLWarning</code> Object or null.
310      * @throws SQLException In case of a database error.
311      */

312     public SQLWarning getWarnings() throws SQLException {
313     checkValidity();
314         return con.getWarnings();
315     }
316     
317     /**
318      * Retrieves whether underlying <code>Connection</code> object is closed.
319      *
320      * @return true if <code>Connection</code> object is closed, false
321      * if it is closed.
322      * @throws SQLException In case of a database error.
323      */

324     public boolean isClosed() throws SQLException {
325         return isClosed;
326     }
327     
328     /**
329      * Retrieves whether this <code>Connection</code> object is read-only.
330      *
331      * @return true if <code> Connection </code> is read-only, false other-wise
332      * @throws SQLException In case of a database error.
333      */

334     public boolean isReadOnly() throws SQLException {
335     checkValidity();
336         return con.isReadOnly();
337     }
338     
339     /**
340      * Converts the given SQL statement into the system's native SQL grammer.
341      *
342      * @param sql SQL statement , to be converted.
343      * @return Converted SQL string.
344      * @throws SQLException In case of a database error.
345      */

346     public String JavaDoc nativeSQL(String JavaDoc sql) throws SQLException {
347     checkValidity();
348         return con.nativeSQL(sql);
349     }
350     
351     /**
352      * Creates a <code> CallableStatement </code> object for calling database
353      * stored procedures.
354      *
355      * @param sql SQL Statement
356      * @return <code> CallableStatement</code> object.
357      * @throws SQLException In case of a database error.
358      */

359     public CallableStatement prepareCall(String JavaDoc sql) throws SQLException {
360     checkValidity();
361         jdbcPreInvoke();
362         return con.prepareCall(sql);
363     }
364     
365     /**
366      * Creates a <code> CallableStatement </code> object for calling database
367      * stored procedures.
368      *
369      * @param sql SQL Statement
370      * @param resultSetType Type of the ResultSet
371      * @param resultSetConcurrency ResultSet Concurrency.
372      * @return <code> CallableStatement</code> object.
373      * @throws SQLException In case of a database error.
374      */

375     public CallableStatement prepareCall(String JavaDoc sql,int resultSetType,
376                         int resultSetConcurrency) throws SQLException{
377     checkValidity();
378         jdbcPreInvoke();
379         return con.prepareCall(sql, resultSetType, resultSetConcurrency);
380     }
381     
382     /**
383      * Creates a <code> CallableStatement </code> object for calling database
384      * stored procedures.
385      *
386      * @param sql SQL Statement
387      * @param resultSetType Type of the ResultSet
388      * @param resultSetConcurrency ResultSet Concurrency.
389      * @param resultSetHoldability ResultSet Holdability.
390      * @return <code> CallableStatement</code> object.
391      * @throws SQLException In case of a database error.
392      */

393     public CallableStatement prepareCall(String JavaDoc sql, int resultSetType,
394                          int resultSetConcurrency,
395                          int resultSetHoldabilty) throws SQLException{
396     checkValidity();
397         jdbcPreInvoke();
398         return con.prepareCall(sql, resultSetType, resultSetConcurrency,
399                        resultSetHoldabilty);
400     }
401
402     /**
403      * Creates a <code> PreparedStatement </code> object for sending
404      * paramterized SQL statements to database
405      *
406      * @param sql SQL Statement
407      * @return <code> PreparedStatement</code> object.
408      * @throws SQLException In case of a database error.
409      */

410     public PreparedStatement prepareStatement(String JavaDoc sql) throws SQLException {
411     checkValidity();
412         jdbcPreInvoke();
413         return con.prepareStatement(sql);
414     }
415     
416     /**
417      * Creates a <code> PreparedStatement </code> object for sending
418      * paramterized SQL statements to database
419      *
420      * @param sql SQL Statement
421      * @param autoGeneratedKeys a flag indicating AutoGeneratedKeys need to be returned.
422      * @return <code> PreparedStatement</code> object.
423      * @throws SQLException In case of a database error.
424      */

425     public PreparedStatement prepareStatement(String JavaDoc sql, int autoGeneratedKeys) throws SQLException {
426     checkValidity();
427         jdbcPreInvoke();
428         return con.prepareStatement(sql,autoGeneratedKeys);
429     }
430       
431     /**
432      * Creates a <code> PreparedStatement </code> object for sending
433      * paramterized SQL statements to database
434      *
435      * @param sql SQL Statement
436      * @param columnIndexes an array of column indexes indicating the columns that should be
437      * returned from the inserted row or rows.
438      * @return <code> PreparedStatement</code> object.
439      * @throws SQLException In case of a database error.
440      */

441     public PreparedStatement prepareStatement(String JavaDoc sql, int[] columnIndexes) throws SQLException {
442     checkValidity();
443         jdbcPreInvoke();
444         return con.prepareStatement(sql,columnIndexes);
445     }
446  
447     /**
448      * Creates a <code> PreparedStatement </code> object for sending
449      * paramterized SQL statements to database
450      *
451      * @param sql SQL Statement
452      * @param resultSetType Type of the ResultSet
453      * @param resultSetConcurrency ResultSet Concurrency.
454      * @return <code> PreparedStatement</code> object.
455      * @throws SQLException In case of a database error.
456      */

457     public PreparedStatement prepareStatement(String JavaDoc sql,int resultSetType,
458                         int resultSetConcurrency) throws SQLException{
459     checkValidity();
460         jdbcPreInvoke();
461         return con.prepareStatement(sql, resultSetType, resultSetConcurrency);
462     }
463     
464     /**
465      * Creates a <code> PreparedStatement </code> object for sending
466      * paramterized SQL statements to database
467      *
468      * @param sql SQL Statement
469      * @param resultSetType Type of the ResultSet
470      * @param resultSetConcurrency ResultSet Concurrency.
471      * @param resultSetHoldability ResultSet Holdability.
472      * @return <code> PreparedStatement</code> object.
473      * @throws SQLException In case of a database error.
474      */

475     public PreparedStatement prepareStatement(String JavaDoc sql, int resultSetType,
476                          int resultSetConcurrency,
477                          int resultSetHoldabilty) throws SQLException {
478     checkValidity();
479         jdbcPreInvoke();
480         return con.prepareStatement(sql, resultSetType, resultSetConcurrency,
481                         resultSetHoldabilty);
482     }
483
484     /**
485      * Creates a <code> PreparedStatement </code> object for sending
486      * paramterized SQL statements to database
487      *
488      * @param sql SQL Statement
489      * @param columnNames Name of bound columns.
490      * @return <code> PreparedStatement</code> object.
491      * @throws SQLException In case of a database error.
492      */

493     public PreparedStatement prepareStatement(String JavaDoc sql, String JavaDoc[] columnNames) throws SQLException {
494     checkValidity();
495         jdbcPreInvoke();
496         return con.prepareStatement(sql,columnNames);
497     }
498     
499     /**
500      * Removes the given <code>Savepoint</code> object from the current transaction.
501      *
502      * @param savepoint <code>Savepoint</code> object
503      * @throws SQLException In case of a database error.
504      */

505     public void releaseSavepoint(Savepoint savepoint) throws SQLException {
506     checkValidity();
507         con.releaseSavepoint(savepoint);
508     }
509     
510     /**
511      * Rolls back the changes made in the current transaction.
512      *
513      * @throws SQLException In case of a database error.
514      */

515     public void rollback() throws SQLException {
516     checkValidity();
517         con.rollback();
518     }
519     
520     /**
521      * Rolls back the changes made after the savepoint.
522      *
523      * @throws SQLException In case of a database error.
524      */

525     public void rollback(Savepoint savepoint) throws SQLException {
526     checkValidity();
527         con.rollback(savepoint);
528     }
529         
530     /**
531      * Sets the auto-commmit mode of the <code>Connection</code> object.
532      *
533      * @param autoCommit boolean value indicating the auto-commit mode.
534      * @throws SQLException In case of a database error.
535      */

536     public void setAutoCommit(boolean autoCommit) throws SQLException {
537     checkValidity();
538         con.setAutoCommit(autoCommit);
539         mc.setLastAutoCommitValue(autoCommit);
540     }
541     
542     /**
543      * Sets the catalog name to the <code>Connection</code> object
544      *
545      * @param catalog Catalog name.
546      * @throws SQLException In case of a database error.
547      */

548     public void setCatalog(String JavaDoc catalog) throws SQLException {
549     checkValidity();
550         con.setCatalog(catalog);
551     }
552     
553     /**
554      * Sets the holdability of <code>ResultSet</code> objects created
555      * using this <code>Connection</code> object.
556      *
557      * @param holdability A <code>ResultSet</code> holdability constant
558      * @throws SQLException In case of a database error.
559      */

560     public void setHoldability(int holdability) throws SQLException {
561     checkValidity();
562         con.setHoldability(holdability);
563     }
564     
565     /**
566      * Puts the connection in read-only mode as a hint to the driver to
567      * perform database optimizations.
568      *
569      * @param readOnly true enables read-only mode, false disables it.
570      * @throws SQLException In case of a database error.
571      */

572     public void setReadOnly(boolean readOnly) throws SQLException {
573     checkValidity();
574         con.setReadOnly(readOnly);
575     }
576     
577     /**
578      * Creates and unnamed savepoint and returns an object corresponding to that.
579      *
580      * @return <code>Savepoint</code> object.
581      * @throws SQLException In case of a database error.
582      */

583     public Savepoint setSavepoint() throws SQLException {
584     checkValidity();
585         return con.setSavepoint();
586     }
587     
588     /**
589      * Creates a savepoint with the name and returns an object corresponding to that.
590      *
591      * @param name Name of the savepoint.
592      * @return <code>Savepoint</code> object.
593      * @throws SQLException In case of a database error.
594      */

595     public Savepoint setSavepoint(String JavaDoc name) throws SQLException {
596     checkValidity();
597         return con.setSavepoint(name);
598     }
599     
600     /**
601      * Creates the transaction isolation level.
602      *
603      * @param level transaction isolation level.
604      * @throws SQLException In case of a database error.
605      */

606     public void setTransactionIsolation(int level) throws SQLException {
607     checkValidity();
608         con.setTransactionIsolation(level);
609     }
610     
611     /**
612      * Installs the given <code>Map</code> object as the tyoe map for this
613      * <code> Connection </code> object.
614      *
615      * @param map <code>Map</code> a Map object to install.
616      * @throws SQLException In case of a database error.
617      */

618     public void setTypeMap(Map JavaDoc map) throws SQLException {
619     checkValidity();
620         con.setTypeMap(map);
621     }
622     
623     /**
624      * Checks the validity of this object
625      */

626     protected void checkValidity() throws SQLException {
627         if (isClosed) throw new SQLException ("Connection closed");
628         if (!valid) throw new SQLException ("Invalid Connection");
629         if(active == false) {
630             mc.checkIfActive(this);
631         }
632     }
633     
634     /**
635      * Sets the active flag to true
636      *
637      * @param actv boolean
638      */

639     void setActive(boolean actv) {
640         active = actv;
641     }
642
643     /*
644      * Here this is a no-op. In the LazyEnlistableConnectionHolder, it will
645      * actually fire the lazyEnlist method of LazyEnlistableManagedConnection
646      */

647     protected void jdbcPreInvoke() throws SQLException {
648         if ( myType_ == ConnectionType.LAZY_ASSOCIATABLE ) {
649             performLazyAssociation();
650         } else if ( myType_ == ConnectionType.LAZY_ENLISTABLE ) {
651             performLazyEnlistment();
652         }
653
654     }
655
656     protected void performLazyEnlistment() throws SQLException {
657         try {
658             this.lazyEnlistCm_.lazyEnlist(mc);
659         } catch( ResourceException JavaDoc re ) {
660         String JavaDoc msg = sm.getString(
661                 "jdbc.cannot_enlist" , re.getMessage() +
662                 " Cannnot Enlist ManagedConnection");
663                 
664             SQLException sqle = new SQLException( msg );
665             sqle.initCause( re );
666             throw sqle;
667         }
668
669     }
670     
671     protected void performLazyAssociation() throws SQLException {
672         try {
673             this.lazyAssocCm_.associateConnection( this, mcf_, cxReqInfo_);
674         } catch( ResourceException JavaDoc re ) {
675         String JavaDoc msg = sm.getString(
676                 "jdbc.cannot_enlist", re.getMessage() +
677                 " Cannnot Enlist ManagedConnection");
678                 
679             SQLException sqle = new SQLException( msg);
680             sqle.initCause( re );
681             throw sqle;
682         }
683
684     }
685
686     public void setConnectionType( ConnectionType type ) {
687         myType_ = type;
688     }
689
690     public ConnectionType getConnectionType() {
691         return myType_;
692     }
693
694     public void setLazyAssociatableConnectionManager(
695         javax.resource.spi.LazyAssociatableConnectionManager JavaDoc cm ) {
696         
697         lazyAssocCm_ = cm;
698     }
699
700     public void setLazyEnlistableConnectionManager(
701         javax.resource.spi.LazyEnlistableConnectionManager JavaDoc cm ) {
702         
703         lazyEnlistCm_ = cm;
704     }
705
706     public void setManagedConnection( ManagedConnection con ) {
707         this.mc = con;
708     }
709
710 }
711
Popular Tags