KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > ejbs > stateless > containermanaged > entitymanager > SLSBTransactionContextTester


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: SLSBTransactionContextTester.java 844 2006-07-12 09:28:58Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.entitymanager;
26
27 import javax.ejb.Remote JavaDoc;
28 import javax.ejb.Stateless JavaDoc;
29 import javax.ejb.TransactionAttribute JavaDoc;
30 import javax.ejb.TransactionAttributeType JavaDoc;
31 import javax.persistence.EntityManager;
32 import javax.persistence.PersistenceContext;
33
34 import org.objectweb.easybeans.tests.common.ejbs.entity.ebstore.EBStore;
35
36 /**
37  * Creates a bean using different transaction context.
38  * @author Gisele Pinheiro Souza
39  * @author Eduardo Studzinski Estima de Castro
40  */

41 @Stateless JavaDoc
42 @Remote JavaDoc(ItfTransactionContextTester.class)
43 public class SLSBTransactionContextTester implements ItfTransactionContextTester {
44
45     /**
46      * The entity manager used during the test.
47      */

48     @PersistenceContext
49     private EntityManager em;
50
51     /**
52      * The entity manager used to delete the bean, if the bean is already in the
53      * database.
54      */

55     @PersistenceContext
56     private EntityManager emDelete;
57
58     /**
59      * Deletes the bean with the default value.
60      */

61     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
62     public void deleteBean() {
63         EBStore ebstore = emDelete.find(EBStore.class, new Integer JavaDoc(ID));
64         if (ebstore != null) {
65             emDelete.remove(ebstore);
66         }
67         emDelete.flush();
68     }
69
70     /**
71      * Creates a entity bean in a transaction context and call other method that has a different
72      * transaction to persist the bean.
73      */

74     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
75     public void createBeanRequiresNewWithClientTransaction() {
76         deleteBean();
77         EBStore ebstore = new EBStore();
78         ebstore.setId(ID);
79         ebstore.setName(NAME);
80         persistRequiresNew(ebstore);
81     }
82
83     /**
84      * Creates a entity bean without a transaction context and call other method
85      * that has a transaction to persist the bean.
86      */

87     @TransactionAttribute JavaDoc(TransactionAttributeType.NOT_SUPPORTED)
88     public void createBeanRequiresNewWithoutClientTransaction() {
89         deleteBean();
90         EBStore ebstore = new EBStore();
91         ebstore.setId(ID);
92         ebstore.setName(NAME);
93         persistRequiresNew(ebstore);
94     }
95     /**
96      * Creates a entity bean and call other method that has the same transaction
97      * to persist the bean.
98      */

99     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
100     public void createBeanRequired() {
101         deleteBean();
102         EBStore ebstore = new EBStore();
103         ebstore.setId(ID);
104         ebstore.setName(NAME);
105         persistRequired(ebstore);
106     }
107
108     /**
109      * Makes the persist and does not use the client transaction.
110      * @param entity the entity class to be stored.
111      */

112     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRES_NEW)
113     public void persistRequiresNew(final EBStore entity) {
114         em.persist(entity);
115     }
116
117     /**
118      * Makes the persist and use the client transaction.
119      * @param entity the entity class to be stored.
120      */

121     @TransactionAttribute JavaDoc(TransactionAttributeType.REQUIRED)
122     public void persistRequired(final EBStore entity) {
123         em.persist(entity);
124     }
125
126 }
127
Popular Tags