1 Reply Latest reply on Jul 20, 2007 3:51 PM by Andrew Rubinger

    EJB with synchronized

    horikawa yoji Newbie

      hi, fellaz.

      i created a class that sums up the prices of the ads and updates the DB records.

      the single price of the test ad was 10.

      it does what it supporsed to do when the concurrent access is "1".
      i did the test using Apache Bench.
      used "ab -n 50 -c 1 <update url>".
      the sum of the price was 500, which was ok.

      the problem appeared when i did "ab -n 50 -c 2 <update url>".

      the total supposed to be 500, but it was something like 350.

      i changed the code a bit with the "synchronized" keyword and made it thread-safe, and it worked fine.

      but the problem is that the EJB prohibits the "synchronized" keyword.

      Is there anyway (annotation or methods) to solve this problem?

      One way that I'm thinking of is to use Singleton method and use "synchronized" keyword inside of the Singleton class...

        • 1. Re: EJB with synchronized
          Andrew Rubinger Master

          According to the EJB3 Spec, 4.7.11, the container is responsible to ensure that only one thread is passed through any session bean instance at any one time. Therefore, using the synchronized on a method would be moot.

          However, it's entirely possible to have many instances accessing the same field (static?) and that leading to your problems...

          Mind posting the session bean code (or relevant portions)?

          S,
          ALR