Concurrency in Database

Hi, i have a Web App and i have a problem when i insert values into a table. Let's say that I have two tables A and B, in order to insert a value in A i have to read, first, a value from table B, this value in table B is just like a sequence, i mean is a number that is increased everytime i register a row in table A. This works well when there is just one client registering values in A, but the problem is when i have two clients trying to register in table A at the same time, both are reading the same value in B so when they are trying to register in A an exception of "a repeated primary key" appears and just one of the clients can register in A meanwhile the other gets an exception.

This problem is about concurrency and right now i'm using the autocommit option on, how can i deal with this kind of problem?, keeping in mind that is more likely to have in my web application more than 2 people trying to register a row in table A.

[956 byte] By [pompeighuIIa] at [2007-11-27 9:54:08]
# 1
I think you can solve the problem making your insert/update methods synchronized.
manuel.leiriaa at 2007-7-13 0:23:59 > top of Java-index,Java Essentials,Java Programming...
# 2
put into a transaction. What kind of DB is it? If it is Sybase/MS SQL Server, you can use:Begin TransactionBegin...... your SQLs hereEndCommit
yue42a at 2007-7-13 0:23:59 > top of Java-index,Java Essentials,Java Programming...
# 3

You have a couple of choices. As has already been suggested you can synchronize the methods in your Java code where the updates / inserts begin. Or you can effectively synchronize the db by having the record in table B lock when it is being read or updated.

Which is the correct method is entirely a matter of personal taste and platform. Some databases are better at managing record locks than others. I would generally lean towards putting the lock in the database because it is entirely possible that some other mechanism than my application may be updating the database as well and that would allow me to protect the reads and writes to the db regardless of where they originate.

All of that said, be aware that there is a performance price to be paid regardless of how you choose to do it. You're blocking one attempt while another finishes. That's going to translate directly into one user waiting for an indeterminant amount of time before they can finish their commit. They may never notice it, but be aware that it is there.

Hope this helps,

PS.

puckstopper31a at 2007-7-13 0:23:59 > top of Java-index,Java Essentials,Java Programming...
# 4
i'm using postgresql 8.2
pompeighuIIa at 2007-7-13 0:23:59 > top of Java-index,Java Essentials,Java Programming...
# 5
could anyone give me a hint of how to do a Locking Table?
pompeighuIIa at 2007-7-13 0:23:59 > top of Java-index,Java Essentials,Java Programming...
# 6
http://www.google.com/search?hl=en&q=PostgressSQL+%2B+record+lockFollow the bouncing link.PS.
puckstopper31a at 2007-7-13 0:23:59 > top of Java-index,Java Essentials,Java Programming...