4 Replies Latest reply on Mar 26, 2004 10:44 AM by Darran Lofthouse

    EJB Design Patterns - primary key 'sequence block'

    ahardy66 Novice


      Do any of you nice folk out there have an example of the primary-key-generating SequenceBlock entity bean ?

      I'm taken by the EJB Design Pattern's pattern, but unfortunately unlike most books, it doesn't have any example code and I can't find anything like it elsewhere either.

      I hope you know what I mean - just a relatively simple entity bean that grabs blocks of 10 or 100 or 1000 integers from a primary key database table, and has a method returning the next key, refreshing the block if it runs out.

      Apparently it should be hidden behind a session bean, but I guess that's just a bit tra-la-la for now.

      Any comments on this pattern as well?

      Adam

        • 1. Re: EJB Design Patterns - primary key 'sequence block'
          Marek Neumann Novice

          IIRC middlegen has support for generating code for this pattern.

          -marek

          • 2. Re: EJB Design Patterns - primary key 'sequence block'
            Darran Lofthouse Master

             

            Apparently it should be hidden behind a session bean, but I guess that's just a bit tra-la-la for now.


            No the session bean is the most important part of this pattern, it is the stateless session bean that caches a reserved range of integers.

            I hope you know what I mean - just a relatively simple entity bean that grabs blocks of 10 or 100 or 1000 integers from a primary key database table, and has a method returning the next key, refreshing the block if it runs out.


            No the entity bean does not grab a block of integers, the entity bean just holds a record of the highest integer reserved by one of the session bean instances.

            The stateless session bean holds the integer range that it has reserved using member variables, when the end of the range is reached the session bean makes use of the entity to reserve a new range.

            • 3. Re: EJB Design Patterns - primary key 'sequence block'
              ahardy66 Novice

              Excellent. Middlegen does indeed generate one (or rather both) EJBs.

              I appreciate the reasons for the session bean, now that I'm looking at the code, but isn't the bean going to get cached by JBoss at any point and lose the sequence block? In which case it will have to get a new block and if it happens alot, how will I make sure it actually uses at least most of the reserved primary key integers?

              • 4. Re: EJB Design Patterns - primary key 'sequence block'
                Darran Lofthouse Master

                Yes some of the reserved range will be lost if the session bean gets removed from the pool or if JBoss is shutdown, this is something you would need to be prepared to accept.

                The other issue that you would need to be aware of is that the integers will not necesarily be used in order, as multiple session beans will each have a different range reserved.