title: Set pessimistic lock on entity with EntityManager
body: <p>Consider the following situation:
  We receive a request from a web service which updates our entity. Sometimes we might get two requests at (almost) the same time. We had situations in which our entity looked completely wrong, because of concurrent updates. The idea is to lock the entity pessimistic so that whenever the first request comes it instantly locks the entity and the second request can't touch it (Optimistic locking is no alternative for us). I wrote an integration test to check this behaviour.</p>
  
  <p>I got an integration test which looks like the following:</p>
  
  <pre><code>protected static TestRemoteFacade testFacade;
  
  @BeforeClass
  public static void setup() {
      testFacade = BeanLocator.lookupRemote(TestRemoteFacade.class, TestRemoteFacade.REMOTE_JNDI_NAME, TestRemoteFacade.NAMESPACE);
  }
  
  @Test
  public void testPessimisticLock() throws Exception {
      testFacade.readPessimisticTwice();
  }
  </code></pre>
  
  <p>which calls the bean</p>
  
  <pre><code>@Stateless
  @Clustered
  @SecurityDomain("myDomain")
  @RolesAllowed({ Roles.ACCESS })
  public class TestFacadeBean extends FacadeBean implements TestRemoteFacade {
  
      @EJB
      private FiolaProduktLocalFacade produkt;
  
      @Override
      public void readPessimisticTwice() {
          produkt.readPessimisticTwice();
      }
  }
  </code></pre>
  
  <p>with <code>produkt</code> being a bean itself</p>
  
  <pre><code>@Stateless
  @Clustered
  @SecurityDomain("myDomain")
  @RolesAllowed({ Roles.ACCESS })
  public class ProduktFacadeBean implements ProduktLocalFacade {
  
      @Override
      public void readPessimisticTwice() {
          EntityManager entityManager = MyService.getCrudService().getEntityManager();
          System.out.println("Before first try.");
          entityManager.find(MyEntity.class, 1, LockModeType.PESSIMISTIC_WRITE);
          System.out.println("Before second try.");
          entityManager.find(MyEntity.class, 1, LockModeType.PESSIMISTIC_WRITE);
          System.out.println("After second try.");
      }
  }
  </code></pre>
  
  <p>with </p>
  
  <pre><code>public class MyService {
  
      public static CrudServiceLocalFacade getCrudService() {
          return CrudServiceLookup.getCrudService();
      }
  
  }
  
  public final class CrudServiceLookup {
  
      private static CrudServiceLocalFacade crudService;
  
      private CrudServiceLookup(){
      }
  
      public static CrudServiceLocalFacade getCrudService() {
          if (crudService == null)
              crudService = BeanLocator.lookup(CrudServiceLocalFacade.class, CrudServiceLocalFacade.LOCAL_JNDI_NAME);
          return crudService;
      }
  
      public static void setCrudService(CrudServiceLocalFacade crudService) {
          CrudServiceLookup.crudService = crudService;
      }
  
  }
  
  @Stateless
  @Local(CrudServiceLocalFacade.class)
  @TransactionAttribute(TransactionAttributeType.MANDATORY)
  @Interceptors(OracleDataBaseInterceptor.class)
  public class CrudServiceFacadeBean implements CrudServiceLocalFacade {
  
      private EntityManager em;
  
      @Override
      @PersistenceContext(unitName = "persistence_unit")
      public void setEntityManager(EntityManager entityManager) {
          em = entityManager;
      }
  
      @Override
      public EntityManager getEntityManager() {
          return em;
      }
  
  }
  </code></pre>
  
  <p>The problem that arises now is: If I start the integration test once with a breakpoint at <code>System.out.println("Before second try.");</code> and then start the integration test a second time, the latter one can still read MyEntity. Remarkable is that they were <strong>different</strong> instances (I made this observation on the instanceId in debug mode). This suggests that the <code>entityManager</code> didn't share his hibernate context.</p>
  
  <p>I made the following observations:</p>
  
  <ul>
  <li>Whenever I call a setter on <code>entity</code> and save it to the db, the lock is aquired. But this is not what I need. I need the lock without having modified the entity.</li>
  <li>I tried the method <code>entityManager.lock(entity, LockModeType.PESSIMISTIC_WRITE)</code> as well, but the behaviour was the same.</li>
  <li>I found Transaction settings in DBVisualizer. At the moment it is set to TRANSACTION_NONE. I tried all the others (TRANSACTION_READ_UNCOMMITTED, TRANSACTION_READ_COMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE) as well, without any success.</li>
  <li>Let the first thread read the entity, then the second thread read the same entity. Let the first tread modify the entity and then the second modify it. Then let both save the entity and whoever saves the entity last wins and no exceptions will be thrown.</li>
  </ul>
  
  <p>How can I read an object pessimistic, that means: Whenever I load an entity from the db I want it to be locked immediately (even if there was no modification).</p>