public static String obtainLock(EntityManager em,long expiration,long type,String note){
  EntityTransaction tx=null;
  try {
    long now=System.currentTimeMillis();
    tx=em.getTransaction();
    tx.begin();
    GlobalInterlock lock=_findAndRefreshLock(em,type);
    if (lock != null && now - lock.lockTime > expiration) {
      em.remove(lock);
      em.flush();
    }
    tx.commit();
  }
 catch (  Exception ex) {
    LOGGER.warn("An error occurred trying to refresh the type {} lock: {}",type,ex.getMessage());
    LOGGER.debug(ex.getMessage(),ex);
    if (tx != null && tx.isActive()) {
      tx.rollback();
    }
  }
  try {
    tx=em.getTransaction();
    tx.begin();
    GlobalInterlock lock=em.merge(new GlobalInterlock(type,note));
    em.flush();
    tx.commit();
    return Long.toHexString(lock.lockTime);
  }
 catch (  Exception ex) {
    throw new GlobalInterlockException("Could not obtain " + type + " lock",ex);
  }
 finally {
    if (tx != null && tx.isActive()) {
      tx.rollback();
    }
  }
}

public static String obtainLock ( EntityManager em , long expiration , long type , String note ) { EntityTransaction tx = null ; try { long now = System . currentTimeMillis ( ) ; tx = em . getTransaction ( ) ; tx . begin ( ) ; GlobalInterlock lock = _findAndRefreshLock ( em , type ) ; if ( lock != null && now - lock . lockTime > expiration ) { em . remove ( lock ) ; em . flush ( ) ; } tx . commit ( ) ; } catch ( Exception ex ) { LOGGER . warn ( "An error occurred trying to refresh the type {} lock: {}" , type , ex . getMessage ( ) ) ; LOGGER . debug ( ex . getMessage ( ) , ex ) ; if ( tx != null && tx . isActive ( ) ) { tx . rollback ( ) ; } } try { tx = em . getTransaction ( ) ; tx . begin ( ) ; GlobalInterlock lock = em . merge ( new GlobalInterlock ( type , note ) ) ; em . flush ( ) ; tx . commit ( ) ; return Long . toHexString ( lock . lockTime ) ; } catch ( Exception ex ) { throw new GlobalInterlockException ( "Could not obtain " + type + " lock" , ex ) ; } finally { if ( tx != null && tx . isActive ( ) ) { tx . rollback ( ) ; } } }
obtains a global lock of a given type .
