01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.openejb.core.transaction;
17:
18: import org.apache.openejb.ApplicationException;
19: import org.apache.openejb.SystemException;
20:
21: import javax.transaction.TransactionRequiredException;
22:
23: /**
24: * 17.6.2.5 Mandatory
25: *
26: * The Container must invoke an enterprise Bean method whose transaction
27: * attribute is set to Mandatory in a client's transaction context. The client
28: * is required to call with a transaction context.
29: *
30: * * If the client calls with a transaction context, the container invokes the
31: * enterprise Bean's method in the client's transaction context.
32: *
33: * * If the client calls without a transaction context, the Container throws
34: * the javax.transaction.TransactionRequiredException exception if the
35: * client is a remote client, or the
36: * javax.ejb.TransactionRequiredLocalException if the client is a local
37: * client.
38: *
39: */
40: public class TxMandatory extends TransactionPolicy {
41:
42: public TxMandatory(TransactionContainer container) {
43: super (Type.Mandatory, container);
44: }
45:
46: public void beforeInvoke(Object instance, TransactionContext context)
47: throws SystemException, ApplicationException {
48:
49: try {
50:
51: context.clientTx = context.getTransactionManager()
52: .getTransaction();
53:
54: if (context.clientTx == null) {
55:
56: throw new ApplicationException(
57: new TransactionRequiredException());
58: }
59:
60: context.currentTx = context.clientTx;
61:
62: } catch (javax.transaction.SystemException se) {
63: logger.debug("Exception during getTransaction()", se);
64: throw new SystemException(se);
65: }
66: }
67:
68: public void afterInvoke(Object instance, TransactionContext context)
69: throws ApplicationException, SystemException {
70: }
71:
72: public void handleApplicationException(Throwable appException,
73: boolean rollback, TransactionContext context)
74: throws ApplicationException, SystemException {
75: if (rollback && context.currentTx != null)
76: markTxRollbackOnly(context.currentTx);
77:
78: throw new ApplicationException(appException);
79: }
80:
81: public void handleSystemException(Throwable sysException,
82: Object instance, TransactionContext context)
83: throws ApplicationException, SystemException {
84:
85: /* [1] Log the system exception or error *********/
86: logSystemException(sysException, context);
87:
88: /* [2] Mark the transaction for rollback. ********/
89: markTxRollbackOnly(context.currentTx);
90:
91: /* [3] Discard instance. *************************/
92: discardBeanInstance(instance, context.callContext);
93:
94: /* [4] TransactionRolledbackException to client **/
95: throwTxExceptionToServer(sysException);
96: }
97:
98: }
|