01: package org.objectweb.jonas.jtests.beans.ejbql;
02:
03: public class SequenceSessionSL implements javax.ejb.SessionBean {
04:
05: private class Entry {
06: SequenceLocal sequence;
07: int last;
08: };
09:
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: // add an entry to the sequence table
22: entry = new Entry();
23: try {
24: entry.sequence = _sequenceHome
25: .findByPrimaryKey(name);
26:
27: } catch (javax.ejb.FinderException e) {
28: // if we couldn't find it, then create it...
29: entry.sequence = _sequenceHome.create(name);
30: }
31: _entries.put(name, entry);
32: }
33: if (entry.last % _blockSize == 0) {
34: for (int retry = 0; true; retry++) {
35: try {
36: entry.last = entry.sequence
37: .getNextKeyAfterIncrementingBy(_blockSize);
38:
39: break;
40: } catch (javax.ejb.TransactionRolledbackLocalException e) {
41: if (retry < _retryCount) {
42: // we hit a concurrency exception, so try again...
43: continue;
44: } else {
45: // we tried too many times, so fail...
46: throw new javax.ejb.EJBException(e);
47: }
48: }
49: }
50: }
51: int ret = entry.last + 1;
52: return entry.last++;
53: } catch (javax.ejb.CreateException e) {
54: throw new javax.ejb.EJBException(e);
55: }
56: }
57:
58: public void setSessionContext(
59: javax.ejb.SessionContext sessionContext) {
60: try {
61: javax.naming.Context namingContext = new javax.naming.InitialContext();
62: _blockSize = ((Integer) namingContext
63: .lookup("java:comp/env/blockSize")).intValue();
64: _retryCount = ((Integer) namingContext
65: .lookup("java:comp/env/retryCount")).intValue();
66:
67: _sequenceHome = (SequenceLocalHome) namingContext
68: .lookup("java:comp/env/ejb/SequenceLocalHome");
69: } catch (javax.naming.NamingException e) {
70: throw new javax.ejb.EJBException(e);
71: }
72: }
73:
74: public void ejbActivate() {
75: }
76:
77: public void ejbCreate() {
78: }
79:
80: public void ejbPassivate() {
81: }
82:
83: public void ejbRemove() {
84: }
85:
86: }
|