Solved

Error duplicate key

Posted on 2004-09-20
13
188 Views
Last Modified: 2010-04-01
I have method insert(UserData[] ud)(Servlet) to insert array UserData into DB(Postgres 7.1) when user click Submit button (JSP)
My problems are when more than a user click Submit button simultaneously, Error duplicate key throw.

Before inserting call method ret = getMaxValues() that return max value of primary key from DB and plus 1.
Primary key in each SQL statement has value is ret ++.

Anyone has experienced my problems, please help.

Thank in advance.

JV.
0
Comment
Question by:lam0620
  • 7
  • 2
  • 2
13 Comments
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12108627
I think you should do insert operation in one atomic command like this.

insert into SOME_TABLE(key, field1, field2, ...) values ((select max(key)+1 from SOME_TABLE), some_value1, some_value2, ...);
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12108639
Another way is to lock the table when you get next key and release locking table after you insert record but this way may need you to understand more database you use. This is called "ISOLATION LEVEL". you may read this topic more in google.
0
 

Author Comment

by:lam0620
ID: 12108682
I dont know can  synchronize on method insert(UserData[] ud)?
(
public void synchronize insert(UserData[] ud)?) {
....
}
)

JV.
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12108715
Yes. You can synchronize in method level too. It's an option you can do buy you should keep in mind that there is no other methods do SQL to the same table.
0
 

Author Comment

by:lam0620
ID: 12109153
Hi sompol_kiatkamolchai,

Can you give me a example about using ISOLATION LEVEL?

JV.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 125 total points
ID: 12109176
Isolation Level is feather of each databases. It depends on database you use. It allows us to set whether we want to lock table or lock record in the table.

Here are more information for Postgre.
http://www.postgresql.org/docs/7.4/interactive/transaction-iso.html
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12109184
Transaction Isolation Level concept also implement in persistent mechanism like EJB. In EJB we can set what level of isolation we will use. If we set it to highest(Serialize), it will lock that table and release it after completing operation on that table.
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12109190
It's quite an advance topic for s/b who want to manage transaction on database. If you want to adhoc solve the problem, I recommend you to do insert into table in one atomic operation.
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12109196
Best luck, :)
0
 
LVL 4

Expert Comment

by:Oliver_Dornauf
ID: 12111534
the isolation level is not the problem here and is also not a concept to implement serialisition.
Weather you look the complete datebase nor trasaction acrobatics will prevent you from duplicate keys.
You need to prevent the duplicate submission !ee:http://javaalmanac.com/egs/javax.servlet.jsp/myformts.jsp.html?l=new
0
 
LVL 4

Expert Comment

by:Oliver_Dornauf
ID: 12111550
Uups ..i answerd the wrong thread ..sorry
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
method inner class 6 81
How to group array of objects ? 5 60
squirrelPlay java challenge 40 157
print map entry 34 54
With more and more people tethered to the internet, advertisers can reach you in new and creative ways. Push notifications on your smartphone, customized advertisements based on your previous searches, and email inboxes filled with promotional pitch…
Facing problems with you memory card? Cannot access your memory card? All stored data, images, videos are lost? If these are your questions...than this small article might help you out in retrieving your lost or inaccessible data.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now