@Override
  public NucleusConnection getNucleusConnection(ExecutionContext ec) {
    ConnectionFactory cf = connectionMgr.lookupConnectionFactory(primaryConnectionFactoryName);

    final ManagedConnection mc;
    final boolean enlisted;
    enlisted = ec.getTransaction().isActive();
    mc =
        cf.getConnection(
            enlisted ? ec : null,
            ec.getTransaction(),
            null); // Will throw exception if already locked

    // Lock the connection now that it is in use by the user
    mc.lock();

    Runnable closeRunnable =
        new Runnable() {
          public void run() {
            // Unlock the connection now that the user has finished with it
            mc.unlock();
            if (!enlisted) {
              // TODO Anything to do here?
            }
          }
        };
    return new NucleusConnectionImpl(mc.getConnection(), closeRunnable);
  }
 /**
  * Constructor.
  *
  * @param ec The ExecutionContext providing the transaction.
  */
 public JPAEntityTransaction(ExecutionContext ec) {
   this.tx = ec.getTransaction();
 }