users@glassfish.java.net

JPA concurrent INSERT violating unique constraint

From: <forums_at_java.net>
Date: Wed, 10 Nov 2010 13:07:42 -0800

Hi,

Most users enter my application by clicking a link in an email.  Many don't
realize that you only need to single click links on the Internet so they
double or even tripple click.  This causes multiple browser windows to open
at the same time and load their data.   This usually does not cause a
problem, but when something is wrong with their data (such as time of day,
etc.) then the application:

 

1) inserts a record in a db table called "transactions"

2) either inserts or updates a record in a table called "stats" for the
customer ID the user is trying to access and the current date. 

 

The stats table has one record per day per customer ID and includes columns
for things such as "total_success", "total_failed", etc.   There is a
unique constraint on the customer ID and date.    

The problem I'm having is when the user double or tripple clicks and there is
an error, AND this is the first time a user wants to access the particular
customer for the day, then each of the threads tries to INSERT a new record
with the same data causing a "duplicate key value violates unique constraint"
error.

 

I've thought about things like "read uncommitted" transaction isolation in my
JDBC connection pool, and synchronizing the "recordTransaction" method in my
code.  Neither help.  

 

Can anyone provide some suggestions?

 

Thanks,

Ryan

 


--
[Message sent by forum member 'rdelaplante']
View Post: http://forums.java.net/node/715588