Package com.sun.sgs.service

Examples of com.sun.sgs.service.Transaction


    */
    public T joinTransaction(TransactionContextFactory<T> contextFactory) {
  if (contextFactory == null) {
      throw new NullPointerException("null contextFactory");
  }
        Transaction txn = txnProxy.getCurrentTransaction();
        if (txn == null) {
            throw new TransactionNotActiveException(
                "No transaction is active");
        }
        T context = currentContext.get();
        if (context == null) {
            if (logger.isLoggable(Level.FINER)) {
                logger.log(Level.FINER, "join txn:{0}", txn);
            }
            context = contextFactory.createContext(txn);
            currentContext.set(context);
            txn.join(contextFactory.getParticipant());
        } else if (!txn.equals(context.getTransaction())) {
            clearContext();
            throw new IllegalStateException(
                "Wrong transaction: Expected " + context.getTransaction() +
                ", found " + txn);
        }
View Full Code Here


     * @throws  TransactionNotActiveException if no transaction is active
     * @throws  IllegalStateException if there is a problem with the
     *    state of the transaction.
     */
    public T getContext() {
        Transaction txn = txnProxy.getCurrentTransaction();
        T context = currentContext.get();
        if (context == null) {
      throw new IllegalStateException(
    "Not participating in transaction " + txn);
        } else if (!txn.equals(context.getTransaction())) {
            throw new IllegalStateException(
                "Wrong transaction: Expected " + context.getTransaction() +
                ", found " + txn);
        }
        return context;
View Full Code Here

                                                 task.getOwner(),
                                                 task.getStartTime(),
                                                 waitSize);
                task.incrementTryCount();

                Transaction transaction = null;

                try {
                    // setup the transaction state
                    TransactionHandle handle =
                        transactionCoordinator.createTransaction(unbounded);
                    transaction = handle.getTransaction();
                    ContextResolver.setCurrentTransaction(transaction);
                   
                    try {
                        // notify the profiler and access coordinator
                        profileCollectorHandle.noteTransactional(
                                                    transaction.getId());
                        accessCoordinator.
                            notifyNewTransaction(transaction,
             task.getStartTime(),
                                                 task.getTryCount());

                        // run the task in the new transactional context
                        task.getTask().run();
                    } finally {
                        // regardless of the outcome, always clear the current
                        // transaction state before proceeding...
                        ContextResolver.clearCurrentTransaction(transaction);
                    }

                    // try to commit the transaction...note that there's the
                    // chance that the application code masked the orginal
                    // cause of a failure, so we'll check for that first,
                    // re-throwing the root cause in that case
                    if (transaction.isAborted()) {
                        throw transaction.getAbortCause();
                    }
                    handle.commit();

                    // the task completed successfully, so we're done
                    profileCollectorHandle.finishTask(task.getTryCount());
                    task.setDone(null);
                    return true;
                } catch (InterruptedException ie) {
                    // make sure the transaction was aborted
                    if (!transaction.isAborted()) {
                        transaction.abort(ie);
                    }
                    profileCollectorHandle.finishTask(task.getTryCount(), ie);
                    // if the task didn't finish because of the interruption
                    // then we want to note that and possibly re-queue the
                    // task to run in a usable thread
                    if (task.setInterrupted() && retryOnInterruption) {
                        if (!handoffRetry(task, ie)) {
                            // if the task couldn't be re-queued, then there's
                            // nothing left to do but drop it
                            task.setDone(ie);
                            if (logger.isLoggable(Level.WARNING)) {
                                logger.logThrow(Level.WARNING, ie, "dropping " +
                                                "an interrupted task: {0}" +
                                                task);
                            }
                        }
                    }
                    // always re-throw the interruption
                    throw ie;
                } catch (Throwable t) {
                    // make sure the transaction was aborted
                    if ((transaction != null) && (!transaction.isAborted())) {
                        transaction.abort(t);
                    }
                    profileCollectorHandle.finishTask(task.getTryCount(), t);
                    // some error occurred, so see if we should re-try
                    if (!shouldRetry(task, t)) {
                        // the task is not being re-tried
View Full Code Here

        // No need to buffer it.
        if (!proxy.inTransaction()) {
            handler.publish(record);
            return;
        }
  Transaction txn = proxy.getCurrentTransaction();
  if (txn == null) {
      // in the event that a TransactionalHandler is used
      // outside the scope of a transaction (which could happen
      // if it is used by certain classes like DataStoreImpl),
      // then we just pass the log record on through without
      // buffering
      handler.publish(record);
        } else {
      Queue<LogRecord> records = bufferedRecords.get(txn);
      if (records == null) {
    txn.join(this);
    records = new ArrayDeque<LogRecord>();
    // this code path is guaranteed to be unique by way of
    // the transaction's uniqueness, so we don't need to
    // worry about a race condition with putting the queue
    // into the map
View Full Code Here

     * @throws TransactionNotActiveException if there is no currently active
     *                                       transaction
     * @throws TransactionTimeoutException if the transaction has timed out
     */
    static Transaction getCurrentTransaction() {
        Transaction txn = currentTransaction.get();

        if (txn == null) {
            throw new TransactionNotActiveException("no current transaction");
        }

        if (txn.isAborted()) {
            throw new TransactionNotActiveException("Transaction is aborted",
                                                    txn.getAbortCause());
        }

        txn.checkTimeout();

        return txn;
    }
View Full Code Here

     * the state of that transaction.
     *
     * @return {@code true} if there is a current transaction
     */
    static boolean inTransaction() {
        Transaction txn = currentTransaction.get();
        return (txn != null);
    }
View Full Code Here

    static void setCurrentTransaction(Transaction transaction) {
        if (transaction == null) {
            throw new NullPointerException("null transactions not allowed");
        }

        Transaction txn = currentTransaction.get();
        if (txn != null) {
            throw new IllegalStateException("an active transaction is " +
                                            "currently running");
        }
View Full Code Here

    static void clearCurrentTransaction(Transaction transaction) {
        if (transaction == null) {
            throw new NullPointerException("transaction cannot be null");
        }

        Transaction txn = currentTransaction.get();
        if (txn == null) {
            throw new IllegalStateException("no active transaction to clear");
        }
        if (!txn.equals(transaction)) {
            throw new IllegalArgumentException("provided transaction is " +
                                               "not currently active");
        }

        currentTransaction.set(null);
View Full Code Here

    /**
     * Checks that the specified context is currently active, throwing
     * TransactionNotActiveException if it isn't.
     */
    static void checkTransaction(Transaction txn) {
  Transaction currentTxn = txnProxy.getCurrentTransaction();
  if (currentTxn != txn) {
      throw new TransactionNotActiveException(
     "mismatched transaction; expected " + currentTxn + ", got " +
    txn);
  }
View Full Code Here

     * locker2:      => blocked
     * locker3:      => blocked
     */
    @Test
    public void testUpgradeWaiterConflictTimesOut() throws Exception {
  Transaction txn = new DummyTransaction();
  locker = createTxnLocker(lockManager, txn, 0);
  assertGranted(acquireLock(locker, "o1", false));
  Locker<String> locker2 = createLocker(lockManager);
  assertGranted(acquireLock(locker2, "o1", false));
  Locker<String> locker3 = createTxnLocker(
      lockManager, new DummyTransaction(txn.getTimeout() / 3), 0);
  AcquireLock acquire3 = new AcquireLock(locker3, "o1", true);
  acquire3.assertBlocked();
  AcquireLock acquire2 = new AcquireLock(locker2, "o1", true);
  acquire2.assertBlocked();
  lockManager.releaseLock(locker, "o1");
  acquire2.assertBlocked();
  acquire3.assertBlocked();
  Thread.sleep(txn.getTimeout() / 2);
  assertTimeout(acquire3.getResult(), locker2);
  assertGranted(acquire2.getResult());
    }
View Full Code Here

TOP

Related Classes of com.sun.sgs.service.Transaction

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.