SequenceGeneratorBean.java~12~
上传用户:jnhyscl
上传日期:2010-03-08
资源大小:345k
文件大小:3k
- package shiponline;
- import javax.ejb.*;
- public class SequenceGeneratorBean implements SessionBean {
- SessionContext sessionContext;
- private class Entry {
- Sequence sequence;
- int first = 0;
- int last = Integer.MAX_VALUE;
- };
- private java.util.Hashtable entries = new java.util.Hashtable();
- private int blockSize;
- private int retryCount;
- private SequenceHome sequenceHome;
- public void ejbCreate() throws CreateException {
- /**@todo Complete this method*/
- }
- public void ejbRemove() {
- /**@todo Complete this method*/
- }
- public void ejbActivate() {
- /**@todo Complete this method*/
- }
- public void ejbPassivate() {
- /**@todo Complete this method*/
- }
- public void setSessionContext(SessionContext sessionContext) {
- try {
- javax.naming.Context context = new javax.naming.InitialContext();
- blockSize=1;
- retryCount=5;
- //blockSize = ((Integer) context.lookup("java:comp/env/blockSize")).intValue();
- //retryCount =/((Integer) context.lookup("java:comp/env/retryCount")).intValue();
- sequenceHome = (SequenceHome) context.lookup("java:comp/env/Sequence");
- }
- catch(javax.naming.NamingException e) {
- throw new javax.ejb.EJBException(e);
- }
- }
- public int nextSequenceNumber(String name) {
- try {
- Entry entry = (Entry) entries.get(name);
- System.out.println("name"+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) {
- // if we couldn't find it, then create it...
- entry.sequence = sequenceHome.create(name);
- }
- entries.put(name, entry);
- }
- if(entry.last > entry.first + blockSize) {
- for(int retry = 0; true; retry++) {
- try {
- entry.first = entry.sequence.seqValueBeforeIncrementingBy(blockSize);
- entry.last = entry.first;
- break;
- }
- catch(Exception e) {
- if(retry < retryCount) {
- // we hit a concurrency exception, so try again...
- 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);
- }
- catch(Exception e) {
- throw new javax.ejb.EJBException(e);
- }
- }
- }