users@glassfish.java.net

Re: JPA concurrent INSERT violating unique constraint

From: Gregory Gerard <ggerard_at_mac.com>
Date: Wed, 10 Nov 2010 13:47:42 -0800

For such a simple task as recording, I'd use UPSERT if available.

If the database is mysql, you can always just grab the connection and do an insert-on-duplicate-key-{update|ignore}.

greg

On Nov 10, 2010, at 13:07, forums_at_java.net wrote:

> 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
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>