|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| BMTStatelessTransactionInterceptor.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 | /** | |
| 2 | * EasyBeans | |
| 3 | * Copyright (C) 2006 Bull S.A.S. | |
| 4 | * Contact: easybeans@objectweb.org | |
| 5 | * | |
| 6 | * This library is free software; you can redistribute it and/or | |
| 7 | * modify it under the terms of the GNU Lesser General Public | |
| 8 | * License as published by the Free Software Foundation; either | |
| 9 | * version 2.1 of the License, or any later version. | |
| 10 | * | |
| 11 | * This library is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 | * Lesser General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU Lesser General Public | |
| 17 | * License along with this library; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
| 19 | * USA | |
| 20 | * | |
| 21 | * -------------------------------------------------------------------------- | |
| 22 | * $Id: BMTStatelessTransactionInterceptor.java 244 2006-03-22 10:35:56Z benoitf $ | |
| 23 | * -------------------------------------------------------------------------- | |
| 24 | */ | |
| 25 | ||
| 26 | package org.objectweb.easybeans.transaction.interceptors; | |
| 27 | ||
| 28 | import static javax.transaction.Status.STATUS_COMMITTED; | |
| 29 | ||
| 30 | import javax.ejb.EJBException; | |
| 31 | import javax.transaction.InvalidTransactionException; | |
| 32 | import javax.transaction.SystemException; | |
| 33 | import javax.transaction.Transaction; | |
| 34 | ||
| 35 | import org.objectweb.easybeans.api.EasyBeansInvocationContext; | |
| 36 | import org.objectweb.easybeans.log.JLog; | |
| 37 | import org.objectweb.easybeans.log.JLogFactory; | |
| 38 | ||
| 39 | /** | |
| 40 | * Defines an interceptor for method that are in Bean managed mode and then in Bean Managed Transaction. | |
| 41 | * It is used by stateless session bean. | |
| 42 | * @author Florent Benoit | |
| 43 | */ | |
| 44 | public class BMTStatelessTransactionInterceptor extends AbsTransactionInterceptor { | |
| 45 | ||
| 46 | /** | |
| 47 | * Logger. | |
| 48 | */ | |
| 49 | private JLog logger = JLogFactory.getLog(BMTStatelessTransactionInterceptor.class); | |
| 50 | ||
| 51 | /** | |
| 52 | * Constructor.<br> | |
| 53 | * Acquire the transaction manager. | |
| 54 | */ | |
| 55 | 0 | public BMTStatelessTransactionInterceptor() { |
| 56 | 0 | super(); |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Execute transaction as specified for BMT. | |
| 61 | * @param invocationContext context with useful attributes on the current | |
| 62 | * invocation | |
| 63 | * @return result of the next invocation (to chain interceptors) | |
| 64 | * @throws Exception if interceptor fails | |
| 65 | * @see <a href="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0 | |
| 66 | * specification ?12.6.1</a> | |
| 67 | */ | |
| 68 | 0 | @Override |
| 69 | public Object intercept(final EasyBeansInvocationContext invocationContext) throws Exception { | |
| 70 | 0 | logger.debug("Calling BMT TX interceptor"); |
| 71 | ||
| 72 | // Get current transaction | |
| 73 | 0 | Transaction transaction; |
| 74 | 0 | try { |
| 75 | 0 | transaction = getTransactionManager().getTransaction(); |
| 76 | } catch (SystemException se) { | |
| 77 | 0 | throw new EJBException("Cannot get the current transaction on transaction manager.", se); |
| 78 | } | |
| 79 | ||
| 80 | 0 | logger.debug("Transaction found = {0}", transaction); |
| 81 | ||
| 82 | /* | |
| 83 | * When a client invokes a business method via one of the enterprise | |
| 84 | * bean's client view interfaces, the container suspends any transaction | |
| 85 | * that may be associated with the client request. If there is a | |
| 86 | * transaction associated with the instance (this would happen if a | |
| 87 | * stateful session bean instance started the transaction in some | |
| 88 | * previous business method), the container associates the method | |
| 89 | * execution with this transaction. If there are interceptor methods | |
| 90 | * associated with the bean instances, these actions are taken before | |
| 91 | * the interceptor methods are invoked. | |
| 92 | */ | |
| 93 | ||
| 94 | 0 | Transaction suspendedTransaction = null; |
| 95 | 0 | if (transaction != null) { |
| 96 | 0 | try { |
| 97 | 0 | logger.debug("Suspending transaction {0}", transaction); |
| 98 | 0 | suspendedTransaction = getTransactionManager().suspend(); |
| 99 | } catch (SystemException se) { | |
| 100 | 0 | throw new EJBException("Cannot call suspend() on the transaction manager.", se); |
| 101 | } | |
| 102 | } | |
| 103 | ||
| 104 | 0 | try { |
| 105 | 0 | return invocationContext.proceed(); |
| 106 | } finally { | |
| 107 | /** | |
| 108 | * If a stateless session bean instance starts a transaction in a | |
| 109 | * business method or interceptor method, it must commit the | |
| 110 | * transaction before the business method (or all its interceptor | |
| 111 | * methods) returns. The container must detect the case in which a | |
| 112 | * transaction was started, but not completed, in the business | |
| 113 | * method or interceptor method for the business method, and handle | |
| 114 | * it as follows: | |
| 115 | * <ul> | |
| 116 | * <li>Log this as an application error to alert the System | |
| 117 | * Administrator.</li> | |
| 118 | * <li>Roll back the started transaction</li> | |
| 119 | * <li>Discard the instance of the session bean</li> | |
| 120 | * <li>Throw the javax.ejb.EJBException[53]. If the EJB 2.1 client | |
| 121 | * view is used, the container should throw java.rmi.RemoteException | |
| 122 | * if the client is a remote client, or throw the | |
| 123 | * javax.ejb.EJBException if the client is a local client.</li> | |
| 124 | */ | |
| 125 | 0 | Transaction transactionAfter = null; |
| 126 | 0 | try { |
| 127 | 0 | transactionAfter = getTransactionManager().getTransaction(); |
| 128 | } catch (SystemException se) { | |
| 129 | 0 | throw new EJBException("Cannot get the current transaction on transaction manager.", se); |
| 130 | } | |
| 131 | 0 | if (transactionAfter != null) { |
| 132 | 0 | int transactionStatus = transactionAfter.getStatus(); |
| 133 | // There is a transaction and it was not committed | |
| 134 | 0 | if (transactionStatus != STATUS_COMMITTED) { |
| 135 | 0 | String errMsg = "Transaction started by the bean but not committed."; |
| 136 | // Log error | |
| 137 | 0 | logger.error(errMsg); |
| 138 | // Rollback | |
| 139 | 0 | transactionAfter.rollback(); |
| 140 | //TODO: discard | |
| 141 | // Throw Exception | |
| 142 | 0 | throw new EJBException(errMsg); |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | ||
| 147 | /* | |
| 148 | * The container resumes the suspended association when the business | |
| 149 | * method has completed. | |
| 150 | */ | |
| 151 | 0 | if (suspendedTransaction != null) { |
| 152 | ||
| 153 | 0 | logger.debug("Resuming transaction {0}", transaction); |
| 154 | ||
| 155 | 0 | try { |
| 156 | 0 | getTransactionManager().resume(suspendedTransaction); |
| 157 | } catch (InvalidTransactionException ite) { | |
| 158 | 0 | throw new EJBException( |
| 159 | "Cannot call resume() on the given transaction. There is an invalid transaction", ite); | |
| 160 | } catch (IllegalStateException ise) { | |
| 161 | 0 | throw new EJBException( |
| 162 | "Cannot call resume() on the given transaction. There is another associated transaction", | |
| 163 | ise); | |
| 164 | } catch (SystemException se) { | |
| 165 | 0 | throw new EJBException("Cannot call resume() on the given transaction. Unexpected error condition", |
| 166 | se); | |
| 167 | } | |
| 168 | } | |
| 169 | } | |
| 170 | } | |
| 171 | ||
| 172 | } |
|
||||||||||