title: Concurrency in Message Driven Bean - Thread safe Java EE5 vs. EE6
body: <p>I have a situation where I need a set of operations be enclosed into a single transaction and be thread safe from a MDB.</p>
  
  <p>If thread A executes the instruction 1, do not want other threads can read, at least not the same, data that thread A is processing.
  In the code below since IMAGE table contains duplicated data, coming from different sources, this will lead in a duplicated INFRANCTION. Situation that needs to be avoided.</p>
  
  <p>The actual solution that I found is declaring a new transaction for each new message and synchronize the entire transaction. 
  Simplifying the code:</p>
  
  <pre><code>@Stateless
  InfranctionBean{
      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
      checkInfranction(String plate){
          1. imageBean.getImage(plate); // read from table IMAGE
          2. infranctionBean.insertInfranction(String plate); // insert into table INFRANCTION
          3. imageBean.deleteImage(String plate); //delete from table IMAGE
      }
  }
  
  @MessageDriven
  public class ImageReceiver {
  
      private static Object lock = new Object();
  
      public void onMessage(Message msg){
          String plate = msg.plate;
  
          synchronized (lock) {
              infanctionBean.checkInfranction(plate);
          }
      }
  }
  </code></pre>
  
  <p>I am aware that using synchronized blocks inside the EJB is not recommanded by EJB specification. This can lead even in problems if the applicaton server runs in two node cluster. </p>
  
  <p>Seems like EE6 has introduced a solution for this scenario, which is the EJB Singleton.
  In this case, my solution would be something like this:</p>
  
  <pre><code>@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
  @Singleton
  InfranctionBean{
  
      @Lock(LockType.WRITE)
      checkInfranction(String plate){
          1...
          2...
          3...
      }
  }
  </code></pre>
  
  <p>And from MDB would not be neccessary the usage of synchronized block since the container will handle the concurrency. 
  With @Lock(WRITE) the container guarantees the access of single thread inside checkInfranction().</p>
  
  <p>My queston is: How can I handle this situation in EE5? There is a cleaner solution without using synchronized block?</p>
  
  <p>Environment: Java5,jboss-4.2.3.GA,Oracle10.</p>
  
  <p><strong>ACTUAL SOLUTION</strong></p>
  
  <pre><code>@Stateless
  InfranctionBean{
      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
      checkInfranction(String plate){    
          1. imageBean.lockImageTable(); // lock table IMAGE in exclusive mode
          2. imageBean.getImage(plate); // read from table IMAGE
          3. infranctionBean.insertInfranction(String plate); // insert into table INFRANCTION
          4. imageBean.deleteImage(String plate); //delete from table IMAGE
      }
  }
  
  @MessageDriven
  public class ImageReceiver {
      public void onMessage(Message msg){
          infanctionBean.checkInfranction(msg.plate);
      }
  }
  </code></pre>
  
  <p>On 20.000 incoming messages (half of them simultaneously) seems the application works ok.  </p>