PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/remote/protocol/versionone/XidTransactionCommitTask.java

#
Java | 148 lines | 89 code | 21 blank | 38 comment | 11 complexity | b07db748068efc1f3e5aa8197e12c944 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.ejb3.remote.protocol.versionone;
  23. import javax.transaction.HeuristicCommitException;
  24. import javax.transaction.HeuristicMixedException;
  25. import javax.transaction.HeuristicRollbackException;
  26. import javax.transaction.RollbackException;
  27. import javax.transaction.SystemException;
  28. import javax.transaction.Transaction;
  29. import javax.transaction.xa.XAException;
  30. import javax.transaction.xa.Xid;
  31. import com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinateTransaction;
  32. import com.arjuna.ats.internal.jta.transaction.arjunacore.jca.SubordinationManager;
  33. import org.jboss.as.ejb3.EjbLogger;
  34. import org.jboss.as.ejb3.remote.EJBRemoteTransactionsRepository;
  35. import org.jboss.ejb.client.XidTransactionID;
  36. import org.jboss.logging.Logger;
  37. import org.jboss.marshalling.MarshallerFactory;
  38. import org.jboss.remoting3.Channel;
  39. /**
  40. * @author Jaikiran Pai
  41. */
  42. class XidTransactionCommitTask extends XidTransactionManagementTask {
  43. private static final Logger logger = Logger.getLogger(XidTransactionCommitTask.class);
  44. private final boolean onePhaseCommit;
  45. XidTransactionCommitTask(final TransactionRequestHandler txRequestHandler, final EJBRemoteTransactionsRepository transactionsRepository,
  46. final MarshallerFactory marshallerFactory, final XidTransactionID xidTransactionID, final Channel channel,
  47. final short invocationId, final boolean onePhaseCommit) {
  48. super(txRequestHandler, transactionsRepository, marshallerFactory, xidTransactionID, channel, invocationId);
  49. this.onePhaseCommit = onePhaseCommit;
  50. }
  51. @Override
  52. protected void manageTransaction() throws Throwable {
  53. // first associate the tx on this thread, by resuming the tx
  54. final Transaction transaction = this.transactionsRepository.removeTransaction(this.xidTransactionID);
  55. if(transaction == null) {
  56. if(EjbLogger.EJB3_INVOCATION_LOGGER.isDebugEnabled()) {
  57. //this happens if no ejb invocations where made within the TX
  58. EjbLogger.EJB3_INVOCATION_LOGGER.debug("Not committing transaction " + this.xidTransactionID + " as is was not found on the server");
  59. }
  60. return;
  61. }
  62. this.resumeTransaction(transaction);
  63. // now commit
  64. final Xid xid = this.xidTransactionID.getXid();
  65. // Courtesy: com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple
  66. try {
  67. // get the subordinate tx
  68. final SubordinateTransaction subordinateTransaction = SubordinationManager.getTransactionImporter().getImportedTransaction(xid);
  69. if (subordinateTransaction == null) {
  70. throw new XAException(XAException.XAER_INVAL);
  71. }
  72. if (subordinateTransaction.activated()) {
  73. if (this.onePhaseCommit) {
  74. subordinateTransaction.doOnePhaseCommit();
  75. } else {
  76. subordinateTransaction.doCommit();
  77. }
  78. // remove the tx
  79. SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
  80. } else {
  81. throw new XAException(XAException.XA_RETRY);
  82. }
  83. } catch (RollbackException e) {
  84. // remove the tx
  85. SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
  86. final XAException xaException = new XAException(XAException.XA_RBROLLBACK);
  87. xaException.initCause(e);
  88. throw xaException;
  89. } catch (XAException ex) {
  90. // resource hasn't had a chance to recover yet
  91. if (ex.errorCode != XAException.XA_RETRY) {
  92. // remove tx
  93. SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
  94. }
  95. throw ex;
  96. } catch (HeuristicRollbackException ex) {
  97. final XAException xaException = new XAException(XAException.XA_HEURRB);
  98. xaException.initCause(ex);
  99. throw xaException;
  100. } catch (HeuristicMixedException ex) {
  101. final XAException xaException = new XAException(XAException.XA_HEURMIX);
  102. xaException.initCause(ex);
  103. throw xaException;
  104. } catch (final HeuristicCommitException ex) {
  105. final XAException xaException = new XAException(XAException.XA_HEURCOM);
  106. xaException.initCause(ex);
  107. throw xaException;
  108. } catch (final IllegalStateException ex) {
  109. // remove tx
  110. SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
  111. final XAException xaException = new XAException(XAException.XAER_NOTA);
  112. xaException.initCause(ex);
  113. throw xaException;
  114. } catch (SystemException ex) {
  115. // remove tx
  116. SubordinationManager.getTransactionImporter().removeImportedTransaction(xid);
  117. final XAException xaException = new XAException(XAException.XAER_RMERR);
  118. xaException.initCause(ex);
  119. throw xaException;
  120. } finally {
  121. // disassociate the tx that was associated (resumed) on this thread.
  122. // This needs to be done explicitly because the SubOrdinationManager isn't responsible
  123. // for clearing the tx context from the thread
  124. this.transactionsRepository.getTransactionManager().suspend();
  125. }
  126. }
  127. }