1 45 package org.openejb.resource; 46 47 import java.util.HashMap ; 48 import java.util.HashSet ; 49 import java.util.Set ; 50 51 import javax.resource.spi.ConnectionEvent ; 52 import javax.resource.spi.ConnectionRequestInfo ; 53 import javax.resource.spi.LocalTransaction ; 54 import javax.resource.spi.ManagedConnection ; 55 import javax.resource.spi.ManagedConnectionFactory ; 56 import javax.transaction.Transaction ; 57 58 import org.openejb.OpenEJB; 59 77 public class SharedLocalConnectionManager implements javax.resource.spi.ConnectionManager , 78 javax.resource.spi.ConnectionEventListener , 79 java.io.Serializable { 80 81 private Set connSet; 82 private SpecialHashThreadLocal threadLocal = new SpecialHashThreadLocal(); 83 private HashMap factoryMap = new HashMap (); 84 85 public void init(java.util.Properties props){ 86 } 89 90 public SharedLocalConnectionManager() throws javax.resource.spi.ApplicationServerInternalException { 91 connSet = java.util.Collections.synchronizedSet(new HashSet ()); 92 } 93 public java.lang.Object allocateConnection(ManagedConnectionFactory factory, 94 ConnectionRequestInfo cxRequestInfo) 95 throws javax.resource.ResourceException { 96 97 100 ManagedConnection conn = (ManagedConnection )threadLocal.get(factory); 101 if(conn == null){ 102 conn = factory.matchManagedConnections(connSet, null, cxRequestInfo); 103 if (conn != null) 104 connSet.remove(conn); 105 else { conn = factory.createManagedConnection(null, cxRequestInfo); 107 conn.addConnectionEventListener(this); 108 } 109 conn.getLocalTransaction().begin(); 110 111 try{ 112 119 Transaction tx = OpenEJB.getTransactionManager().getTransaction(); 120 if(tx!=null) 121 tx.registerSynchronization(new Synchronizer(conn.getLocalTransaction())); 122 }catch(javax.transaction.SystemException se){ 123 throw new javax.resource.spi.ApplicationServerInternalException ("Can not obtain a Transaction object from TransactionManager. "+se.getMessage()); 124 }catch(javax.transaction.RollbackException re){ 125 throw new javax.resource.spi.ApplicationServerInternalException ("Can not register org.openejb.resource.LocalTransacton with transaciton manager. Transaction has already been rolled back"+re.getMessage()); 126 } 127 128 threadLocal.put(factory,conn); 129 } 130 Object handle = conn.getConnection(null, cxRequestInfo); 132 return handle; 133 } 134 135 public void connectionClosed(ConnectionEvent event){ 136 try{ 137 if(OpenEJB.getTransactionManager().getTransaction()==null){ 138 ManagedConnection conn = (ManagedConnection )event.getSource(); 139 conn.getLocalTransaction().commit(); 140 this.cleanup(conn); 141 } 142 }catch(javax.transaction.SystemException se){ 143 }catch(javax.resource.ResourceException re){ 146 } 148 } 149 150 public void connectionErrorOccurred(ConnectionEvent event){ 151 ManagedConnection conn = (ManagedConnection )event.getSource(); 152 ManagedConnectionFactory mcf = (ManagedConnectionFactory )threadLocal.getKey(conn); 155 try{ 156 conn.destroy(); 157 if ( threadLocal.get(mcf)==conn ) threadLocal.put(mcf,null); 158 }catch(javax.resource.ResourceException re){ 159 } 161 } 162 163 public void localTransactionCommitted(ConnectionEvent event){ 164 cleanup((ManagedConnection )event.getSource()); 165 } 166 167 public void localTransactionRolledback(ConnectionEvent event){ 168 cleanup((ManagedConnection )event.getSource()); 169 } 170 171 private void cleanup(ManagedConnection conn){ 172 if(conn!=null){ 173 ManagedConnectionFactory mcf = (ManagedConnectionFactory )threadLocal.getKey(conn); 176 try{ 177 conn.cleanup(); 178 connSet.add(conn); 179 180 }catch(javax.resource.ResourceException re){ 181 try{ 182 conn.destroy(); 184 }catch(javax.resource.ResourceException re2){ 185 } 187 } 188 threadLocal.put(mcf,null); 189 } 190 } 191 public void localTransactionStarted(ConnectionEvent event){ 192 } 194 195 196 static class Synchronizer implements javax.transaction.Synchronization { 197 LocalTransaction localTx; 198 199 public Synchronizer(LocalTransaction lt){ 200 localTx = lt; 201 } 202 public void beforeCompletion(){ 203 } 204 205 public void afterCompletion(int status){ 206 if ( status == javax.transaction.Status.STATUS_COMMITTED ){ 207 try{ 208 localTx.commit(); 209 } catch ( javax.resource.ResourceException re ) { 210 throw new RuntimeException ("JDBC driver failed to commit transaction. "+ re.getMessage()); 211 } 212 } else { 213 try{ 214 localTx.rollback(); 215 } catch ( javax.resource.ResourceException re ) { 216 throw new RuntimeException ("JDBC driver failed to rollback transaction. "+ re.getMessage()); 217 } 218 } 219 } 220 } 221 222 228 static class SpecialHashThreadLocal extends org.openejb.util.HashThreadLocal{ 229 HashMap keyMap = new HashMap (); 230 public synchronized void put(Object key, Object value){ 231 if(!keyMap.containsKey(key))keyMap.put(value,key); 232 super.put(key,value); 233 } 234 public synchronized Object getKey(Object value){ 235 return keyMap.get(value); 236 } 237 } 238 239 }
| Popular Tags
|