SequenceSessionBean.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:3k
源码类别:
Java编程
开发平台:
Java
- package examples.sequencegenerator;
- // SequenceGeneratorBean.java
- public class SequenceSessionBean implements javax.ejb.SessionBean {
- private class Entry {
- Sequence sequence;
- int last;
- };
- private java.util.Hashtable _entries = new java.util.Hashtable();
- private int _blockSize;
- private int _retryCount;
- private SequenceLocalHome _sequenceHome;
- public void ejbActivate() {
- }
- public void ejbCreate()
- {
- }
- public void ejbPassivate() {
- }
- public void ejbRemove() {
- }
- public int getNextSequenceNumber(String name)
- {
- try
- {
- Entry entry = (Entry) _entries.get(name);
- if (entry == null)
- {
- // add an entry to the sequence table
- entry = new Entry();
- try
- {
- entry.sequence = _sequenceHome.findByPrimaryKey(name);
- }
- catch (javax.ejb.FinderException e)
- {
- System.out.println("nnnn******** : " + e + "nnnn");
- // if we couldn't find it, then create it...
- entry.sequence = _sequenceHome.create(name);
- }
- _entries.put(name, entry);
- }
- if (entry.last % _blockSize == 0)
- {
- for (int retry = 0; true; retry++)
- {
- try
- {
- entry.last = entry.sequence.getValueAfterIncrementingBy(_blockSize);
- break;
- }
- catch (javax.ejb.TransactionRolledbackLocalException e)
- {
- if (retry < _retryCount)
- {
- // we hit a concurrency exception, so try again...
- System.out.println("RETRYING");
- continue;
- }
- else
- {
- // we tried too many times, so fail...
- throw new javax.ejb.EJBException(e);
- }
- }
- }
- }
- return entry.last++;
- }
- catch (javax.ejb.CreateException e)
- {
- throw new javax.ejb.EJBException(e);
- }
- }
- public void setSessionContext( javax.ejb.SessionContext sessionContext) {
- try {
- javax.naming.Context namingContext = new javax.naming.InitialContext();
- _blockSize = ((Integer) namingContext.lookup("java:comp/env/blockSize")).intValue();
- _retryCount = ((Integer) namingContext.lookup("java:comp/env/retryCount")).intValue();
- _sequenceHome = (SequenceLocalHome) namingContext.lookup("SequenceLocalHome");
- }
- catch(javax.naming.NamingException e) {
- throw new javax.ejb.EJBException(e);
- }
- }
- }