1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.openejb.core.transaction;
18
19 import org.apache.openejb.ApplicationException;
20 import org.apache.openejb.SystemException;
21
22 import javax.transaction.Status;
23
24 /**
25 * 17.6.2.4 RequiresNew
26 *
27 * The Container must invoke an enterprise Bean method whose transaction
28 * attribute is set to RequiresNew with a new transaction context.
29 *
30 * If the client invokes the enterprise Bean's method while the client is not
31 * associated with a transaction context, the container automatically starts a
32 * new transaction before delegating a method call to the enterprise Bean
33 * business method. The Container automatically enlists all the resource
34 * managers accessed by the business method with the transaction. If the
35 * business method invokes other enterprise beans, the Container passes the
36 * transaction context with the invocation. The Container attempts to commit
37 * the transaction when the business method has completed. The container
38 * performs the commit protocol before the method result is sent to the client.
39 *
40 * If a client calls with a transaction context, the container suspends the
41 * association of the transaction context with the current thread before
42 * starting the new transaction and invoking the business method. The container
43 * resumes the suspended transaction association after the business method and
44 * the new transaction have been completed.
45 *
46 */
47 public class TxRequiresNew extends TransactionPolicy {
48
49 public TxRequiresNew(TransactionContainer container) {
50 super(Type.RequiresNew, container);
51 }
52
53 public void beforeInvoke(Object instance, TransactionContext context) throws SystemException, ApplicationException {
54 context.callContext.set(Type.class, getPolicyType());
55
56 try {
57
58 context.clientTx = suspendTransaction(context);
59 beginTransaction(context);
60 context.currentTx = context.getTransactionManager().getTransaction();
61
62 } catch (javax.transaction.SystemException se) {
63 throw new SystemException(se);
64 }
65
66 }
67
68 public void afterInvoke(Object instance, TransactionContext context) throws ApplicationException, SystemException {
69
70 try {
71
72 if (context.currentTx.getStatus() == Status.STATUS_ACTIVE) {
73 commitTransaction(context, context.currentTx);
74 } else {
75 rollbackTransaction(context, context.currentTx);
76 }
77
78 } catch (javax.transaction.SystemException se) {
79 throw new SystemException(se);
80 } finally {
81 if (context.clientTx != null) {
82 resumeTransaction(context, context.clientTx);
83 } else if (txLogger.isInfoEnabled()) {
84 txLogger.info("TX " + policyToString() + ": No transaction to resume");
85 }
86 }
87 }
88
89 public void handleApplicationException(Throwable appException, boolean rollback, TransactionContext context) throws ApplicationException, SystemException {
90 if (rollback && context.currentTx != null) markTxRollbackOnly(context.currentTx);
91
92 throw new ApplicationException(appException);
93 }
94
95 public void handleSystemException(Throwable sysException, Object instance, TransactionContext context) throws ApplicationException, SystemException {
96
97 /* [1] Log the system exception or error **********/
98 logSystemException(sysException, context);
99
100 /* [2] afterInvoke will roll back the tx */
101 markTxRollbackOnly(context.currentTx);
102
103 /* [3] Discard instance. **************************/
104 discardBeanInstance(instance, context.callContext);
105
106 /* [4] Throw RemoteException to client ************/
107 throwExceptionToServer(sysException);
108
109 }
110 }
111