Diff
Modified: trunk/openejb2/modules/core/src/java/org/openejb/transaction/ContainerPolicy.java (2450 => 2451)
--- trunk/openejb2/modules/core/src/java/org/openejb/transaction/ContainerPolicy.java 2006-02-16 08:02:31 UTC (rev 2450)
+++ trunk/openejb2/modules/core/src/java/org/openejb/transaction/ContainerPolicy.java 2006-02-16 10:58:37 UTC (rev 2451)
@@ -47,27 +47,12 @@
*/
package org.openejb.transaction;
-import javax.ejb.TransactionRequiredLocalException;
-import javax.ejb.TransactionRolledbackLocalException;
-import javax.transaction.RollbackException;
-import javax.transaction.TransactionRequiredException;
-import javax.transaction.TransactionRolledbackException;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.geronimo.interceptor.Interceptor;
-import org.apache.geronimo.interceptor.InvocationResult;
-import org.apache.geronimo.transaction.context.TransactionContext;
-import org.apache.geronimo.transaction.context.TransactionContextManager;
-import org.openejb.EjbInvocation;
-
/**
*
*
* @version $Revision$ $Date$
*/
public class ContainerPolicy {
- private static final Log log = LogFactory.getLog(ContainerPolicy.class);
public static final TransactionPolicy NotSupported = new TxNotSupported();
public static final TransactionPolicy Required = new TxRequired();
@@ -76,271 +61,4 @@
public static final TransactionPolicy Mandatory = new TxMandatory();
public static final TransactionPolicy Never = new TxNever();
- private static final class TxNotSupported implements TransactionPolicy {
- public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
- TransactionContext callerContext = transactionContextManager.getContext();
- if (callerContext != null) {
- callerContext.suspend();
- }
- try {
- TransactionContext beanContext = transactionContextManager.newUnspecifiedTransactionContext();
- ejbInvocation.setTransactionContext(beanContext);
- try {
- InvocationResult result = interceptor.invoke(ejbInvocation);
- return result;
- } catch (Throwable t) {
- beanContext.setRollbackOnly();
- throw t;
- } finally {
- beanContext.commit();
- }
- } finally {
- ejbInvocation.setTransactionContext(null);
- transactionContextManager.setContext(callerContext);
- if (callerContext != null) {
- callerContext.resume();
- }
- }
- }
- public String toString() {
- return "NotSupported";
- }
- private Object readResolve() {
- return ContainerPolicy.NotSupported;
- }
- }
-
- private static final class TxRequired implements TransactionPolicy {
- public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
- TransactionContext callerContext = transactionContextManager.getContext();
- if (callerContext != null && callerContext.isInheritable()) {
- try {
- ejbInvocation.setTransactionContext(callerContext);
- return interceptor.invoke(ejbInvocation);
- } catch (Throwable t){
- callerContext.setRollbackOnly();
- if (ejbInvocation.getType().isLocal()) {
- throw new TransactionRolledbackLocalException().initCause(t);
- } else {
- // can't set an initCause on a TransactionRolledbackException
- throw new TransactionRolledbackException(t.getMessage());
- }
- } finally {
- ejbInvocation.setTransactionContext(null);
- }
- }
-
- if (callerContext != null) {
- callerContext.suspend();
- }
- try {
- TransactionContext beanContext = transactionContextManager.newContainerTransactionContext();
- ejbInvocation.setTransactionContext(beanContext);
- try {
- InvocationResult result = interceptor.invoke(ejbInvocation);
- return result;
- } catch (RollbackException re) {
- throw re;
- } catch (Throwable t) {
- try {
- beanContext.setRollbackOnly();
- } catch (Exception e) {
- log.warn("Unable to roll back", e);
- }
- throw t;
- } finally {
- beanContext.commit();
- }
- } finally {
- ejbInvocation.setTransactionContext(null);
- transactionContextManager.setContext(callerContext);
- if (callerContext != null) {
- callerContext.resume();
- }
- }
- }
- public String toString() {
- return "Required";
- }
-
- private Object readResolve() {
- return ContainerPolicy.Required;
- }
- }
-
- private static final class TxSupports implements TransactionPolicy {
- public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
- TransactionContext callerContext = transactionContextManager.getContext();
- if (callerContext != null && callerContext.isInheritable()) {
- try {
- ejbInvocation.setTransactionContext(callerContext);
- return interceptor.invoke(ejbInvocation);
- } catch (Throwable t){
- callerContext.setRollbackOnly();
- if (ejbInvocation.getType().isLocal()) {
- throw new TransactionRolledbackLocalException().initCause(t);
- } else {
- // can't set an initCause on a TransactionRolledbackException
- throw new TransactionRolledbackException(t.getMessage());
- }
- } finally {
- ejbInvocation.setTransactionContext(null);
- }
- }
-
- if (callerContext != null) {
- callerContext.suspend();
- }
- try {
- TransactionContext beanContext = transactionContextManager.newUnspecifiedTransactionContext();
- ejbInvocation.setTransactionContext(beanContext);
- try {
- InvocationResult result = interceptor.invoke(ejbInvocation);
- return result;
- } catch (Throwable t) {
- beanContext.setRollbackOnly();
- throw t;
- } finally {
- beanContext.commit();
- }
- } finally {
- ejbInvocation.setTransactionContext(null);
- transactionContextManager.setContext(callerContext);
- if (callerContext != null) {
- callerContext.resume();
- }
- }
- }
- public String toString() {
- return "Supports";
- }
-
- private Object readResolve() {
- return ContainerPolicy.Supports;
- }
- }
-
- private static final class TxRequiresNew implements TransactionPolicy {
- public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
- TransactionContext callerContext = transactionContextManager.getContext();
-
- if (callerContext != null) {
- callerContext.suspend();
- }
- try {
- TransactionContext beanContext = transactionContextManager.newContainerTransactionContext();
- ejbInvocation.setTransactionContext(beanContext);
- try {
- InvocationResult result = interceptor.invoke(ejbInvocation);
- return result;
- } catch (RollbackException re) {
- throw re;
- } catch (Throwable t) {
- try {
- beanContext.setRollbackOnly();
- } catch (Exception e) {
- log.warn("Unable to roll back", e);
- }
- throw t;
- } finally {
- beanContext.commit();
- }
- } finally {
- ejbInvocation.setTransactionContext(null);
- transactionContextManager.setContext(callerContext);
- if (callerContext != null) {
- callerContext.resume();
- }
- }
- }
- public String toString() {
- return "RequiresNew";
- }
-
- private Object readResolve() {
- return ContainerPolicy.RequiresNew;
- }
- }
-
- private static final class TxMandatory implements TransactionPolicy {
- public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
- TransactionContext callerContext = transactionContextManager.getContext();
-
- // If we don't have a transaction, throw an exception
- if (callerContext == null || !callerContext.isInheritable()) {
- if (ejbInvocation.getType().isLocal()) {
- throw new TransactionRequiredLocalException();
- } else {
- throw new TransactionRequiredException();
- }
- }
-
- try {
- ejbInvocation.setTransactionContext(callerContext);
- return interceptor.invoke(ejbInvocation);
- } catch (Throwable t) {
- callerContext.setRollbackOnly();
- if (ejbInvocation.getType().isLocal()) {
- throw new TransactionRolledbackLocalException().initCause(t);
- } else {
- // can't set an initCause on a TransactionRolledbackException
- throw new TransactionRolledbackException(t.getMessage());
- }
- } finally {
- ejbInvocation.setTransactionContext(null);
- }
- }
- public String toString() {
- return "Mandatory";
- }
-
- private Object readResolve() {
- return ContainerPolicy.Mandatory;
- }
- }
-
- private static final class TxNever implements TransactionPolicy {
- public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
- TransactionContext callerContext = transactionContextManager.getContext();
-
- // If we have a transaction, throw an exception
- if (callerContext != null && callerContext.isInheritable()) {
- if (ejbInvocation.getType().isLocal()) {
- throw new TransactionNotSupportedLocalException();
- } else {
- throw new TransactionNotSupportedException();
- }
- }
-
- if (callerContext != null) {
- callerContext.suspend();
- }
- try {
- TransactionContext beanContext = transactionContextManager.newUnspecifiedTransactionContext();
- ejbInvocation.setTransactionContext(beanContext);
- try {
- InvocationResult result = interceptor.invoke(ejbInvocation);
- return result;
- } catch (Throwable t) {
- beanContext.setRollbackOnly();
- throw t;
- } finally {
- beanContext.commit();
- }
- } finally {
- ejbInvocation.setTransactionContext(null);
- transactionContextManager.setContext(callerContext);
- if (callerContext != null) {
- callerContext.resume();
- }
- }
- }
- public String toString() {
- return "Never";
- }
-
- private Object readResolve() {
- return ContainerPolicy.Never;
- }
- }
}
Added: trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxMandatory.java (2450 => 2451)
--- trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxMandatory.java 2006-02-16 08:02:31 UTC (rev 2450)
+++ trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxMandatory.java 2006-02-16 10:58:37 UTC (rev 2451)
@@ -0,0 +1,82 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openejb.transaction;
+
+import org.apache.geronimo.interceptor.InvocationResult;
+import org.apache.geronimo.interceptor.Interceptor;
+import org.apache.geronimo.transaction.context.TransactionContextManager;
+import org.apache.geronimo.transaction.context.TransactionContext;
+import org.openejb.EjbInvocation;
+
+import javax.ejb.TransactionRequiredLocalException;
+import javax.ejb.TransactionRolledbackLocalException;
+import javax.transaction.TransactionRequiredException;
+import javax.transaction.TransactionRolledbackException;
+
+/**
+ * Mandatory
+ *
+ * The Container must invoke an enterprise Bean method whose transaction
+ * attribute is set to Mandatory in a client's transaction context. The client
+ * is required to call with a transaction context.
+ *
+ * - If the client calls with a transaction context, the container invokes the
+ * enterprise Bean's method in the client's transaction context.
+ *
+ * - If the client calls without a transaction context, the Container throws
+ * the javax.transaction.TransactionRequiredException exception if the
+ * client is a remote client, or the
+ * javax.ejb.TransactionRequiredLocalException if the client is a local
+ * client.
+ *
+ */
+final class TxMandatory implements TransactionPolicy {
+ public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
+ TransactionContext callerContext = transactionContextManager.getContext();
+
+ // If we don't have a transaction, throw an exception
+ if (callerContext == null || !callerContext.isInheritable()) {
+ if (ejbInvocation.getType().isLocal()) {
+ throw new TransactionRequiredLocalException();
+ } else {
+ throw new TransactionRequiredException();
+ }
+ }
+
+ try {
+ ejbInvocation.setTransactionContext(callerContext);
+ return interceptor.invoke(ejbInvocation);
+ } catch (Throwable t) {
+ callerContext.setRollbackOnly();
+ if (ejbInvocation.getType().isLocal()) {
+ throw new TransactionRolledbackLocalException().initCause(t);
+ } else {
+ // can't set an initCause on a TransactionRolledbackException
+ throw new TransactionRolledbackException(t.getMessage());
+ }
+ } finally {
+ ejbInvocation.setTransactionContext(null);
+ }
+ }
+ public String toString() {
+ return "Mandatory";
+ }
+
+ private Object readResolve() {
+ return ContainerPolicy.Mandatory;
+ }
+}
Added: trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxNever.java (2450 => 2451)
--- trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxNever.java 2006-02-16 08:02:31 UTC (rev 2450)
+++ trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxNever.java 2006-02-16 10:58:37 UTC (rev 2451)
@@ -0,0 +1,83 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openejb.transaction;
+
+import org.apache.geronimo.interceptor.InvocationResult;
+import org.apache.geronimo.interceptor.Interceptor;
+import org.apache.geronimo.transaction.context.TransactionContextManager;
+import org.apache.geronimo.transaction.context.TransactionContext;
+import org.openejb.EjbInvocation;
+
+/**
+ * Never
+ *
+ * The Container invokes an enterprise Bean method whose transaction attribute
+ * is set to Never without a transaction context defined by the EJB spec.
+ *
+ * The client is required to call without a transaction context.
+ *
+ * - If the client calls with a transaction context, the Container throws the
+ * java.rmi.RemoteException exception if the client is a remote client, or
+ * the javax.ejb.EJBException if the client is a local client.
+ * - If the client calls without a transaction context, the Container performs
+ * the same steps as described in the NotSupported case.
+ *
+ */
+final class TxNever implements TransactionPolicy {
+ public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
+ TransactionContext callerContext = transactionContextManager.getContext();
+
+ // If we have a transaction, throw an exception
+ if (callerContext != null && callerContext.isInheritable()) {
+ if (ejbInvocation.getType().isLocal()) {
+ throw new TransactionNotSupportedLocalException();
+ } else {
+ throw new TransactionNotSupportedException();
+ }
+ }
+
+ if (callerContext != null) {
+ callerContext.suspend();
+ }
+ try {
+ TransactionContext beanContext = transactionContextManager.newUnspecifiedTransactionContext();
+ ejbInvocation.setTransactionContext(beanContext);
+ try {
+ InvocationResult result = interceptor.invoke(ejbInvocation);
+ return result;
+ } catch (Throwable t) {
+ beanContext.setRollbackOnly();
+ throw t;
+ } finally {
+ beanContext.commit();
+ }
+ } finally {
+ ejbInvocation.setTransactionContext(null);
+ transactionContextManager.setContext(callerContext);
+ if (callerContext != null) {
+ callerContext.resume();
+ }
+ }
+ }
+ public String toString() {
+ return "Never";
+ }
+
+ private Object readResolve() {
+ return ContainerPolicy.Never;
+ }
+}
Added: trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxNotSupported.java (2450 => 2451)
--- trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxNotSupported.java 2006-02-16 08:02:31 UTC (rev 2450)
+++ trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxNotSupported.java 2006-02-16 10:58:37 UTC (rev 2451)
@@ -0,0 +1,77 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openejb.transaction;
+
+import org.apache.geronimo.interceptor.InvocationResult;
+import org.apache.geronimo.interceptor.Interceptor;
+import org.apache.geronimo.transaction.context.TransactionContextManager;
+import org.apache.geronimo.transaction.context.TransactionContext;
+import org.openejb.EjbInvocation;
+
+/**
+ * NotSupported
+ *
+ * The Container invokes an enterprise Bean method whose transaction attribute
+ * is set to NotSupported with an unspecified transaction context.
+ *
+ * If a client calls with a transaction context, the container suspends the
+ * association of the transaction context with the current thread before
+ * invoking the enterprise bean's business method. The container resumes the
+ * suspended association when the business method has completed. The suspended
+ * transaction context of the client is not passed to the resource managers or
+ * other enterprise Bean objects that are invoked from the business method.
+ *
+ * If the business method invokes other enterprise beans, the Container passes
+ * no transaction context with the invocation.
+ *
+ * Refer to Subsection 17.6.5 for more details of how the Container can
+ * implement this case.
+ *
+ */
+final class TxNotSupported implements TransactionPolicy {
+ public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
+ TransactionContext callerContext = transactionContextManager.getContext();
+ if (callerContext != null) {
+ callerContext.suspend();
+ }
+ try {
+ TransactionContext beanContext = transactionContextManager.newUnspecifiedTransactionContext();
+ ejbInvocation.setTransactionContext(beanContext);
+ try {
+ InvocationResult result = interceptor.invoke(ejbInvocation);
+ return result;
+ } catch (Throwable t) {
+ beanContext.setRollbackOnly();
+ throw t;
+ } finally {
+ beanContext.commit();
+ }
+ } finally {
+ ejbInvocation.setTransactionContext(null);
+ transactionContextManager.setContext(callerContext);
+ if (callerContext != null) {
+ callerContext.resume();
+ }
+ }
+ }
+ public String toString() {
+ return "NotSupported";
+ }
+ private Object readResolve() {
+ return ContainerPolicy.NotSupported;
+ }
+}
Added: trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxRequired.java (2450 => 2451)
--- trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxRequired.java 2006-02-16 08:02:31 UTC (rev 2450)
+++ trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxRequired.java 2006-02-16 10:58:37 UTC (rev 2451)
@@ -0,0 +1,110 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openejb.transaction;
+
+import org.apache.geronimo.interceptor.InvocationResult;
+import org.apache.geronimo.interceptor.Interceptor;
+import org.apache.geronimo.transaction.context.TransactionContextManager;
+import org.apache.geronimo.transaction.context.TransactionContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.openejb.EjbInvocation;
+
+import javax.ejb.TransactionRolledbackLocalException;
+import javax.transaction.TransactionRolledbackException;
+import javax.transaction.RollbackException;
+
+/**
+ * Required
+ *
+ * The Container must invoke an enterprise Bean method whose transaction
+ * attribute is set to Required with a valid transaction context.
+ *
+ * If a client invokes the enterprise Bean's method while the client is
+ * associated with a transaction context, the container invokes the enterprise
+ * Bean's method in the client's transaction context.
+ *
+ * If the client invokes the enterprise Bean's method while the client is not
+ * associated with a transaction context, the container automatically starts a
+ * new transaction before delegating a method call to the enterprise Bean
+ * business method. The Container automatically enlists all the resource
+ * managers accessed by the business method with the transaction. If the
+ * business method invokes other enterprise beans, the Container passes the
+ * transaction context with the invocation. The Container attempts to commit
+ * the transaction when the business method has completed. The container
+ * performs the commit protocol before the method result is sent to the client.
+ *
+ */
+final class TxRequired implements TransactionPolicy {
+ private static final Log log = LogFactory.getLog(TxRequired.class);
+
+ public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
+ TransactionContext callerContext = transactionContextManager.getContext();
+ if (callerContext != null && callerContext.isInheritable()) {
+ try {
+ ejbInvocation.setTransactionContext(callerContext);
+ return interceptor.invoke(ejbInvocation);
+ } catch (Throwable t){
+ callerContext.setRollbackOnly();
+ if (ejbInvocation.getType().isLocal()) {
+ throw new TransactionRolledbackLocalException().initCause(t);
+ } else {
+ // can't set an initCause on a TransactionRolledbackException
+ throw new TransactionRolledbackException(t.getMessage());
+ }
+ } finally {
+ ejbInvocation.setTransactionContext(null);
+ }
+ }
+
+ if (callerContext != null) {
+ callerContext.suspend();
+ }
+ try {
+ TransactionContext beanContext = transactionContextManager.newContainerTransactionContext();
+ ejbInvocation.setTransactionContext(beanContext);
+ try {
+ InvocationResult result = interceptor.invoke(ejbInvocation);
+ return result;
+ } catch (RollbackException re) {
+ throw re;
+ } catch (Throwable t) {
+ try {
+ beanContext.setRollbackOnly();
+ } catch (Exception e) {
+ log.warn("Unable to roll back", e);
+ }
+ throw t;
+ } finally {
+ beanContext.commit();
+ }
+ } finally {
+ ejbInvocation.setTransactionContext(null);
+ transactionContextManager.setContext(callerContext);
+ if (callerContext != null) {
+ callerContext.resume();
+ }
+ }
+ }
+ public String toString() {
+ return "Required";
+ }
+
+ private Object readResolve() {
+ return ContainerPolicy.Required;
+ }
+}
Added: trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxRequiresNew.java (2450 => 2451)
--- trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxRequiresNew.java 2006-02-16 08:02:31 UTC (rev 2450)
+++ trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxRequiresNew.java 2006-02-16 10:58:37 UTC (rev 2451)
@@ -0,0 +1,94 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openejb.transaction;
+
+import org.apache.geronimo.interceptor.InvocationResult;
+import org.apache.geronimo.interceptor.Interceptor;
+import org.apache.geronimo.transaction.context.TransactionContextManager;
+import org.apache.geronimo.transaction.context.TransactionContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.openejb.EjbInvocation;
+
+import javax.transaction.RollbackException;
+
+/**
+ * RequiresNew
+ *
+ * The Container must invoke an enterprise Bean method whose transaction
+ * attribute is set to RequiresNew with a new transaction context.
+ *
+ * If the client invokes the enterprise Bean's method while the client is not
+ * associated with a transaction context, the container automatically starts a
+ * new transaction before delegating a method call to the enterprise Bean
+ * business method. The Container automatically enlists all the resource
+ * managers accessed by the business method with the transaction. If the
+ * business method invokes other enterprise beans, the Container passes the
+ * transaction context with the invocation. The Container attempts to commit
+ * the transaction when the business method has completed. The container
+ * performs the commit protocol before the method result is sent to the client.
+ *
+ * If a client calls with a transaction context, the container suspends the
+ * association of the transaction context with the current thread before
+ * starting the new transaction and invoking the business method. The container
+ * resumes the suspended transaction association after the business method and
+ * the new transaction have been completed.
+ *
+ */
+final class TxRequiresNew implements TransactionPolicy {
+ private static final Log log = LogFactory.getLog(TxRequiresNew.class);
+
+ public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
+ TransactionContext callerContext = transactionContextManager.getContext();
+
+ if (callerContext != null) {
+ callerContext.suspend();
+ }
+ try {
+ TransactionContext beanContext = transactionContextManager.newContainerTransactionContext();
+ ejbInvocation.setTransactionContext(beanContext);
+ try {
+ InvocationResult result = interceptor.invoke(ejbInvocation);
+ return result;
+ } catch (RollbackException re) {
+ throw re;
+ } catch (Throwable t) {
+ try {
+ beanContext.setRollbackOnly();
+ } catch (Exception e) {
+ log.warn("Unable to roll back", e);
+ }
+ throw t;
+ } finally {
+ beanContext.commit();
+ }
+ } finally {
+ ejbInvocation.setTransactionContext(null);
+ transactionContextManager.setContext(callerContext);
+ if (callerContext != null) {
+ callerContext.resume();
+ }
+ }
+ }
+ public String toString() {
+ return "RequiresNew";
+ }
+
+ private Object readResolve() {
+ return ContainerPolicy.RequiresNew;
+ }
+}
Added: trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxSupports.java (2450 => 2451)
--- trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxSupports.java 2006-02-16 08:02:31 UTC (rev 2450)
+++ trunk/openejb2/modules/core/src/java/org/openejb/transaction/TxSupports.java 2006-02-16 10:58:37 UTC (rev 2451)
@@ -0,0 +1,97 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.openejb.transaction;
+
+import org.apache.geronimo.interceptor.InvocationResult;
+import org.apache.geronimo.interceptor.Interceptor;
+import org.apache.geronimo.transaction.context.TransactionContextManager;
+import org.apache.geronimo.transaction.context.TransactionContext;
+import org.openejb.EjbInvocation;
+
+import javax.ejb.TransactionRolledbackLocalException;
+import javax.transaction.TransactionRolledbackException;
+
+/**
+ * Supports
+ *
+ * The Container invokes an enterprise Bean method whose transaction attribute
+ * is set to Supports as follows.
+ *
+ * - If the client calls with a transaction context, the Container performs
+ * the same steps as described in the Required case.
+ *
+ * - If the client calls without a transaction context, the Container performs
+ * the same steps as described in the NotSupported case.
+ *
+ * The Supports transaction attribute must be used with caution. This is
+ * because of the different transactional semantics provided by the two
+ * possible modes of execution. Only the enterprise beans that will
+ * execute correctly in both modes should use the Supports transaction
+ * attribute.
+ *
+ */
+final class TxSupports implements TransactionPolicy {
+ public InvocationResult invoke(Interceptor interceptor, EjbInvocation ejbInvocation, TransactionContextManager transactionContextManager) throws Throwable {
+ TransactionContext callerContext = transactionContextManager.getContext();
+ if (callerContext != null && callerContext.isInheritable()) {
+ try {
+ ejbInvocation.setTransactionContext(callerContext);
+ return interceptor.invoke(ejbInvocation);
+ } catch (Throwable t){
+ callerContext.setRollbackOnly();
+ if (ejbInvocation.getType().isLocal()) {
+ throw new TransactionRolledbackLocalException().initCause(t);
+ } else {
+ // can't set an initCause on a TransactionRolledbackException
+ throw new TransactionRolledbackException(t.getMessage());
+ }
+ } finally {
+ ejbInvocation.setTransactionContext(null);
+ }
+ }
+
+ if (callerContext != null) {
+ callerContext.suspend();
+ }
+ try {
+ TransactionContext beanContext = transactionContextManager.newUnspecifiedTransactionContext();
+ ejbInvocation.setTransactionContext(beanContext);
+ try {
+ InvocationResult result = interceptor.invoke(ejbInvocation);
+ return result;
+ } catch (Throwable t) {
+ beanContext.setRollbackOnly();
+ throw t;
+ } finally {
+ beanContext.commit();
+ }
+ } finally {
+ ejbInvocation.setTransactionContext(null);
+ transactionContextManager.setContext(callerContext);
+ if (callerContext != null) {
+ callerContext.resume();
+ }
+ }
+ }
+ public String toString() {
+ return "Supports";
+ }
+
+ private Object readResolve() {
+ return ContainerPolicy.Supports;
+ }
+}