01: package org.objectweb.jonas.jtests.beans.relation.tier;
02:
03: import javax.ejb.CreateException;
04:
05: /**
06: * Sequence Block Session Bean
07: */
08: public class SequenceSessionBean implements javax.ejb.SessionBean {
09:
10: private class Entry {
11: SequenceLocal sequence;
12: int last;
13: }
14:
15: private java.util.Hashtable _entries = new java.util.Hashtable();
16: private int _blockSize;
17: private int _retryCount;
18: private SequenceLocalHome _sequenceHome;
19: private javax.naming.Context namingContext;
20:
21: public int getNextSequenceNumber(String name) {
22: try {
23: Entry entry = (Entry) _entries.get(name);
24:
25: if (entry == null) {
26: // add an entry to the sequence table
27: entry = new Entry();
28: try {
29: entry.sequence = _sequenceHome
30: .findByPrimaryKey(name);
31: } catch (javax.ejb.FinderException 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
41: .getValueAfterIncrementingBy(_blockSize);
42: break;
43: } catch (javax.ejb.TransactionRolledbackLocalException e) {
44: if (retry < _retryCount) {
45: // we hit a concurrency exception, so try again...
46: continue;
47: } else {
48: // we tried too many times, so fail...
49: throw new javax.ejb.EJBException(e);
50: }
51: }
52: }
53: }
54: return entry.last++;
55: } catch (javax.ejb.CreateException e) {
56: throw new javax.ejb.EJBException(e);
57: }
58: }
59:
60: public void setSessionContext(
61: javax.ejb.SessionContext sessionContext) {
62: try {
63: namingContext = new javax.naming.InitialContext();
64: _blockSize = ((Integer) namingContext
65: .lookup("java:comp/env/blockSize")).intValue();
66: _retryCount = ((Integer) namingContext
67: .lookup("java:comp/env/retryCount")).intValue();
68:
69: _sequenceHome = SequenceUtil.getLocalHome();
70: } catch (javax.naming.NamingException e) {
71: throw new javax.ejb.EJBException(e);
72: }
73: }
74:
75: public void ejbCreate() throws CreateException {
76: }
77:
78: public void ejbActivate() {
79: }
80:
81: public void ejbPassivate() {
82: }
83:
84: public void unsetSessionContext() {
85: namingContext = null;
86: }
87:
88: public void ejbRemove() {
89: }
90:
91: }
|