コード例 #1
0
    public void afterCompletion(int status) {

      switch (status) {
        case Status.STATUS_COMMITTED:
          if (logger.isDebugEnabled()) {
            logger.debug("Jta Txn [" + transaction.getId() + "] committed");
          }
          transactionManager.notifyOfCommit(transaction);
          // Remove this transaction object as it is completed
          DefaultTransactionThreadLocal.replace(serverName, null);
          break;

        case Status.STATUS_ROLLEDBACK:
          if (logger.isDebugEnabled()) {
            logger.debug("Jta Txn [" + transaction.getId() + "] rollback");
          }
          transactionManager.notifyOfRollback(transaction, null);
          // Remove this transaction object as it is completed
          DefaultTransactionThreadLocal.replace(serverName, null);
          break;

        default:
          logger.debug("Jta Txn [" + transaction.getId() + "] status:" + status);
      }
    }
コード例 #2
0
  /**
   * Looks for a current Spring managed transaction and wraps/returns that as a Ebean transaction.
   *
   * <p>Returns null if there is no current spring transaction (lazy loading outside a spring txn
   * etc).
   */
  public Object getCurrentTransaction() {

    TransactionSynchronizationRegistry syncRegistry = getSyncRegistry();

    SpiTransaction t = (SpiTransaction) syncRegistry.getResource(EBEAN_TXN_RESOURCE);
    if (t != null) {
      // we have already seen this transaction
      return t;
    }

    // check current Ebean transaction
    SpiTransaction currentEbeanTransaction = DefaultTransactionThreadLocal.get(serverName);
    if (currentEbeanTransaction != null) {
      // NOT expecting this so log WARNING
      String msg =
          "JTA Transaction - no current txn BUT using current Ebean one "
              + currentEbeanTransaction.getId();
      logger.warn(msg);
      return currentEbeanTransaction;
    }

    UserTransaction ut = getUserTransaction();
    if (ut == null) {
      // no current JTA transaction
      if (logger.isDebugEnabled()) {
        logger.debug("JTA Transaction - no current txn");
      }
      return null;
    }

    // This is a transaction that Ebean has not seen before.

    // "wrap" it in a Ebean specific JtaTransaction
    String txnId = String.valueOf(System.currentTimeMillis());
    JtaTransaction newTrans = new JtaTransaction(txnId, true, ut, dataSource, transactionManager);

    // create and register transaction listener
    JtaTxnListener txnListener = createJtaTxnListener(newTrans);

    syncRegistry.putResource(EBEAN_TXN_RESOURCE, newTrans);
    syncRegistry.registerInterposedSynchronization(txnListener);

    // also put in Ebean ThreadLocal
    DefaultTransactionThreadLocal.set(serverName, newTrans);
    return newTrans;
  }