1 22 package org.jboss.resource.adapter.jdbc.xa; 23 24 import java.sql.SQLException ; 25 import java.util.Properties ; 26 27 import javax.resource.ResourceException ; 28 import javax.resource.spi.LocalTransaction ; 29 import javax.sql.XAConnection ; 30 import javax.transaction.xa.XAException ; 31 import javax.transaction.xa.XAResource ; 32 import javax.transaction.xa.Xid ; 33 34 import org.jboss.resource.JBossResourceException; 35 import org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection; 36 37 45 public class XAManagedConnection extends BaseWrapperManagedConnection implements XAResource 46 { 47 protected final XAConnection xaConnection; 48 49 protected final XAResource xaResource; 50 51 protected Xid currentXid; 52 53 public XAManagedConnection(XAManagedConnectionFactory mcf, XAConnection xaConnection, Properties props, 54 int transactionIsolation, int psCacheSize) throws SQLException 55 { 56 super(mcf, xaConnection.getConnection(), props, transactionIsolation, psCacheSize); 57 this.xaConnection = xaConnection; 58 xaConnection.addConnectionEventListener(new javax.sql.ConnectionEventListener () 59 { 60 public void connectionClosed(javax.sql.ConnectionEvent ce) 61 { 62 } 64 65 public void connectionErrorOccurred(javax.sql.ConnectionEvent ce) 66 { 67 SQLException ex = ce.getSQLException(); 68 broadcastConnectionError(ex); 69 } 70 }); 71 this.xaResource = xaConnection.getXAResource(); 72 } 73 74 protected void broadcastConnectionError(SQLException e) 75 { 76 super.broadcastConnectionError(e); 77 } 78 79 public LocalTransaction getLocalTransaction() throws ResourceException 80 { 81 throw new JBossResourceException("xa tx only!"); 82 } 83 84 public XAResource getXAResource() throws ResourceException 85 { 86 return this; 87 } 88 89 public void destroy() throws ResourceException 90 { 91 try 92 { 93 super.destroy(); 94 } 95 finally 96 { 97 try 98 { 99 xaConnection.close(); 100 } 101 catch (SQLException e) 102 { 103 checkException(e); 104 } 105 } 106 } 107 108 public void start(Xid xid, int flags) throws XAException 109 { 110 try 111 { 112 checkState(); 113 } 114 catch (SQLException e) 115 { 116 getLog().warn("Error setting state ", e); 117 } 118 119 try 120 { 121 xaResource.start(xid, flags); 122 123 }catch(XAException e) 124 { 125 if(isFailedXA(e.errorCode)) 128 { 129 broadcastConnectionError(e); 130 } 131 132 throw e; 133 } 134 135 synchronized (stateLock) 136 { 137 currentXid = xid; 138 inManagedTransaction = true; 139 } 140 } 141 142 public void end(Xid xid, int flags) throws XAException 143 { 144 145 try 146 { 147 xaResource.end(xid, flags); 148 149 }catch(XAException e) 150 { 151 broadcastConnectionError(e); 152 throw e; 153 } 154 155 156 synchronized (stateLock) 159 { 160 if (currentXid != null && currentXid.equals(xid)) 161 { 162 inManagedTransaction = false; 163 currentXid = null; 164 } 165 } 166 } 167 168 public int prepare(Xid xid) throws XAException 169 { 170 return xaResource.prepare(xid); 171 } 172 173 public void commit(Xid xid, boolean onePhase) throws XAException 174 { 175 xaResource.commit(xid, onePhase); 176 } 177 178 public void rollback(Xid xid) throws XAException 179 { 180 xaResource.rollback(xid); 181 } 182 183 public void forget(Xid xid) throws XAException 184 { 185 xaResource.forget(xid); 186 } 187 188 public Xid [] recover(int flag) throws XAException 189 { 190 return xaResource.recover(flag); 191 } 192 193 public boolean isSameRM(XAResource other) throws XAException 194 { 195 Boolean overrideValue = ((XAManagedConnectionFactory) mcf).getIsSameRMOverrideValue(); 196 if (overrideValue != null) 197 { 198 return overrideValue.booleanValue(); 199 } 200 201 return (other instanceof XAManagedConnection) 203 ? xaResource.isSameRM(((XAManagedConnection) other).xaResource) 204 : xaResource.isSameRM(other); 205 } 206 207 public int getTransactionTimeout() throws XAException 208 { 209 return xaResource.getTransactionTimeout(); 210 } 211 212 public boolean setTransactionTimeout(int seconds) throws XAException 213 { 214 return xaResource.setTransactionTimeout(seconds); 215 } 216 217 private boolean isFailedXA(int errorCode) 218 { 219 220 return (errorCode == XAException.XAER_RMERR || errorCode == XAException.XAER_RMFAIL); 221 } 222 } 223 | Popular Tags |