KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > relation > tier > SequenceSessionBean


1 package org.objectweb.jonas.jtests.beans.relation.tier;
2
3 import javax.ejb.CreateException JavaDoc;
4
5
6 /**
7  * Sequence Block Session Bean
8  */

9 public class SequenceSessionBean implements javax.ejb.SessionBean JavaDoc {
10
11    private class Entry {
12       SequenceLocal sequence;
13       int last;
14    }
15
16    private java.util.Hashtable JavaDoc _entries = new java.util.Hashtable JavaDoc();
17    private int _blockSize;
18    private int _retryCount;
19    private SequenceLocalHome _sequenceHome;
20    private javax.naming.Context JavaDoc namingContext;
21
22    public int getNextSequenceNumber(String JavaDoc name) {
23       try {
24          Entry entry = (Entry) _entries.get(name);
25
26          if (entry == null) {
27             // add an entry to the sequence table
28
entry = new Entry();
29             try {
30                entry.sequence = _sequenceHome.findByPrimaryKey(name);
31             } catch (javax.ejb.FinderException JavaDoc e) {
32                // if we couldn't find it, then create it...
33
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 JavaDoc e) {
43                   if (retry < _retryCount) {
44                      // we hit a concurrency exception, so try again...
45
continue;
46                   } else {
47                      // we tried too many times, so fail...
48
throw new javax.ejb.EJBException JavaDoc(e);
49                   }
50                }
51             }
52          }
53          return entry.last++;
54       } catch (javax.ejb.CreateException JavaDoc e) {
55          throw new javax.ejb.EJBException JavaDoc(e);
56       }
57    }
58
59    public void setSessionContext( javax.ejb.SessionContext JavaDoc sessionContext) {
60       try {
61          namingContext = new javax.naming.InitialContext JavaDoc();
62          _blockSize = ((Integer JavaDoc) namingContext.lookup("java:comp/env/blockSize")).intValue();
63          _retryCount = ((Integer JavaDoc) namingContext.lookup("java:comp/env/retryCount")).intValue();
64
65          _sequenceHome = SequenceUtil.getLocalHome();
66       } catch(javax.naming.NamingException JavaDoc e) {
67          throw new javax.ejb.EJBException JavaDoc(e);
68       }
69    }
70    
71     public void ejbCreate() throws CreateException JavaDoc {
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