IIRC middlegen has support for generating code for this pattern.
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.
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?
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.