wood burning stoves 2.0*
The moose likes JDBC and the fly likes Protect AutoIncremented Row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Protect AutoIncremented Row" Watch "Protect AutoIncremented Row" New topic
Author

Protect AutoIncremented Row

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

This kind of goes with my other post, but it is different, so here goes.
Let's say I have a table with an auto-incremented field. And when starting a transaction I get the last autoincremented field, then I add 1 to it to get what it will be for my Record.
Now, if I set the isolation level to serializable, is that going to protect my autoincremented value so that it is not used by another transaction?
Also, is there a better way to handle this? I need the autoincremented field value prior to completing the transaction because I am entering data in 2 tables and need that value for the foreign key.
Thanks.


GenRocket - Experts at Building Test Data
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Hey Gregg,
A few questions to help set context. What database are you working with, and are stored procedures an option?
M, author
The Sun Certified Java Developer Exam with J2SE 1.4


Java Regular Expressions
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

MySQL and God I wish I could use Stored Procedures. But MySQL is taking their time implementing them. But I'm sure you already know that.
Thanks for trying to help me. I was beginning to wonder....
BTW - Also using the newest Connector/J
[ March 14, 2003: Message edited by: Gregg Bolinger ]
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
At a high level, I'd suggest a synchronized method to access the db: probably synchronized on a given db lock object, which is maybe a static in a differ class all together. This assumes the no other applications are playing with your table, and that any part of the your code that needs to write to that particular table could synchronize on that variable. Thus, you get atomic behavior.
I know the reply's a bit terse, so I'll ask if I've been clear?
M
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I have to assume that other applications ARE accessing the same table at any time. This is an application that is installed on several PC's.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Protect AutoIncremented Row
 
Similar Threads
Transaction Locking Question
Hibernate Question
A Transaction Question
Hibernate Question
synchronization