Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

howto use a sequence as primary key in entity beans  RSS feed

 
balazs korossy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
anybody got a shoot to this?
I created an entity bean, but what if I want to use a sequence as the primary key, should I create another entity bean just for reading out a sequence?
please help
Thx
Balazs
------------------
--- www.motoros.hu ---
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by balazs korossy:
Hi,
anybody got a shoot to this?
I created an entity bean, but what if I want to use a sequence as the primary key, should I create another entity bean just for reading out a sequence?
please help
Thx
Balazs

Hi
Interesting, this is an ongoing topic on the EJB-INTEREST mailing list from Sun.
Pretty much one of the problems with this type of scenario (Entity Bean sequence manager) is that
sequence number generators just don't fit nicely into an Entity bean model.
- Avi Kivity (from the list)
Entity beans are 'pooled' instances and not realy shared, since you would have to guarantee the integrity of your numbers you really need a singleton model. If each pooled instance of your Sequence numbers is accessed you would have duplicate numbers all over the place in a high volume environment.
To fix this I would possibly use a stateless session bean (using a synchronized static method) to handle the 'creation / getting' of your numbers. It's almost going to be like your number factory.
There is also a scaling issue here as many many simultaneous calls would be 'waiting in line' until all previous 'number request' were processed. (I also am not even looking at the fact that this COULD be a clustered environment.. that's a whole other set of bugs)
Some people on the list are now starting to suggest that if your DB can implement something like the Oracle "Sequeneces", this would be the best approach. I don't know Oracle so I can only quote the list for you here..

("SELECT MYSEQUENCE.NEXTVAL FROM DUAL")

I hope this at least gives you a direction to go in for you solution.

[This message has been edited by John Bateman (edited August 01, 2001).]
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No question about it - this is a major pain. I actually ended up having to yank auto-sequencing off a table the first time I tried this. The EJBWizard creates a block of code (commented out, by default) in the controller module for a detail-level JSP (both files created in prototype by the EJBWizard). That code block contains logic to acquire a new key from an EJB named Keytable, which fronts for a table containing the next available key (long) for a named table (String). To actually use it, you'd uncomment the code, put an import statement in, and, of course, construct a suitable implementation of Keytable.
I think the Session EJB facade's more "correct", but this works, and is less trouble to implement - one Keytable can serve an entire database.
 
David Follow
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
this thread is fairly old and I am wondering if it only regards EJB1.1
or does it also regard EJB2.0? Meaning, do I also have to handle my own
sequencing using EJB2.0?
Is the approach of using a stateless session bean with a synchronized
counter method still "best practice"?
TIA, misos69
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!