001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.tm.ejb;
023:
024: import java.rmi.RemoteException;
025:
026: import javax.transaction.Transaction;
027: import javax.transaction.TransactionManager;
028: import javax.transaction.UserTransaction;
029:
030: import org.jboss.test.tm.interfaces.BMTCleanUp;
031: import org.jboss.test.util.ejb.SessionSupport;
032: import org.jboss.tm.TransactionManagerLocator;
033:
034: /**
035: * BMTCleanUpBean.
036: *
037: * @author <a href="adrian@jboss.com">Adrian Brock</a>
038: * @version $Revision: 1.1 $
039: */
040: public class BMTCleanUpBean extends SessionSupport {
041: /** The serialVersionUID */
042: private static final long serialVersionUID = -6750278789142406435L;
043:
044: public void ejbCreate() {
045: }
046:
047: public void doNormal() throws RemoteException {
048: UserTransaction ut = sessionCtx.getUserTransaction();
049: try {
050: ut.begin();
051: ut.commit();
052: } catch (Exception e) {
053: throw new RemoteException("Error", e);
054: }
055: }
056:
057: public void testIncomplete() throws RemoteException {
058: BMTCleanUp remote = (BMTCleanUp) sessionCtx.getEJBObject();
059: try {
060: remote.doIncomplete();
061: } catch (RemoteException expected) {
062: // expected
063: }
064: checkTransaction();
065: remote.doNormal();
066: }
067:
068: public void doIncomplete() throws RemoteException {
069: UserTransaction ut = sessionCtx.getUserTransaction();
070: try {
071: ut.begin();
072: } catch (Exception e) {
073: throw new RemoteException("Error", e);
074: }
075: }
076:
077: public void testTxTimeout() throws RemoteException {
078: BMTCleanUp remote = (BMTCleanUp) sessionCtx.getEJBObject();
079: try {
080: remote.doTimeout();
081: } catch (RemoteException expected) {
082: // expected
083: }
084: checkTransaction();
085: remote.doNormal();
086: }
087:
088: public void doTimeout() throws RemoteException {
089: UserTransaction ut = sessionCtx.getUserTransaction();
090: try {
091: ut.setTransactionTimeout(5);
092: ut.begin();
093: Thread.sleep(10000);
094: } catch (InterruptedException ignored) {
095: } catch (Exception e) {
096: throw new RemoteException("Error", e);
097: }
098: }
099:
100: private void checkTransaction() throws RemoteException {
101: TransactionManager tm = TransactionManagerLocator.getInstance()
102: .locate();
103: try {
104: Transaction tx = tm.getTransaction();
105: if (tx != null)
106: throw new RemoteException(
107: "There should be no transaction context: " + tx);
108: } catch (RemoteException e) {
109: throw e;
110: } catch (Exception e) {
111: throw new RemoteException("Error", e);
112: }
113: }
114: }
|