1 23 24 28 29 package com.sun.jts.jta; 30 31 import java.util.*; 32 import org.omg.CosTransactions.*; 33 import javax.transaction.*; 34 import javax.transaction.xa.*; 35 36 import com.sun.jts.CosTransactions.Configuration; 37 import com.sun.jts.CosTransactions.ControlImpl; 38 import com.sun.jts.CosTransactions.TopCoordinator; 39 import com.sun.jts.CosTransactions.GlobalTID; 40 41 import javax.transaction.Synchronization ; 42 import javax.transaction.SystemException ; 43 44 import org.omg.CosTransactions.Status; 45 import org.omg.CORBA.TRANSACTION_ROLLEDBACK ; 46 import org.omg.CORBA.INVALID_TRANSACTION ; 47 import org.omg.CORBA.NO_PERMISSION ; 48 import java.util.logging.Logger ; 49 import java.util.logging.Level ; 50 import com.sun.logging.LogDomains; 51 58 public class TransactionImpl implements Transaction { 59 60 63 private Control control; 64 65 private GlobalTID gtid; 66 67 private TransactionState tranState = null; 68 69 private static TransactionManagerImpl tm = TransactionManagerImpl.getTransactionManagerImpl(); 70 73 static Logger _logger = LogDomains.getLogger(LogDomains.TRANSACTION_LOGGER); 74 75 private long startTime; 77 79 public TransactionImpl(TransactionManagerImpl tm, 80 Control control, GlobalTID gtid) 81 throws SystemException { 82 83 this.control = control; 84 this.gtid = gtid; 85 this.tm = tm; 86 startTime=System.currentTimeMillis(); 88 } 90 91 public TransactionImpl(Control control, GlobalTID gtid) 92 throws SystemException { 93 94 this.control = control; 95 this.gtid = gtid; 96 startTime=System.currentTimeMillis(); 97 } 98 99 102 Control getControl() { 103 return control; 104 } 105 106 110 113 public void commit() throws HeuristicMixedException, 114 RollbackException, HeuristicRollbackException, IllegalStateException , 115 SecurityException , SystemException 116 { 117 try { 118 if (Configuration.isLocalFactory()) { 119 ((ControlImpl) control).get_localTerminator().commit(true); 120 } else { 121 control.get_terminator().commit(true); 122 } 123 } catch (TRANSACTION_ROLLEDBACK ex) { 124 throw new RollbackException(); 125 } catch (INVALID_TRANSACTION ex) { 126 throw new IllegalStateException (); 127 } catch (HeuristicMixed ex) { 128 throw new HeuristicMixedException(); 129 } catch (HeuristicHazard ex) { 130 throw new HeuristicMixedException(); 131 } catch (NO_PERMISSION ex) { 132 throw new SecurityException (); 133 } catch (Unavailable ex) { 134 SystemException sException = new SystemException (); 135 sException.initCause(ex); 136 throw sException; 137 } catch (Exception ex) { 138 SystemException sException = new SystemException (); 139 sException.initCause(ex); 140 throw sException; 141 } 142 143 } 144 145 146 149 public void rollback() 150 throws IllegalStateException , SystemException { 151 152 try { 153 if (Configuration.isLocalFactory()) { 154 ((ControlImpl) control).get_localTerminator().rollback(); 155 } else { 156 control.get_terminator().rollback(); 157 } 158 } catch (INVALID_TRANSACTION ex) { 159 throw new IllegalStateException (); 160 } catch (TRANSACTION_ROLLEDBACK ex) { 161 throw new IllegalStateException (); 162 } catch (Unavailable ex) { 163 SystemException sException = new SystemException (); 164 sException.initCause(ex); 165 throw sException; 166 } catch (Exception ex) { 167 SystemException sException = new SystemException (); 168 sException.initCause(ex); 169 throw sException; 170 } 171 } 172 173 174 181 public boolean enlistResource(XAResource res) 182 throws RollbackException, IllegalStateException , 183 SystemException { 184 185 int status = getStatus(); 186 if (status != javax.transaction.Status.STATUS_ACTIVE && 187 status != javax.transaction.Status.STATUS_MARKED_ROLLBACK) { 188 throw new IllegalStateException (); 189 } 190 try{ 192 if(tm.getXAResourceTimeOut() > 0) 193 res.setTransactionTimeout(tm.getXAResourceTimeOut()); 194 }catch(Exception ex){ 195 _logger.log(Level.WARNING,"jts.error_while_setting_xares_txn_timeout",ex); 196 } 197 try { 199 if (tranState == null) { 200 tranState = new TransactionState(gtid, this); 201 } 204 tranState.startAssociation(res, control, status); 205 if (status == javax.transaction.Status.STATUS_MARKED_ROLLBACK) { 206 throw new RollbackException(); 207 } 208 return true; 209 } catch (XAException ex) { 210 _logger.log(Level.WARNING,"jts.resource_outside_transaction",ex); 211 if (ex.errorCode == XAException.XAER_OUTSIDE) { 212 throw new IllegalStateException (); 213 } 214 throw new SystemException (); 217 } 218 219 } 220 221 public boolean delistResource(XAResource res, int flags) 222 throws IllegalStateException , SystemException { 223 224 231 232 try { 233 if (tranState == null) { 235 throw new IllegalStateException (); 237 } 238 if (tranState.containsXAResource(res) == false) { 239 throw new IllegalStateException (); 240 } 241 tranState.endAssociation(res, flags); 242 if ((flags & XAResource.TMFAIL) != 0) { 243 setRollbackOnly(); 245 } 246 return true; 247 } catch (XAException ex) { 248 throw new SystemException (); 249 } 250 } 251 252 public int getStatus() throws SystemException { 253 Status status; 255 try { 256 if (Configuration.isLocalFactory()) { 257 status = ((ControlImpl) control).get_localCoordinator().get_status(); 258 } else { 259 status = control.get_coordinator().get_status(); 260 } 261 return TransactionManagerImpl.mapStatus(status); 262 } catch (TRANSACTION_ROLLEDBACK ex) { 263 return javax.transaction.Status.STATUS_NO_TRANSACTION; 264 } catch (INVALID_TRANSACTION ex) { 265 return javax.transaction.Status.STATUS_NO_TRANSACTION; 266 } catch (Unavailable ex) { 267 return javax.transaction.Status.STATUS_NO_TRANSACTION; 268 } catch (Exception ex) { 269 _logger.log(Level.WARNING,"jts.unexpected_error_in_getstatus",ex); 270 throw new SystemException (); 271 } 272 } 273 274 public boolean equals(Object object) { 275 if ((object instanceof TransactionImpl) == false) { 276 return false; 277 } else if (object == this) { 278 return true; 279 } else { 280 return gtid.equals(((TransactionImpl) object).gtid); 281 } 282 } 283 284 public int hashCode() { 285 return gtid.hashCode(); 286 } 287 288 public void registerSynchronization(Synchronization sync) 289 throws RollbackException, IllegalStateException , 290 SystemException { 291 292 int status = getStatus(); 293 if (status == javax.transaction.Status.STATUS_MARKED_ROLLBACK) { 294 throw new RollbackException(); 295 } 296 if (status != javax.transaction.Status.STATUS_ACTIVE) { 297 throw new IllegalStateException (); 298 } 299 if (tranState == null) { 300 tranState = new TransactionState(gtid, this); 301 } 302 tranState.registerSynchronization(sync, control, false); 303 } 304 305 public void registerInterposedSynchronization(Synchronization sync) 306 throws RollbackException, IllegalStateException , 307 SystemException { 308 309 int status = getStatus(); 310 if (status == javax.transaction.Status.STATUS_MARKED_ROLLBACK) { 311 throw new RollbackException(); 312 } 313 if (status != javax.transaction.Status.STATUS_ACTIVE) { 314 throw new IllegalStateException (); 315 } 316 if (tranState == null) { 317 tranState = new TransactionState(gtid, this); 318 } 319 tranState.registerSynchronization(sync, control, true); 320 } 321 322 public void setRollbackOnly() 323 throws IllegalStateException , SystemException { 324 325 int status = getStatus(); 326 if (status != javax.transaction.Status.STATUS_MARKED_ROLLBACK && 327 status != javax.transaction.Status.STATUS_ACTIVE) { 328 throw new IllegalStateException (); 329 } 330 try { 331 if (Configuration.isLocalFactory()) { 332 ((ControlImpl) control).get_localCoordinator().rollback_only(); 333 } else { 334 control.get_coordinator().rollback_only(); 335 } 336 } catch (Unavailable ex) { 337 throw new SystemException (); 338 } catch (Inactive ex) { 339 throw new IllegalStateException (); 340 } catch (Exception ex) { 341 throw new SystemException (); 342 } 343 } 344 345 346 350 static private void assert_prejdk14(boolean value) { 351 if (!value) { 352 Exception e = new Exception (); 353 _logger.log(Level.WARNING,"jts.assert",e); 354 } 355 } 356 361 public String getTransactionId(){ 362 return gtid.toString(); 363 } 364 365 368 public long getStartTime(){ 369 return startTime; 370 } 371 373 374 400 401 } 402 | Popular Tags |