KickJava   Java API By Example, From Geeks To Geeks.

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


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 "OpenEJB" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of The OpenEJB Group. For written permission,
18  * please contact info@openejb.org.
19  *
20  * 4. Products derived from this Software may not be called "OpenEJB"
21  * nor may "OpenEJB" appear in their names without prior written
22  * permission of The OpenEJB Group. OpenEJB is a registered
23  * trademark of The OpenEJB Group.
24  *
25  * 5. Due credit should be given to the OpenEJB Project
26  * (http://openejb.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP 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  * THE OPENEJB GROUP 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 2005 (C) The OpenEJB Group. All Rights Reserved.
42  *
43  * $Id: JdbcManagedConnection.java 2096 2005-08-23 20:10:37Z dblevins $
44  */

45 package org.openejb.resource.jdbc;
46
47 import javax.resource.ResourceException JavaDoc;
48 import javax.resource.spi.ConnectionEvent JavaDoc;
49 import javax.resource.spi.ConnectionEventListener JavaDoc;
50 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
51 import javax.resource.spi.ManagedConnection JavaDoc;
52 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
53 import javax.resource.spi.ManagedConnectionMetaData JavaDoc;
54 import java.io.PrintWriter JavaDoc;
55 import java.sql.Connection JavaDoc;
56 import java.util.ArrayList JavaDoc;
57 import java.util.HashSet JavaDoc;
58 import java.util.List JavaDoc;
59 import java.util.Set JavaDoc;
60
61 public class JdbcManagedConnection implements ManagedConnection JavaDoc {
62
63     private final JdbcConnectionRequestInfo requestInfo;
64     private final JdbcManagedConnectionMetaData metaData;
65     private final JdbcLocalTransaction localTransaction;
66     private final List JavaDoc jdbcConnections = new ArrayList JavaDoc();
67     private final Set JavaDoc listeners;
68
69     private Connection JavaDoc sqlConn;
70     private PrintWriter JavaDoc logWriter;
71
72     public JdbcManagedConnection(ManagedConnectionFactory JavaDoc managedFactory, java.sql.Connection JavaDoc sqlConn, JdbcConnectionRequestInfo rxInfo)
73             throws javax.resource.spi.ResourceAdapterInternalException JavaDoc {
74         listeners = java.util.Collections.synchronizedSet(new HashSet JavaDoc());
75         this.requestInfo = rxInfo;
76         this.sqlConn = sqlConn;
77         try {
78             logWriter = managedFactory.getLogWriter();
79         } catch (ResourceException JavaDoc e) {
80             throw new RuntimeException JavaDoc(e);
81         }
82         try {
83             metaData = new JdbcManagedConnectionMetaData(sqlConn.getMetaData());
84         } catch (java.sql.SQLException JavaDoc sqlE) {
85             throw new javax.resource.spi.ResourceAdapterInternalException JavaDoc("Problem while attempting to access meta data from physical connection", ErrorCode.JDBC_0004);
86         }
87         localTransaction = new JdbcLocalTransaction(this);
88     }
89
90     protected java.sql.Connection JavaDoc getSQLConnection() {
91         return sqlConn;
92     }
93
94     protected JdbcConnectionRequestInfo getRequestInfo() {
95         return requestInfo;
96     }
97
98     public void addConnectionEventListener(ConnectionEventListener JavaDoc listener) {
99         listeners.add(listener);
100     }
101
102     public void associateConnection(java.lang.Object JavaDoc connection) throws javax.resource.ResourceException JavaDoc {
103         if (connection instanceof JdbcConnection) {
104             JdbcConnection jdbcConn = (JdbcConnection) connection;
105             jdbcConn.associate(this);
106         } else {
107             throw new javax.resource.ResourceException JavaDoc("Connection object is the wrong type. It must be an instance of JdbcConnection");
108         }
109     }
110
111     /**
112      * This method will invalidate any JdbcConnection handles that have not already been invalidated (they self invalidate when they are explicitly closed).
113      */

114     public void cleanup() throws javax.resource.ResourceException JavaDoc {
115         synchronized (jdbcConnections) {
116             Object JavaDoc[] connectionHandles = jdbcConnections.toArray();
117             for (int i = 0; i < connectionHandles.length; i++) {
118                 JdbcConnection handle = (JdbcConnection) connectionHandles[i];
119                 handle.invalidate();
120             }
121             jdbcConnections.clear();
122             localTransaction.cleanup();
123         }
124     }
125
126     public void destroy() throws javax.resource.ResourceException JavaDoc {
127         cleanup();
128         try {
129             sqlConn.close();
130         } catch (java.sql.SQLException JavaDoc sqlE) {
131             throw new javax.resource.spi.ResourceAdapterInternalException JavaDoc("Problem attempting to close physical JDBC connection", ErrorCode.JDBC_0003);
132         }
133         sqlConn = null;
134         listeners.clear();
135     }
136
137     /*
138     * Returns an application level connection handle in the form of a JdbcConnection object
139     * which implements the java.sql.Connection interface and wrappers the physical JDBC connection.
140     *
141     */

142     public java.lang.Object JavaDoc getConnection(javax.security.auth.Subject JavaDoc subject, ConnectionRequestInfo JavaDoc cxRequestInfo) throws javax.resource.ResourceException JavaDoc {
143         synchronized (jdbcConnections) {
144             JdbcConnection jdbcCon = new JdbcConnection(this, sqlConn);
145             jdbcConnections.add(jdbcCon);
146             return jdbcCon;
147         }
148     }
149
150     public javax.resource.spi.LocalTransaction JavaDoc getLocalTransaction() throws javax.resource.ResourceException JavaDoc {
151         return localTransaction;
152     }
153
154     public java.io.PrintWriter JavaDoc getLogWriter() throws javax.resource.ResourceException JavaDoc {
155         return logWriter;
156     }
157
158     public ManagedConnectionMetaData JavaDoc getMetaData() throws javax.resource.ResourceException JavaDoc {
159         return metaData;
160     }
161
162     public javax.transaction.xa.XAResource JavaDoc getXAResource() throws javax.resource.ResourceException JavaDoc {
163         throw new javax.resource.NotSupportedException JavaDoc("Method not implemented");
164     }
165
166     public void removeConnectionEventListener(ConnectionEventListener JavaDoc listener) {
167         listeners.remove(listener);
168     }
169
170     public void setLogWriter(java.io.PrintWriter JavaDoc out) throws javax.resource.ResourceException JavaDoc {
171         logWriter = out;
172     }
173
174     protected void localTransactionCommitted() {
175         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(this, ConnectionEvent.LOCAL_TRANSACTION_COMMITTED);
176         Object JavaDoc[] elements = listeners.toArray();
177         for (int i = 0; i < elements.length; i++) {
178             ConnectionEventListener JavaDoc eventListener = (ConnectionEventListener JavaDoc) elements[i];
179             eventListener.localTransactionCommitted(event);
180         }
181     }
182
183     protected void localTransactionRolledback() {
184         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(this, ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK);
185         Object JavaDoc[] elements = listeners.toArray();
186         for (int i = 0; i < elements.length; i++) {
187             ConnectionEventListener JavaDoc eventListener = (ConnectionEventListener JavaDoc) elements[i];
188             eventListener.localTransactionRolledback(event);
189         }
190     }
191
192     protected void localTransactionStarted() {
193         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(this, ConnectionEvent.LOCAL_TRANSACTION_STARTED);
194         Object JavaDoc[] elements = listeners.toArray();
195         for (int i = 0; i < elements.length; i++) {
196             ConnectionEventListener JavaDoc eventListener = (ConnectionEventListener JavaDoc) elements[i];
197             eventListener.localTransactionStarted(event);
198         }
199     }
200
201     protected void connectionErrorOccurred(JdbcConnection jdbcConn, java.sql.SQLException JavaDoc sqlE) {
202
203         if (logWriter != null) {
204             logWriter.print("\nJdbcConnection Error: On java.sql.Connection (");
205             logWriter.print(jdbcConn);
206             logWriter.println(")");
207             logWriter.println("Exception Stack trace follows:");
208             sqlE.printStackTrace(logWriter);
209             java.sql.SQLException JavaDoc temp = sqlE;
210             while ((temp = sqlE.getNextException()) != null) {
211                 temp.printStackTrace(logWriter);
212             }
213         }
214
215         ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(this, ConnectionEvent.CONNECTION_ERROR_OCCURRED, sqlE);
216         Object JavaDoc[] elements = listeners.toArray();
217         for (int i = 0; i < elements.length; i++) {
218             ConnectionEventListener JavaDoc eventListener = (ConnectionEventListener JavaDoc) elements[i];
219             eventListener.connectionErrorOccurred(event);
220         }
221     }
222
223     /**
224      * Invoked by the JdbcConneciton when its close() method is called.
225      * This method invalidates the JdbcConnection handle, removes it from
226      * the list of active handles and notifies all the ConnectionEventListeners.
227      */

228     protected void connectionClose(JdbcConnection jdbcConn) {
229         synchronized (jdbcConnections) {
230             jdbcConn.invalidate();
231             jdbcConnections.remove(jdbcConn);
232             ConnectionEvent JavaDoc event = new ConnectionEvent JavaDoc(this, ConnectionEvent.CONNECTION_CLOSED);
233             Object JavaDoc[] elements = listeners.toArray();
234             for (int i = 0; i < elements.length; i++) {
235                 ConnectionEventListener JavaDoc eventListener = (ConnectionEventListener JavaDoc) elements[i];
236                 eventListener.connectionClosed(event);
237             }
238         }
239     }
240
241     public String JavaDoc toString() {
242         return "JdbcManagedConnection (" + sqlConn.toString() + ")";
243     }
244 }
Popular Tags