Generating a sequence number


EJB design: Generating a sequence number

  1. Generating a sequence number (16 messages)

    I wanted to generate a sequence number say for numbering orders. First I thought of getting the last number from the table and incrementing it. If two persons access the Server at the same time, there's every chance of getting primary key exception. How to overcome this !

    Threaded Messages (16)

  2. Generating a sequence number[ Go to top ]

    If u are using oracle but i think this probably applies to all databases use the database sequence. But I am not sure how to use this with entity beans. So what we are doing here is use a jdbc insert statemet with the sequence no parameter set as xxxSeq.nextval hardcoded for the sequence no field where xxxSeq is the name of the sequence which has a starting no say 1.
    For selection from the database we use beans. This sequence no is thread and transaction safe and u dont have to worry bout the things u would have to if u try to manage this at the application layer


    Hope this helps
  3. Generating a sequence number[ Go to top ]

    You can have a separate table that has two columns(TableName, CurrentIndex). This table maintains the Current Sequence number for all the tables in Databse system. Write an CMP entity Bean that encapsulates this table and use the findByPrimaryKey() method to retrieve a Bean Instance of a particular TableName and use a method like getNextKey() to return the next sequence number. In the method getNextKey() simply increment the current Sequence number and return it. The concurrency issue will be handled by the EJB container.
  4. Generating a sequence number[ Go to top ]

    But the idea of seperate tables might throw up a performance bottleneck as the transaction isolation level needs to be TransactionSerializable. That way multiple transactions cant update it. This further throws up a problem using oracle which uses optimistic concurrency. That it does not detect conflict till commit. If u are using weblogic 4.5.1 u will find that oracle will throw an exception and when this happens weblogic slows down anyways. Its best in this case to leave the transactions and threading issues to the database. It works very fast and is very scalable.
  5. Generating a sequence number[ Go to top ]

     I am using a singleton to do that.When the instance gets created for the first time ; a sql query is fired to retrieve the last number and stored in a static variable ; which u hand over to any calling program after incrementing it.It seems to be a good method to me ; the only problem can be that there can be missing numbers ; if the transaction rolls back ; cause the static variable will still have the incremented count.
     I am wondering if in this design there can be any synchronsation issues.Guys any ideas?
  6. Generating a sequence number[ Go to top ]

    Using the singleton like that will cause problems in the cluster.

    I use the stateless session bean to generate PKs - stateless bean allocates chunks of PKs (using the CMP entity bean) and dispences keys to the client's (SSB is deployed with max-beans-in-pool set to 1). Of course, this is cheating, because stateless session bean keeps it's state (chunk of PKs) between invocations, but in the worst case if the bean instance gets discarded the chunk of PKs will be lost - no big deal.

    This works correctly in the cluster and it is fast.
  7. Generating a sequence number[ Go to top ]

    Why dont u use the database sequences they are meant for just this kind of scenario. they are thread safe and no headaches. Everything done at the database level.

  8. Generating a sequence number[ Go to top ]

  9. Generating a sequence number[ Go to top ]

    because it is not portable.
  10. Generating a sequence number[ Go to top ]

    I have used a class with a static variable called lastnumber. The constructer of the class gets the last number from the database.

    I use this class in my bean to get the last number.

    The class has a check to see if its initialized or not. If its not initialized it goes to the database, else the static number in memory is used to get the next sequence number.

    The got number is updated to database by the bean.

    This works fine but i have not tested it with more than 20-30 clients.

    the only problems i see are

    1) In case of roll back after getting the sequence number and if the number is not updated to database, there will be gaps in the sequence numbers.

    I would be glad if this helps. I also want a critical review as to whether this would work.
  11. Generating a sequence number[ Go to top ]

    Well it may not be portable but it how many time do u chage Databases. Unless u are in product development wherein u need to support multiple databases, u dont need to be portable across multiple databases. Besides u can use SQLs from properties files in which case u can provide a diff property file for each database. It all works fine.
    Worrying about sequences is not a pretty thing especially when u have just one database and multiple applications on multiple instances of your app server. U will be running severe circles.
    Even if u do use a singleton how will u manage one instance of the singleton across two app server instances running two applications and sharing the same database.

    In some case where in u have just one appserver instance using a singleton works but eventually u will rue this decision. I did.

    So use database sequences where u can. It hurts portability but if u dont hard codethe sqls in your code but keep them in property files u can port easily by making minor modifications.

  12. Generating a sequence number[ Go to top ]

    Hi Dimitri R
       I dont understand that if you limit the no. of stateless session beans in pool to 1 ; will they be 1 per cluster or 1 per instance.
       Cause if its 1 per instance ; I dont understand how it will hand over unique numbers.Can u explain the process in which u aim to achieve this.
       Regards ,
  13. Generating a sequence number[ Go to top ]

    The stateless session bean is deployed with beans-in-pool set to 1 (or more, depending on the concurrency needs - if there is only one instance of the ssb available all accesses to it will be serialized by the container) on each server in the cluster.

    When the new pk is requested bean allocates chunk of them (by increasing nextvalue field in the database by it's chunk size), stores it in it's member variable (thats where cheating happens - bean now has state between invocations), and dispences keys from this chunk. When all keys from the chunk are dispenced it repeats the process of allocating the new one from the database. That way pk's generated are unique in the cluster.

    If you want I can post source for the whole thing.
  14. Generating a sequence number[ Go to top ]

      I guess that will help me ; tho I think i got the concept.
  15. Generating a sequence number[ Go to top ]

        Could you please possibly post the source, it would be very much appreciated.
     for the Statless
  16. Generating a sequence number[ Go to top ]

    But there is absolutely nothing to it:
    deploy with with beans-in-pool set to 1 and the bean looks like this:

    private LinkedList block = new LinkedList();

    /* business method */
    public long getKey() {
        if(block.isEmpty()) {
           try {
           ... grab the next block of keys from the database...
           } catch(Exception e) {
              throw new EJBException(e.getMessage());
        return ((Long)(block.removeFirst())).longValue();

  17. Generating a sequence number[ Go to top ]

        Could you please possibly post the source, it would be very much appreciated.
     for the Statless