Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7961037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:55:14+00:00 2026-06-04T04:55:14+00:00

I have this transaction: em.getTransaction().begin(); { final Payment payment = em.find(Payment.class, id); if (payment.status

  • 0

I have this transaction:

em.getTransaction().begin();
{
    final Payment payment = em.find(Payment.class, id);
    if (payment.status != Status.INIT)
        throw new IllegalStateException("Cannot set to PAID, is not INIT but " + status);

    payment.status = Status.PAID;

}
em.getTransaction().commit();
log.info("Payment " + id + " was paid");

However, as you can see here, the transaction does not prevent a race condition:

[11:10:18.265] INFO  [PaymentServlet] [MSP] Status COMPLETED 
[11:10:18.265] INFO  [PaymentServlet] Payment c76f9e75-99d7-4721-a8ac-e3a638dd8317 was paid 
[11:10:18.267] INFO  [PaymentServlet] [MSP] Status COMPLETED 
[11:10:18.267] INFO  [PaymentServlet] Payment c76f9e75-99d7-4721-a8ac-e3a638dd8317 was paid 

The payment is set to PAID twice. My exception is not thrown, nor is there a rollback or anything.

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:55:17+00:00Added an answer on June 4, 2026 at 4:55 am

    You need to use optimistic locking. Optimistic locking is where conflicting updates are rare so it is acceptable to rollback the occasional transaction when it occurs. Pessimistic locking causes the database to hold a lock on the object while it’s in use, effectively single-threading everything and potentially causing performance problems. See http://en.wikibooks.org/wiki/Java_Persistence/Locking#JPA_2.0_Locking for a more detailed explanation.

    To solve the problem here, you should add a field to Payment (the traditional declaration is private Long version) and give it the JPA @Version annotation. If you’re managing your schema manually, ensure that a corresponding column exists in the right table. JPA will then use this field to check for conflicting updates and roll back the transaction if a conflict exists.

    Update: More on pessimistic locking here: https://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and In short, you can configure JPA to lock objects, but it’s extremely rare that it’s a good idea to do so. Put another way, if you were hand-coding queries to JDBC, you’d have to write in “for update” at the end of each select to cause pessimistic locking; the default is not to lock on read because it makes databases and database users cry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in Transaction.cs using (TransactionScope scope = new TransactionScope()) { //
I have a declarative table defined like this: class Transaction(Base): __tablename__ = transactions id
you have this procedure CREATE PROCEDURE dbo.test1 AS BEGIN begin transaction begin try exec
Say I have this MYBATIS statement: <insert id=insertValue resultType=car > BEGIN TRANSACTION INSERT INTO
If I have a table structure like this: Transaction [TransID, ...] Document [DocID, TransID,
I have this code to get all transaction between 2 dates. I would like
I have this $_POST array coming from a paypal transaction Array ( [address_city] =>
I have this table : TABLE Transaction Trans_ID Name Value Total_Item 100 I1 0.33333333
I just begin my JPA 2.0 studies, and I have this piece of code:
I have this following code:- using (var transactionScope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required)) { bool IsCreated=false;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.