1 22 package org.jboss.ejb3.test.asynchronous; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import javax.annotation.Resource; 29 import javax.ejb.Remote ; 30 import javax.ejb.Stateless ; 31 import javax.ejb.TransactionAttribute ; 32 import javax.ejb.TransactionAttributeType ; 33 import javax.ejb.Local ; 34 import javax.persistence.EntityManager; 35 import javax.persistence.Query; 36 import javax.persistence.PersistenceContext; 37 import javax.persistence.PersistenceContext; 38 39 43 @Stateless 44 @Remote (TxSessionRemote.class) 45 @Local (TxSessionLocal.class) 46 public class TxSessionBean implements TxSessionRemote, TxSessionLocal 47 { 48 @PersistenceContext EntityManager manager; 49 @Resource javax.ejb.SessionContext ctx; 50 51 @TransactionAttribute (TransactionAttributeType.REQUIRED) 52 public void createFruit(String name, boolean rollback) 53 { 54 Fruit fruit = new Fruit(name); 55 manager.persist(fruit); 56 if (rollback)ctx.setRollbackOnly(); 57 } 58 59 @TransactionAttribute (TransactionAttributeType.REQUIRED) 60 public void createVeg(String name, boolean rollback) 61 { 62 Vegetable veg = new Vegetable(name); 63 manager.persist(veg); 64 if (rollback)ctx.setRollbackOnly(); 65 } 66 67 @TransactionAttribute (TransactionAttributeType.REQUIRED) 68 public Collection getEntries() 69 { 70 Query vegQuery = manager.createQuery("SELECT v FROM Vegetable v"); 71 List veg = vegQuery.getResultList(); 72 Query fruitQuery = manager.createQuery("SELECT f FROM Fruit f"); 73 List fruit = fruitQuery.getResultList(); 74 75 76 ArrayList list = new ArrayList (); 77 for (Iterator it = veg.iterator() ; it.hasNext() ; ) 78 { 79 list.add(it.next().toString()); 80 } 81 82 for (Iterator it = fruit.iterator() ; it.hasNext() ; ) 83 { 84 list.add(it.next().toString()); 85 } 86 87 88 return list; 89 } 90 91 @TransactionAttribute (TransactionAttributeType.REQUIRED) 92 public void cleanAll() 93 { 94 Query vegQuery = manager.createQuery("SELECT v FROM Vegetable v"); 95 List veg = vegQuery.getResultList(); 96 for (Iterator 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 fruit = fruitQuery.getResultList(); 103 for (Iterator it = fruit.iterator() ; it.hasNext() ; ) 104 { 105 manager.remove(it.next()); 106 } 107 } 108 109 } 110 | Popular Tags |