简体   繁体   中英

Handling dirty read in Spring,Hibernate with JPA

We have an web application which is running under Load balancer.Two users are trying to do the same operation for example both are raising a same order at same time which is creating duplicate orders.

Before one transaction commit another request is checking is there any order.It's creating duplicate entries.Both the request are trying to process at the same time.

  1. User A & B raise an order at same time.
  2. User A's transaction validates that, is there any existing entry for same order. If there is no other entry for the same order, it will create an entry.
  3. User B's transaction also validates and create the same.
  4. If A's transaction is complete, then B's Transaction will not happen. But Before the transaction commit, both the requests are valid, so its creating two entries for the same order (duplicate. )

We have tried the below options,

  1. Lock the entitymanager with Lock Type as OPTIMISTIC_FORCE_INCREMENT
  2. Make method as synchronized
  3. Isolation Level as SERIALIZABLE

but none of the options helped.

Kindly suggest how to proceed.

Thanks

Set up a separate database table containing the next order number.

Split the creation of an order into two separate database transactions.

The first one reads and updates the order number.

The second one stores the order information.

When processing an order, if the first transaction fails, it should be retried before proceeding with the second transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM