001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: SLSBTransactionContextTester.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.tests.common.ejbs.stateless.containermanaged.entitymanager;
025:
026: import javax.ejb.Remote;
027: import javax.ejb.Stateless;
028: import javax.ejb.TransactionAttribute;
029: import javax.ejb.TransactionAttributeType;
030: import javax.persistence.EntityManager;
031: import javax.persistence.PersistenceContext;
032:
033: import org.ow2.easybeans.tests.common.ejbs.entity.ebstore.EBStore;
034:
035: /**
036: * Creates a bean using different transaction context.
037: * @author Gisele Pinheiro Souza
038: * @author Eduardo Studzinski Estima de Castro
039: */
040: @Stateless
041: @Remote(ItfTransactionContextTester.class)
042: public class SLSBTransactionContextTester implements
043: ItfTransactionContextTester {
044:
045: /**
046: * The entity manager used during the test.
047: */
048: @PersistenceContext
049: private EntityManager em;
050:
051: /**
052: * The entity manager used to delete the bean, if the bean is already in the
053: * database.
054: */
055: @PersistenceContext
056: private EntityManager emDelete;
057:
058: /**
059: * Deletes the bean with the default value.
060: */
061: @TransactionAttribute(TransactionAttributeType.REQUIRED)
062: public void deleteBean() {
063: EBStore ebstore = emDelete.find(EBStore.class, new Integer(ID));
064: if (ebstore != null) {
065: emDelete.remove(ebstore);
066: }
067: emDelete.flush();
068: }
069:
070: /**
071: * Creates a entity bean in a transaction context and call other method that has a different
072: * transaction to persist the bean.
073: */
074: @TransactionAttribute(TransactionAttributeType.REQUIRED)
075: public void createBeanRequiresNewWithClientTransaction() {
076: deleteBean();
077: EBStore ebstore = new EBStore();
078: ebstore.setId(ID);
079: ebstore.setName(NAME);
080: persistRequiresNew(ebstore);
081: }
082:
083: /**
084: * Creates a entity bean without a transaction context and call other method
085: * that has a transaction to persist the bean.
086: */
087: @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
088: public void createBeanRequiresNewWithoutClientTransaction() {
089: deleteBean();
090: EBStore ebstore = new EBStore();
091: ebstore.setId(ID);
092: ebstore.setName(NAME);
093: persistRequiresNew(ebstore);
094: }
095:
096: /**
097: * Creates a entity bean and call other method that has the same transaction
098: * to persist the bean.
099: */
100: @TransactionAttribute(TransactionAttributeType.REQUIRED)
101: public void createBeanRequired() {
102: deleteBean();
103: EBStore ebstore = new EBStore();
104: ebstore.setId(ID);
105: ebstore.setName(NAME);
106: persistRequired(ebstore);
107: }
108:
109: /**
110: * Makes the persist and does not use the client transaction.
111: * @param entity the entity class to be stored.
112: */
113: @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
114: public void persistRequiresNew(final EBStore entity) {
115: em.persist(entity);
116: }
117:
118: /**
119: * Makes the persist and use the client transaction.
120: * @param entity the entity class to be stored.
121: */
122: @TransactionAttribute(TransactionAttributeType.REQUIRED)
123: public void persistRequired(final EBStore entity) {
124: em.persist(entity);
125: }
126:
127: }
|