1 45 package org.openejb.resource.jdbc; 46 47 import javax.resource.ResourceException ; 48 import javax.resource.spi.ConnectionEvent ; 49 import javax.resource.spi.ConnectionEventListener ; 50 import javax.resource.spi.ConnectionRequestInfo ; 51 import javax.resource.spi.ManagedConnection ; 52 import javax.resource.spi.ManagedConnectionFactory ; 53 import javax.resource.spi.ManagedConnectionMetaData ; 54 import java.io.PrintWriter ; 55 import java.sql.Connection ; 56 import java.util.ArrayList ; 57 import java.util.HashSet ; 58 import java.util.List ; 59 import java.util.Set ; 60 61 public class JdbcManagedConnection implements ManagedConnection { 62 63 private final JdbcConnectionRequestInfo requestInfo; 64 private final JdbcManagedConnectionMetaData metaData; 65 private final JdbcLocalTransaction localTransaction; 66 private final List jdbcConnections = new ArrayList (); 67 private final Set listeners; 68 69 private Connection sqlConn; 70 private PrintWriter logWriter; 71 72 public JdbcManagedConnection(ManagedConnectionFactory managedFactory, java.sql.Connection sqlConn, JdbcConnectionRequestInfo rxInfo) 73 throws javax.resource.spi.ResourceAdapterInternalException { 74 listeners = java.util.Collections.synchronizedSet(new HashSet ()); 75 this.requestInfo = rxInfo; 76 this.sqlConn = sqlConn; 77 try { 78 logWriter = managedFactory.getLogWriter(); 79 } catch (ResourceException e) { 80 throw new RuntimeException (e); 81 } 82 try { 83 metaData = new JdbcManagedConnectionMetaData(sqlConn.getMetaData()); 84 } catch (java.sql.SQLException sqlE) { 85 throw new javax.resource.spi.ResourceAdapterInternalException ("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 getSQLConnection() { 91 return sqlConn; 92 } 93 94 protected JdbcConnectionRequestInfo getRequestInfo() { 95 return requestInfo; 96 } 97 98 public void addConnectionEventListener(ConnectionEventListener listener) { 99 listeners.add(listener); 100 } 101 102 public void associateConnection(java.lang.Object connection) throws javax.resource.ResourceException { 103 if (connection instanceof JdbcConnection) { 104 JdbcConnection jdbcConn = (JdbcConnection) connection; 105 jdbcConn.associate(this); 106 } else { 107 throw new javax.resource.ResourceException ("Connection object is the wrong type. It must be an instance of JdbcConnection"); 108 } 109 } 110 111 114 public void cleanup() throws javax.resource.ResourceException { 115 synchronized (jdbcConnections) { 116 Object [] 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 { 127 cleanup(); 128 try { 129 sqlConn.close(); 130 } catch (java.sql.SQLException sqlE) { 131 throw new javax.resource.spi.ResourceAdapterInternalException ("Problem attempting to close physical JDBC connection", ErrorCode.JDBC_0003); 132 } 133 sqlConn = null; 134 listeners.clear(); 135 } 136 137 142 public java.lang.Object getConnection(javax.security.auth.Subject subject, ConnectionRequestInfo cxRequestInfo) throws javax.resource.ResourceException { 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 getLocalTransaction() throws javax.resource.ResourceException { 151 return localTransaction; 152 } 153 154 public java.io.PrintWriter getLogWriter() throws javax.resource.ResourceException { 155 return logWriter; 156 } 157 158 public ManagedConnectionMetaData getMetaData() throws javax.resource.ResourceException { 159 return metaData; 160 } 161 162 public javax.transaction.xa.XAResource getXAResource() throws javax.resource.ResourceException { 163 throw new javax.resource.NotSupportedException ("Method not implemented"); 164 } 165 166 public void removeConnectionEventListener(ConnectionEventListener listener) { 167 listeners.remove(listener); 168 } 169 170 public void setLogWriter(java.io.PrintWriter out) throws javax.resource.ResourceException { 171 logWriter = out; 172 } 173 174 protected void localTransactionCommitted() { 175 ConnectionEvent event = new ConnectionEvent (this, ConnectionEvent.LOCAL_TRANSACTION_COMMITTED); 176 Object [] elements = listeners.toArray(); 177 for (int i = 0; i < elements.length; i++) { 178 ConnectionEventListener eventListener = (ConnectionEventListener ) elements[i]; 179 eventListener.localTransactionCommitted(event); 180 } 181 } 182 183 protected void localTransactionRolledback() { 184 ConnectionEvent event = new ConnectionEvent (this, ConnectionEvent.LOCAL_TRANSACTION_ROLLEDBACK); 185 Object [] elements = listeners.toArray(); 186 for (int i = 0; i < elements.length; i++) { 187 ConnectionEventListener eventListener = (ConnectionEventListener ) elements[i]; 188 eventListener.localTransactionRolledback(event); 189 } 190 } 191 192 protected void localTransactionStarted() { 193 ConnectionEvent event = new ConnectionEvent (this, ConnectionEvent.LOCAL_TRANSACTION_STARTED); 194 Object [] elements = listeners.toArray(); 195 for (int i = 0; i < elements.length; i++) { 196 ConnectionEventListener eventListener = (ConnectionEventListener ) elements[i]; 197 eventListener.localTransactionStarted(event); 198 } 199 } 200 201 protected void connectionErrorOccurred(JdbcConnection jdbcConn, java.sql.SQLException 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 temp = sqlE; 210 while ((temp = sqlE.getNextException()) != null) { 211 temp.printStackTrace(logWriter); 212 } 213 } 214 215 ConnectionEvent event = new ConnectionEvent (this, ConnectionEvent.CONNECTION_ERROR_OCCURRED, sqlE); 216 Object [] elements = listeners.toArray(); 217 for (int i = 0; i < elements.length; i++) { 218 ConnectionEventListener eventListener = (ConnectionEventListener ) elements[i]; 219 eventListener.connectionErrorOccurred(event); 220 } 221 } 222 223 228 protected void connectionClose(JdbcConnection jdbcConn) { 229 synchronized (jdbcConnections) { 230 jdbcConn.invalidate(); 231 jdbcConnections.remove(jdbcConn); 232 ConnectionEvent event = new ConnectionEvent (this, ConnectionEvent.CONNECTION_CLOSED); 233 Object [] elements = listeners.toArray(); 234 for (int i = 0; i < elements.length; i++) { 235 ConnectionEventListener eventListener = (ConnectionEventListener ) elements[i]; 236 eventListener.connectionClosed(event); 237 } 238 } 239 } 240 241 public String toString() { 242 return "JdbcManagedConnection (" + sqlConn.toString() + ")"; 243 } 244 } | Popular Tags |