KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > ejbql > SequenceSessionSL


1 package org.objectweb.jonas.jtests.beans.ejbql;
2
3 public class SequenceSessionSL implements javax.ejb.SessionBean JavaDoc {
4
5     private class Entry {
6     SequenceLocal sequence;
7     int last;
8     };
9
10     private java.util.Hashtable JavaDoc _entries = new java.util.Hashtable JavaDoc();
11     private int _blockSize;
12     private int _retryCount;
13     private SequenceLocalHome _sequenceHome;
14
15     public int getNextNumberInSequence(String JavaDoc 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.findByPrimaryKey(name);
25
26         } catch (javax.ejb.FinderException JavaDoc e) {
27             // if we couldn't find it, then create it...
28
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 JavaDoc e) {
39             if (retry < _retryCount) {
40                 // we hit a concurrency exception, so try again...
41
continue;
42             } else {
43                 // we tried too many times, so fail...
44
throw new javax.ejb.EJBException JavaDoc(e);
45             }
46             }
47         }
48         }
49         int ret = entry.last+1;
50         return entry.last++;
51     } catch (javax.ejb.CreateException JavaDoc e) {
52         throw new javax.ejb.EJBException JavaDoc(e);
53     }
54     }
55     public void setSessionContext( javax.ejb.SessionContext JavaDoc sessionContext) {
56     try {
57         javax.naming.Context JavaDoc namingContext = new javax.naming.InitialContext JavaDoc();
58         _blockSize = ((Integer JavaDoc) namingContext.lookup("java:comp/env/blockSize")).intValue();
59         _retryCount = ((Integer JavaDoc) namingContext.lookup("java:comp/env/retryCount")).intValue();
60       
61         _sequenceHome = (SequenceLocalHome) namingContext.lookup("java:comp/env/ejb/SequenceLocalHome");
62     } catch(javax.naming.NamingException JavaDoc e) {
63         throw new javax.ejb.EJBException JavaDoc(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