aspose file tools
The moose likes JDBC and the fly likes Table Lock Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Reply locked New topic
Author

Table Lock

Raymond Ong
Ranch Hand

Joined: Jul 17, 2005
Posts: 46
Hi,

I have a several instances of Message Driven Beans accessing a single MySQL table. My first procedure "selects" a user id, if the user id does not exist it then executes an "insert" statement. Problem is, with my multiple MDB instances, there is a slight overlap when two identical user id's are inserted. The first MDB attempts to insert the user id, just after the second MDB finished checking if the same user id exists. when the second MDB tries to insert, of course it gets a duplicate pk error.
My question is how to do table locks in MDBs or MySQL.

Thanks in advance lot for the help
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I don't think this is possible. I think you will need to use the facilities of the database to generate new unique numbers and the like. Check the MySQL Java documentation. Its probably described with the driver.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You shouldn't be using table locks - why lock the entire table when you only (possibly) need to lock the row?

MySQL should support row locking. You could use SELECT ... FOR UPDATE to lock it. But I think what you need is a proper key generation strategy rather than relying on locks. Or use an optimistic locking pattern.
[ September 30, 2005: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Raymond Ong
Ranch Hand

Joined: Jul 17, 2005
Posts: 46
Hi Paul, let me answer you in the other thread instead.

http://www.coderanch.com/t/317089/EJB-JEE/java/Synchronizing-MDBs

Thanks
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Table Lock
 
Similar Threads
Concurrent Users inserting a record
Synchronizing MDBs
error in database
Know auto incremented pimary key
MDB and BMT