/**
  * Prepare a transaction on the given EntityManager, if possible.
  *
  * @param em the EntityManager to prepare
  * @param emf the EntityManagerFactory that the EntityManager has been created with
  * @return an arbitrary object that holds transaction data, if any (to be passed into
  *     cleanupTransaction)
  * @see JpaDialect#prepareTransaction
  */
 private static Object prepareTransaction(EntityManager em, EntityManagerFactory emf) {
   if (emf instanceof EntityManagerFactoryInfo) {
     EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf;
     JpaDialect jpaDialect = emfInfo.getJpaDialect();
     if (jpaDialect != null) {
       return jpaDialect.prepareTransaction(
           em,
           TransactionSynchronizationManager.isCurrentTransactionReadOnly(),
           TransactionSynchronizationManager.getCurrentTransactionName());
     }
   }
   return null;
 }
  /** Borrowed from Seam */
  public int getStatus() {
    if (ptm == null) {
      return TransactionManager.STATUS_NO_TRANSACTION;
    }

    logger.debug(
        "Current TX name (According to TransactionSynchronizationManager) : "
            + TransactionSynchronizationManager.getCurrentTransactionName());
    if (TransactionSynchronizationManager.isActualTransactionActive()) {
      TransactionStatus transaction = null;
      try {
        if (currentTransaction == null) {
          transaction = ptm.getTransaction(td);
          if (transaction.isNewTransaction()) {
            return TransactionManager.STATUS_COMMITTED;
          }
        } else {
          transaction = currentTransaction;
        }
        logger.debug("Current TX: " + transaction);
        // If SynchronizationManager thinks it has an active transaction but
        // our transaction is a new one
        // then we must be in the middle of committing
        if (transaction.isCompleted()) {
          if (transaction.isRollbackOnly()) {
            return TransactionManager.STATUS_ROLLEDBACK;
          }
          return TransactionManager.STATUS_COMMITTED;
        } else {
          if (transaction.isRollbackOnly()) {
            return TransactionManager.STATUS_ROLLEDBACK;
          }
          return TransactionManager.STATUS_ACTIVE;
        }
      } finally {
        if (currentTransaction == null) {
          ptm.commit(transaction);
        }
      }
    }
    return TransactionManager.STATUS_NO_TRANSACTION;
  }