• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

finally decided to generate keys this way

 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I was trying to implement "select max+1.." logic for ID generating with my CMP and went to some oracle sequencing and mysql autoincrement and finally I think I would take the following approach,
1. I would create a table say "MaxPK (BeanName varchar, MaxIdSofar number)"
2. I would create corresponding Entitybean MaxPKEntityBean
3. Whenever I need a new Id for a entitybean e.g. Order or OrderItem I would call the MaxPKEntityBean as,
// to get new id for Order
tempMaxPKEB = maxPKEBHome.findByPrimaryKey("Order");
tempMaxPKEB.getNextId(); // business method I would implement
// to get new id for OrderItem
tempMaxPKEB = maxPKEBHome.findByPrimaryKey("OrderItem");
tempMaxPKEB.getNextId();
In the database table I would have for e.g.,
BeanName MaxIdSoFar
---------- ----------
Order 4
OrdeItem 50
(assuming there were already some orders and orderitems generated...)
How does this sound?
THanks
Maulin
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wowever you decide to back you "next ID table," I recommend against using an entity bean to access it. Use some simple JDBC to do what you need.
I'm curious, why did you bypass using Oracle sequences? While not portable directly to other databases, they're certainly easy and safe. And if you're using WebLogic, it will use sequences directly without your code intervening.
Anyway, I'm glad that ServerSide thread convinced you to do away with "select max(id) + 1."
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
I don't mind using sequences but it just sound too specific. I would be simply stuck with Oracle. But I see that almost 99% people use Oracle with J2EE and thats why its okay to go with sequences. It seems it would be sometime I can be little optimistic and assume that people would have Oracle.
Also, I don't mind coding something specific to oracle as far as I have a way to add/extends classes to make my logic work with other database if required. e.g. If I have sequence logic and I switch to MySQL what are the steps I need to do? Can I just extend my class to generate new ID for MySQL without adding 2 more classes and all you know...So these are the questions scares me...
Now, if I use JDBC from my code it means I need to impose synchronization on my own via a singleton+synchronized access to getNextId() method you know. Again, to make that happen I would need a standalone java class and make that class query some SessionBean to obtain DataSource.getConnection()...right? So I thought if EntityBeans can take care of synch and transaction for me then why not use it...
Regards
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay..
I guess I would switch to UUID
regards
maulin
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maulin,
I guess UUID would be the best bet for your kind of design. But ur more dependant now mainly on the underlying computer's IP Address getting the encoded String type data in hex format.
Do u still want to go with this Sequence Generator pattern?
Rgds,
Seetesh
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seetesh
so what can go wrong with the IP thing?
Thanks
Maulin
 
Seetesh Hindlekar
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Case 1.
U generate a primarykey value based on the IP address of your server and using the ejbCreate() store the same in the database.
If ur using the findByPrimaryKey(primaryKey) and u are assigning the same primarykey value to this method, the findBy...() will return the EJBObject.
Code works fine for the time being.
Case 2.
If the IP address of the server changes due to the company's policy (in real time systems or after a couple of years), then the primary key generated in the process may differ and the value of the primarykey passed to the findByPrimaryKey() method will differ than the value stored in the database resulting in null or exception being thrown or some other value being retrieved which is not the desired value.
Hope I have made my point clear.
Seetesh
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seetesh

then the primary key generated in the process may differ and the value of the primarykey passed to the findByPrimaryKey() method will differ than the value stored in the database

why would this happen?
Because we don't generate PK to use findByPrimaryKey() right? So once generated, PK is just a string which is unique. I just treat the PK as pure string w/o caring what is in there. Everywhere when I want to do lookup via findByPrimaryKey(), I would have to pass the correct key. So, my code wouldn't have any logistic of worrying about IPs specifically. I agree that once IP is changed new keys would be different, but so what? as far as I am not getting duplicates, I am happy.
I hope I didn't miss any critical part in your comment.
Thanks
Maulin
 
Seetesh Hindlekar
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maulin,
>>then the primary key generated in the process may differ and the value of the primarykey passed to the findByPrimaryKey() method will differ than the value stored in the database
>>Because we don't generate PK to use findByPrimaryKey() right? So once generated, PK is just a string which is unique. I just treat the PK as pure string w/o caring what is in there. Everywhere when I want to do lookup via findByPrimaryKey(), I would have to pass the correct key. So, my code wouldn't have any logistic of worrying about IPs specifically. I agree that once IP is changed new keys would be different, but so what? as far as I am not getting duplicates, I am happy.
As mentioned in my mail, u have to be extra careful while passing the PK in the ejbFindByPrimaryKey. Remember this is a crucial topic and as long as u can get over with its fine.
Rest be assured. But still there is this just a small little "bug" u have to take care of.
Seetesh
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The UUID would work great for you, but there would be processing overhead.
Understand that with the code, you can just not set the PK field, and while different appservers handle how to get the PK differently, they are all specified outside of code.
For most cases, you just call the ejbCreate() method with no arguments, and when stored, the appserver will generate it for you based on vendor specifics. I know this works accross WebSphere, JBoss, WebLogic, and Oracle, MySQL, DB2, UDB, SQLServer.
While I see uses for UUID in clustered environments - that's the only place I see it.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic