KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > asynchronous > TxSessionBean


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.ejb3.test.asynchronous;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.annotation.Resource;
29 import javax.ejb.Remote JavaDoc;
30 import javax.ejb.Stateless JavaDoc;
31 import javax.ejb.TransactionAttribute JavaDoc;
32 import javax.ejb.TransactionAttributeType JavaDoc;
33 import javax.ejb.Local JavaDoc;
34 import javax.persistence.EntityManager;
35 import javax.persistence.Query;
36 import javax.persistence.PersistenceContext;
37 import javax.persistence.PersistenceContext;
38
39 /**
40  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
41  * @version $Revision: 37459 $
42  */

43 @Stateless JavaDoc
44 @Remote JavaDoc(TxSessionRemote.class)
45 @Local JavaDoc(TxSessionLocal.class)
46 public class TxSessionBean implements TxSessionRemote, TxSessionLocal
47 {
48    @PersistenceContext EntityManager manager;
49    @Resource javax.ejb.SessionContext JavaDoc ctx;
50
51    @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
52    public void createFruit(String JavaDoc name, boolean rollback)
53    {
54       Fruit fruit = new Fruit(name);
55       manager.persist(fruit);
56       if (rollback)ctx.setRollbackOnly();
57    }
58
59    @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
60    public void createVeg(String JavaDoc name, boolean rollback)
61    {
62       Vegetable veg = new Vegetable(name);
63       manager.persist(veg);
64       if (rollback)ctx.setRollbackOnly();
65    }
66
67    @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
68    public Collection JavaDoc getEntries()
69    {
70       Query vegQuery = manager.createQuery("SELECT v FROM Vegetable v");
71       List JavaDoc veg = vegQuery.getResultList();
72       Query fruitQuery = manager.createQuery("SELECT f FROM Fruit f");
73       List JavaDoc fruit = fruitQuery.getResultList();
74
75
76       ArrayList JavaDoc list = new ArrayList JavaDoc();
77       for (Iterator JavaDoc it = veg.iterator() ; it.hasNext() ; )
78       {
79          list.add(it.next().toString());
80       }
81
82       for (Iterator JavaDoc it = fruit.iterator() ; it.hasNext() ; )
83       {
84          list.add(it.next().toString());
85       }
86
87
88       return list;
89    }
90
91    @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
92    public void cleanAll()
93    {
94       Query vegQuery = manager.createQuery("SELECT v FROM Vegetable v");
95       List JavaDoc veg = vegQuery.getResultList();
96       for (Iterator JavaDoc it = veg.iterator() ; it.hasNext() ; )
97       {
98          manager.remove(it.next());
99       }
100
101       Query fruitQuery = manager.createQuery("SELECT f FROM Fruit f");
102       List JavaDoc fruit = fruitQuery.getResultList();
103       for (Iterator JavaDoc it = fruit.iterator() ; it.hasNext() ; )
104       {
105          manager.remove(it.next());
106       }
107    }
108
109 }
110
Popular Tags