aspose file tools*
The moose likes JDBC and the fly likes Tomcat 5 _ Mysql 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Tomcat 5 _ Mysql 5 " Watch "Tomcat 5 _ Mysql 5 " New topic
Author

Tomcat 5 _ Mysql 5

Kalyani Nayak
Greenhorn

Joined: Jan 01, 2006
Posts: 25
I am using Apache Tomcat5.0, And Mysql5 .
In my web application I want to generate the primary keys.
I am using jsp and servlets technologies.
Just now I have designed it as follow
While creating new record I am fetching the max(id) from the corresponding table
Incrementing the value by one and adding in into create query.
This entire code I am writting in a 'synchronized' block to avoid concurrency.

Please let me know any disadvantages of such method, and if there is any better solution for it without using any third party jars.
Or if no, then please suggest me the better third party jar to generate the keys from servlet.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2500
    
    8

The disadvantage are that:
- select max(id) might lock your table for a moment,
- does not guarantee a unique id if you request it on different places in your code (synchronized will not help you in that case),
- it's not very performant.

When I use MySQL, I define an auto-increment key, and ask my statement what id has been generated (pseudocode):



The insert statement should not insert a value into the auto-generated key. MySQL will take care of that.

Regards, Jan
[ February 15, 2007: Message edited by: Jan Cumps ]

OCUP UML fundamental and ITIL foundation
youtube channel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tomcat 5 _ Mysql 5