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

  • Home
  • SEARCH
  • 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 8163091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:56:36+00:00 2026-06-06T18:56:36+00:00

I have a SLSB which increments a number in an entity. If two threads

  • 0

I have a SLSB which increments a number in an entity. If two threads reach the SLSB at the same time, I’m getting the same number in both requests.

SLSB extract

@Stateless(mappedName = "ejb/CustomerManager")
public class CustomerManagerBean implements CustomerManager {
...
    public String recoverName(int id) {
        Customer customer = (Customer) em.createQuery("from Customer where id = :id").setParameter("id", id).getSingleResult();     
        int oldValue = customer.getValue();
        int newValue = oldValue + 1;
        customer.setValue(newValue);        
     [BP]   return customer.getName() + " value=" + customer.getValue();
    }
...
}

Entity extract

@Entity
public class Customer implements Serializable {
    @Id
    private int id;
    private int value;
}

To test the problem, I have a breakpoint at the line marked with [BP] in the SLSB recoverName method. Then a make two calls from two separated browser pages. At the breakpoint the value is the same for the two calls.

Shouldn’t the second call throw some kind of exception when it tried to modify the value with the setter ?

I’m using JBoss 5 as the AS and MySql or Oracle as the DB (tried with both)

Thanks for the help

  • 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-06T18:56:37+00:00Added an answer on June 6, 2026 at 6:56 pm

    You would get an exception at flush time if you added a @Version annotated field to your entity, which would be used by JPA for optimistic locking.

    Each time JPA updates an entity, it would compare the version in memory with the version in database, and would throw an exception if they don’t match. If they match, the version would be incremented.

    Just add the following to your entity:

    @Column(name = "version")
    @Version
    private long version;
    

    (and add the corresponding column to the database, of course)

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

Sidebar

Related Questions

have written this little class, which generates a UUID every time an object of
Have deployed numerous report parts which reference the same view however one of them
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have 2 tables in Access 2007, both lists of certain tasks to be accomplished.
I have a a number of jar files that perform rmi. These are all
Have a rather abstract question for you all. I'm looking at getting involved in
I have a ConfigParameter SLSB that reads configuration values from the ejb-jar.xml file. This
Have a really weird one here. The app has two targets, just to be
I have a stateless session bean which needs access to a factory class. Is
Have a simple one-off tasks which needs a progress bar. OpenSSL has a useful

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.