persistence/play-java-jpa/src/main/java/play/db/jpa/DefaultJPAApi.java
public <T> T withTransaction(String name, boolean readOnly, Function<EntityManager, T> block) {
    EntityManager entityManager = null;
    EntityTransaction tx = null;

    try {
      entityManager = em(name);

      if (entityManager == null) {
        throw new RuntimeException("Could not create JPA entity manager for '" + name + "'");
      }

      if (entityManagerContext != null) {
        entityManagerContext.push(entityManager, true);
      }

      if (!readOnly) {
        tx = entityManager.getTransaction();
        tx.begin();
      }

      T result = block.apply(entityManager);

      if (tx != null) {
        if (tx.getRollbackOnly()) {
          tx.rollback();
        } else {
          tx.commit();
        }
      }

      return result;

    } catch (Throwable t) {
      if (tx != null) {
        try {
          if (tx.isActive()) {
            tx.rollback();
          }
        } catch (Exception e) {
          logger.error("Could not rollback transaction", e);
        }
      }
      throw t;
    } finally {
      if (entityManager != null) {
        if (entityManagerContext != null) {
          entityManagerContext.pop(true);
        }
        entityManager.close();
      }
    }
  }
public <T> T withTransaction(String name, boolean readOnly, Function<EntityManager, T> block) {
    EntityManager entityManager = null;
    EntityTransaction tx = null;

    try {
      entityManager = em(name);

      if (entityManager == null) {
        throw new RuntimeException("Could not create JPA entity manager for '" + name + "'");
      }

      if (entityManagerContext != null) {
        entityManagerContext.push(entityManager, true);
      }

      if (!readOnly) {
        tx = entityManager.getTransaction();
        tx.begin();
      }

      T result = block.apply(entityManager);

      if (tx != null) {
        if (tx.getRollbackOnly()) {
          tx.rollback();
        } else {
          tx.commit();
        }
      }

      return result;

    } catch (Throwable t) {
      if (tx != null) {
        try {
          if (tx.isActive()) {
            tx.rollback();
          }
        } catch (Exception e) {
          logger.error("Could not rollback transaction", e);
        }
      }
      throw t;
    } finally {
      if (entityManager != null) {
        if (entityManagerContext != null) {
          entityManagerContext.pop(true);
        }
        entityManager.close();
      }
    }
  }
public < T > T withTransaction ( String name , boolean readOnly , Function < EntityManager , T > block ) { EntityManager entityManager = null ; EntityTransaction tx = null ; try { entityManager = em ( name ) ; if ( entityManager == null ) { throw new RuntimeException ( "Could not create JPA entity manager for '" + name + "'" ) ; } if ( entityManagerContext != null ) { entityManagerContext . push ( entityManager , true ) ; } if ( ! readOnly ) { tx = entityManager . getTransaction ( ) ; tx . begin ( ) ; } T result = block . apply ( entityManager ) ; if ( tx != null ) { if ( tx . getRollbackOnly ( ) ) { tx . rollback ( ) ; } else { tx . commit ( ) ; } } return result ; } catch ( Throwable t ) { if ( tx != null ) { try { if ( tx . isActive ( ) ) { tx . rollback ( ) ; } } catch ( Exception e ) { logger . error ( "Could not rollback transaction" , e ) ; } } throw t ; } finally { if ( entityManager != null ) { if ( entityManagerContext != null ) { entityManagerContext . pop ( true ) ; } entityManager . close ( ) ; } } }
Run a block of code with a newly created EntityManager for the named Persistence Unit.

@param name The persistence unit name
@param readOnly Is the transaction read-only?
@param block Block of code to execute
@param <T> type of result
@return code execution result
Run a block of code with a newly created EntityManager for the named Persistence Unit .