PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jboss-as-7.1.1.Final/ejb3/src/main/java/org/jboss/as/ejb3/tx/EjbBMTInterceptor.java

#
Java | 120 lines | 76 code | 12 blank | 32 comment | 7 complexity | 030cd0b50f169e495d43f35c6e7ad045 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright (c) 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.tx;
  23. import javax.ejb.EJBException;
  24. import javax.transaction.Status;
  25. import javax.transaction.SystemException;
  26. import javax.transaction.TransactionManager;
  27. import org.jboss.as.ee.component.Component;
  28. import org.jboss.as.ee.component.ComponentInstanceInterceptorFactory;
  29. import org.jboss.as.ejb3.component.EJBComponent;
  30. import org.jboss.invocation.Interceptor;
  31. import org.jboss.invocation.InterceptorContext;
  32. import org.jboss.invocation.InterceptorFactory;
  33. import org.jboss.invocation.InterceptorFactoryContext;
  34. import org.jboss.logging.Logger;
  35. /**
  36. * EJB 3 13.6.1:
  37. * If a stateless session bean instance starts a transaction in a business method or interceptor method, it
  38. * must commit the transaction before the business method (or all its interceptor methods) returns.
  39. *
  40. * @author <a href="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
  41. * @version $Revision: $
  42. */
  43. public class EjbBMTInterceptor extends BMTInterceptor {
  44. private static final Logger log = Logger.getLogger(EjbBMTInterceptor.class);
  45. public static final InterceptorFactory FACTORY = new ComponentInstanceInterceptorFactory() {
  46. @Override
  47. protected Interceptor create(final Component component, final InterceptorFactoryContext context) {
  48. return new EjbBMTInterceptor((EJBComponent) component);
  49. }
  50. };
  51. EjbBMTInterceptor(final EJBComponent component) {
  52. super(component);
  53. }
  54. private void checkStatelessDone(final EJBComponent component, final InterceptorContext invocation, final TransactionManager tm, Throwable ex) throws Exception {
  55. int status = Status.STATUS_NO_TRANSACTION;
  56. try {
  57. status = tm.getStatus();
  58. } catch (SystemException sex) {
  59. log.error("Failed to get status", sex);
  60. }
  61. switch (status) {
  62. case Status.STATUS_ACTIVE:
  63. case Status.STATUS_COMMITTING:
  64. case Status.STATUS_MARKED_ROLLBACK:
  65. case Status.STATUS_PREPARING:
  66. case Status.STATUS_ROLLING_BACK:
  67. try {
  68. tm.rollback();
  69. } catch (Exception sex) {
  70. log.error("Failed to rollback", sex);
  71. }
  72. // fall through...
  73. case Status.STATUS_PREPARED:
  74. String msg = "EJB 3.1 FR 13.3.3: BMT bean " + component.getComponentName()
  75. + " should complete transaction before returning.";
  76. log.error(msg);
  77. if (ex instanceof Exception) {
  78. throw new EJBException(msg, (Exception) ex);
  79. } else {
  80. throw new EJBException(msg, new RuntimeException(ex));
  81. }
  82. }
  83. // the instance interceptor will discard the instance
  84. if (ex != null)
  85. throw this.handleException(invocation, ex);
  86. }
  87. @Override
  88. protected Object handleInvocation(final InterceptorContext invocation) throws Exception {
  89. final EJBComponent ejbComponent = getComponent();
  90. TransactionManager tm = ejbComponent.getTransactionManager();
  91. assert tm.getTransaction() == null : "can't handle BMT transaction, there is a transaction active";
  92. boolean exceptionThrown = false;
  93. try {
  94. return invocation.proceed();
  95. } catch (Throwable ex) {
  96. exceptionThrown = true;
  97. checkStatelessDone(ejbComponent, invocation, tm, ex);
  98. //we should never get here, as checkStatelessDone should re-throw
  99. throw (Exception)ex;
  100. } finally {
  101. try {
  102. if (!exceptionThrown) checkStatelessDone(ejbComponent, invocation, tm, null);
  103. } finally {
  104. tm.suspend();
  105. }
  106. }
  107. }
  108. }