1 package org.objectweb.jonas.jtests.beans.relation.tier; 2 3 import javax.ejb.CreateException ; 4 5 6 9 public class SequenceSessionBean implements javax.ejb.SessionBean { 10 11 private class Entry { 12 SequenceLocal sequence; 13 int last; 14 } 15 16 private java.util.Hashtable _entries = new java.util.Hashtable (); 17 private int _blockSize; 18 private int _retryCount; 19 private SequenceLocalHome _sequenceHome; 20 private javax.naming.Context namingContext; 21 22 public int getNextSequenceNumber(String name) { 23 try { 24 Entry entry = (Entry) _entries.get(name); 25 26 if (entry == null) { 27 entry = new Entry(); 29 try { 30 entry.sequence = _sequenceHome.findByPrimaryKey(name); 31 } catch (javax.ejb.FinderException e) { 32 entry.sequence = _sequenceHome.create(name); 34 } 35 _entries.put(name, entry); 36 } 37 if (entry.last % _blockSize == 0) { 38 for (int retry = 0; true; retry++) { 39 try { 40 entry.last = entry.sequence.getValueAfterIncrementingBy(_blockSize); 41 break; 42 } catch (javax.ejb.TransactionRolledbackLocalException e) { 43 if (retry < _retryCount) { 44 continue; 46 } else { 47 throw new javax.ejb.EJBException (e); 49 } 50 } 51 } 52 } 53 return entry.last++; 54 } catch (javax.ejb.CreateException e) { 55 throw new javax.ejb.EJBException (e); 56 } 57 } 58 59 public void setSessionContext( javax.ejb.SessionContext sessionContext) { 60 try { 61 namingContext = new javax.naming.InitialContext (); 62 _blockSize = ((Integer ) namingContext.lookup("java:comp/env/blockSize")).intValue(); 63 _retryCount = ((Integer ) namingContext.lookup("java:comp/env/retryCount")).intValue(); 64 65 _sequenceHome = SequenceUtil.getLocalHome(); 66 } catch(javax.naming.NamingException e) { 67 throw new javax.ejb.EJBException (e); 68 } 69 } 70 71 public void ejbCreate() throws CreateException { 72 } 73 74 public void ejbActivate() 75 { 76 } 77 78 public void ejbPassivate() 79 { 80 } 81 82 public void unsetSessionContext() 83 { 84 namingContext = null; 85 } 86 87 public void ejbRemove() 88 { 89 } 90 91 } 92 | Popular Tags |