1 package org.objectweb.jonas.jtests.beans.ejbql; 2 3 public class SequenceSessionSL implements javax.ejb.SessionBean { 4 5 private class Entry { 6 SequenceLocal sequence; 7 int last; 8 }; 9 10 private java.util.Hashtable _entries = new java.util.Hashtable (); 11 private int _blockSize; 12 private int _retryCount; 13 private SequenceLocalHome _sequenceHome; 14 15 public int getNextNumberInSequence(String name) { 16 17 try { 18 Entry entry = (Entry) _entries.get(name); 19 20 if (entry == null) { 21 entry = new Entry(); 23 try { 24 entry.sequence = _sequenceHome.findByPrimaryKey(name); 25 26 } catch (javax.ejb.FinderException e) { 27 entry.sequence = _sequenceHome.create(name); 29 } 30 _entries.put(name, entry); 31 } 32 if (entry.last % _blockSize == 0) { 33 for (int retry = 0; true; retry++) { 34 try { 35 entry.last = entry.sequence.getNextKeyAfterIncrementingBy(_blockSize); 36 37 break; 38 } catch (javax.ejb.TransactionRolledbackLocalException e) { 39 if (retry < _retryCount) { 40 continue; 42 } else { 43 throw new javax.ejb.EJBException (e); 45 } 46 } 47 } 48 } 49 int ret = entry.last+1; 50 return entry.last++; 51 } catch (javax.ejb.CreateException e) { 52 throw new javax.ejb.EJBException (e); 53 } 54 } 55 public void setSessionContext( javax.ejb.SessionContext sessionContext) { 56 try { 57 javax.naming.Context namingContext = new javax.naming.InitialContext (); 58 _blockSize = ((Integer ) namingContext.lookup("java:comp/env/blockSize")).intValue(); 59 _retryCount = ((Integer ) namingContext.lookup("java:comp/env/retryCount")).intValue(); 60 61 _sequenceHome = (SequenceLocalHome) namingContext.lookup("java:comp/env/ejb/SequenceLocalHome"); 62 } catch(javax.naming.NamingException e) { 63 throw new javax.ejb.EJBException (e); 64 } 65 } 66 public void ejbActivate() {} 67 public void ejbCreate() {} 68 public void ejbPassivate() {} 69 public void ejbRemove() {} 70 71 72 } 73 | Popular Tags |