1 package xpetstore.util.uidgen.ejb; 2 3 import javax.ejb.EJBException ; 4 import javax.ejb.TransactionAttribute ; 5 import javax.ejb.TransactionAttributeType ; 6 7 import javax.ejb.Local ; 8 import javax.ejb.Stateless ; 9 10 import javax.persistence.EntityManager; 11 import javax.persistence.PersistenceContext; 12 13 26 27 @Stateless (name = "UIDGenerator") 28 @Local (UIDGenerator.class) 29 @TransactionAttribute (TransactionAttributeType.REQUIRED) 30 public class UIDGeneratorBean 31 { 32 @PersistenceContext 33 private EntityManager manager; 34 35 38 public int getUniqueId(String idPrefix) 39 { 40 return getCounter(idPrefix).nextValue(); 41 } 42 43 private Counter getCounter(String name) 44 { 45 Counter counter = null; 46 try 47 { 48 counter = manager.find(Counter.class, name); 49 if (counter == null) 50 { 51 counter = new Counter(name); 52 manager.persist(counter); 53 } 54 } catch (Exception e) 55 { 56 e.printStackTrace(); 57 throw new EJBException (e); 58 } 59 60 return counter; 61 } 62 } 63 | Popular Tags |