1 23 package com.sun.enterprise.resource; 24 25 import javax.transaction.xa.*; 26 import javax.resource.spi.*; 27 import com.sun.enterprise.log.Log; 28 import com.sun.enterprise.*; 29 import com.sun.enterprise.distributedtx.*; 30 import java.util.*; 31 import java.util.logging.*; 32 import com.sun.logging.*; 33 import javax.resource.ResourceException ; 34 import com.sun.jts.CosTransactions.Configuration; 35 36 40 public class ConnectorXAResource implements XAResource { 41 42 private Object userHandle; 43 private ResourceSpec spec; 44 private String poolName; 45 private ResourceAllocator alloc; 46 private PoolManager poolMgr; 47 private ManagedConnection localConnection; 48 private ClientSecurityInfo info; 49 private ConnectionEventListener listener; 50 private ResourceHandle localHandle_; 51 52 private static Hashtable listenerTable = new Hashtable(); 53 54 private static ThreadLocal sharedConnections = new ThreadLocal (); 55 56 57 60 static Logger _logger = LogDomains.getLogger(LogDomains.RSR_LOGGER); 61 62 63 64 public ConnectorXAResource(ResourceHandle handle, 65 ResourceSpec spec, 66 ResourceAllocator alloc, 67 ClientSecurityInfo info ) { 68 69 this.poolMgr = Switch.getSwitch().getPoolManager(); 71 this.userHandle = null; 72 this.spec = spec; 73 this.poolName = spec.getConnectionPoolName(); 74 this.alloc = alloc; 75 this.info = info; 76 localConnection = (ManagedConnection) handle.getResource(); 77 localHandle_ = handle; 78 } 79 80 public void setUserHandle(Object userHandle) { 81 this.userHandle = userHandle; 82 } 83 84 private void handleResourceException(Exception ex) 85 throws XAException { 86 _logger.log(Level.SEVERE,"poolmgr.system_exception",ex); 87 XAException xae = new XAException(ex.toString()); 88 xae.errorCode = XAException.XAER_RMERR; 89 throw xae; 90 } 91 92 public void commit(Xid xid, boolean onePhase) throws XAException { 93 try { 94 ResourceHandle handle = getResourceHandle(); 95 ManagedConnection mc = (ManagedConnection) handle.getResource(); 96 mc.getLocalTransaction().commit(); 97 } catch (Exception ex) { 98 handleResourceException(ex); 99 } 100 resetAssociation(); 101 } 102 103 public void start(Xid xid, int flags) throws XAException { 104 try { 105 ResourceHandle handle = getResourceHandle(); 106 if ( ! localHandle_.equals( handle) ) { 107 ManagedConnection mc = (ManagedConnection) handle.getResource(); 108 mc.associateConnection(userHandle); 109 LocalTxConnectionEventListener l = 110 (LocalTxConnectionEventListener) handle.getListener(); 111 _logger.log(Level.FINEST, "connection_sharing_start", userHandle); 112 l.associateHandle(userHandle, localHandle_); 113 } 114 } catch (Exception ex) { 115 handleResourceException(ex); 116 } 117 } 118 119 public void end(Xid xid, int flags) throws XAException { 120 _logger.log(Level.FINEST, "connection_sharing_end"); 122 } 123 124 private void resetAssociation() throws XAException{ 125 try { 126 ResourceHandle handle = getResourceHandle(); 127 128 LocalTxConnectionEventListener l = (LocalTxConnectionEventListener)handle.getListener(); 129 Map associatedHandles = l.getAssociatedHandles(); 131 if(associatedHandles != null ){ 132 Set userHandles = associatedHandles.keySet(); 133 for(Object userHandleObject : userHandles ){ 134 ResourceHandle associatedHandle = (ResourceHandle)associatedHandles.get(userHandleObject); 135 ManagedConnection associatedConnection = (ManagedConnection)associatedHandle.getResource(); 136 associatedConnection.associateConnection(userHandleObject); 137 _logger.log(Level.FINEST, "connection_sharing_reset_association", userHandleObject); 138 } 139 associatedHandles.clear(); 141 } 142 143 } catch (Exception ex) { 144 handleResourceException(ex); 145 } 146 } 147 148 149 public void forget(Xid xid) throws XAException { 150 _logger.fine("Well, forget is called for xid :"+xid); 151 } 153 154 public int getTransactionTimeout() throws XAException { 155 return 0; 156 } 157 158 public boolean isSameRM(XAResource other) throws XAException { 159 if (this == other) return true; 160 if (other == null) return false; 161 if (other instanceof ConnectorXAResource) { 162 ConnectorXAResource obj = (ConnectorXAResource) other; 163 return (this.spec.equals(obj.spec) && 164 this.info.equals(obj.info)); 165 } else { 166 return false; 167 } 168 } 169 170 public int prepare(Xid xid) throws XAException { 171 return Configuration.LAO_PREPARE_OK; 172 } 173 174 public Xid[] recover(int flag) throws XAException { 175 return new Xid[0]; 176 } 177 178 public void rollback(Xid xid) throws XAException { 179 try { 180 ResourceHandle handle = getResourceHandle(); 181 ManagedConnection mc = (ManagedConnection) handle.getResource(); 182 mc.getLocalTransaction().rollback(); 183 } catch (Exception ex) { 184 handleResourceException(ex); 185 } 186 resetAssociation(); 187 } 188 189 public boolean setTransactionTimeout(int seconds) throws XAException { 190 return false; 191 } 192 193 public static void freeListener(ManagedConnection mc) { 194 listenerTable.remove(mc); 195 } 196 197 public static void addListener(ManagedConnection mc, ConnectionEventListener l) { 198 listenerTable.put(mc,l); 199 } 200 201 private ResourceHandle getResourceHandle() throws PoolingException { 202 try { 203 ResourceHandle h = null; 204 J2EETransactionManager txMgr = Switch.getSwitch().getTransactionManager(); 205 J2EETransaction j2eetran = (J2EETransaction) txMgr.getTransaction(); 206 if (j2eetran == null) { h = localHandle_; } else { 209 h = j2eetran.getNonXAResource(); 210 } 211 if (h.getResourceState().isUnenlisted()) { 212 ManagedConnection mc = (ManagedConnection) h.getResource(); 213 mc.getLocalTransaction().begin(); 216 } 217 return h; 218 } catch (Exception ex) { 219 _logger.log(Level.SEVERE,"poolmgr.system_exception",ex); 220 throw new PoolingException(ex.toString(), ex); 221 } 222 } 223 224 } 225 | Popular Tags |