KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > bean > TxPOJO


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.aop.bean;
23
24 import org.jboss.tm.TxUtils;
25
26 import javax.naming.InitialContext JavaDoc;
27 import javax.transaction.Transaction JavaDoc;
28 import javax.transaction.TransactionManager JavaDoc;
29
30 /**
31  *
32  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
33  * @version $Revision: 37406 $
34  */

35 public class TxPOJO
36 {
37    TransactionManager JavaDoc tm;
38
39    public TxPOJO() throws Exception JavaDoc
40    {
41       tm = (TransactionManager JavaDoc)new InitialContext JavaDoc().lookup("java:/TransactionManager");
42    }
43
44    public void never() {}
45
46    public void callNever() throws Exception JavaDoc
47    {
48       boolean exceptionThrown = false;
49       tm.begin();
50       try
51       {
52          never();
53       }
54       catch (Exception JavaDoc ex)
55       {
56          exceptionThrown = true;
57       }
58       tm.commit();
59       if (!exceptionThrown) throw new Exception JavaDoc("failed on mandatory no tx call");
60    }
61
62    public void notsupported() throws Exception JavaDoc
63    {
64       if (tm.getTransaction() != null) throw new Exception JavaDoc("notsupported() method has tx set");
65    }
66
67    public void callNotSupported() throws Exception JavaDoc
68    {
69       tm.begin();
70       notsupported();
71       tm.commit();
72    }
73
74    public void supports(Transaction JavaDoc tx) throws Exception JavaDoc
75    {
76       Transaction JavaDoc tmTx = tm.getTransaction();
77       if (tx != tmTx) throw new Exception JavaDoc("supports didn't work");
78    }
79
80    public boolean hasActiveTransaction() throws Exception JavaDoc
81    {
82       Transaction JavaDoc tx = tm.getTransaction();
83       if (tx == null)
84       {
85          System.out.println("Transaction: is null");
86       } // end of if ()
87
else
88       {
89          System.out.println("Transaction: status " + tx.getStatus() + " of tx" + tx);
90       } // end of else
91

92       return TxUtils.isActive(tx);
93    }
94
95    public void callSupportsWithTx() throws Exception JavaDoc
96    {
97       tm.begin();
98       Transaction JavaDoc tx = tm.getTransaction();
99       supports(tx);
100       tm.commit();
101    }
102
103    public void callSupportsWithoutTx() throws Exception JavaDoc
104    {
105       supports(null);
106    }
107
108    public void required() throws Exception JavaDoc
109    {
110       if (tm.getTransaction() == null) throw new Exception JavaDoc("rquired() method has no tx set");
111    }
112
113
114    public void requiresNew(Transaction JavaDoc tx) throws Exception JavaDoc
115    {
116       Transaction JavaDoc tmTx = tm.getTransaction();
117       if (tx == tmTx
118           || (tx != null && tx.equals(tmTx))) throw new Exception JavaDoc("transactions shouldn't be equal");
119       if (tmTx == null) throw new Exception JavaDoc("tx is null in RequiresNew");
120    }
121    public void callRequiresNew() throws Exception JavaDoc
122    {
123       tm.begin();
124       Transaction JavaDoc tx = tm.getTransaction();
125       requiresNew(tx);
126       tm.commit();
127    }
128
129    public void mandatory() {}
130
131    public void callMandatoryNoTx() throws Exception JavaDoc
132    {
133       boolean exceptionThrown = false;
134       try
135       {
136          mandatory();
137       }
138       catch (Exception JavaDoc ex)
139       {
140          exceptionThrown = true;
141       }
142       if (!exceptionThrown) throw new Exception JavaDoc("failed on mandatory no tx call");
143    }
144
145    public void callMandatoryWithTx() throws Exception JavaDoc
146    {
147       tm.begin();
148       mandatory();
149       tm.commit();
150    }
151
152
153 }
154
155
Popular Tags