@Override protected ThreadContext initialValue() { return new ThreadContext(); } };
/** * Bind a new context on the current thread. * @param context the context to bind. */ private void setCurrentContext(ThreadContext context) { if (log.isDebugEnabled()) log.debug("changing current thread context to " + context); if (context == null) throw new IllegalArgumentException("setCurrentContext() should not be called with a null context, clearCurrentContextForSuspension() should be used instead"); contexts.put(Thread.currentThread(), context); if (context.getTransaction() != null) { MDC.put(MDC_GTRID_KEY, context.getTransaction().getGtrid()); } }
@Override public void setTransactionTimeout(int seconds) throws SystemException { if (seconds < 0) throw new BitronixSystemException("cannot set a timeout to less than 0 second (was: " + seconds + "s)"); ThreadContext.getThreadContext().setTimeout(seconds); }
/** * Get the transaction currently registered on the current thread context. * @return the current transaction or null if no transaction has been started on the current thread. */ public BitronixTransaction getCurrentTransaction() { return ThreadContext.getThreadContext().getTransaction(); }
/** * Unlink the transaction from the current thread's context. */ private void clearCurrentContextForSuspension() { if (log.isDebugEnabled()) { log.debug("clearing current thread context: " + ThreadContext.getThreadContext()); } ThreadContext.getThreadContext().clearTransaction(); }
/** * Create a new transaction on the current thread's context. * @return the created transaction. */ private BitronixTransaction createTransaction() { BitronixTransaction transaction = new BitronixTransaction(); ThreadContext.getThreadContext().setTransaction(transaction); MDC.put(MDC_GTRID_KEY, transaction.getGtrid()); return transaction; }
public void resume(Transaction transaction) throws InvalidTransactionException, IllegalStateException, SystemException { if (log.isDebugEnabled()) log.debug("resuming " + transaction); if (transaction == null) throw new InvalidTransactionException("resumed transaction cannot be null"); if (!(transaction instanceof BitronixTransaction)) throw new InvalidTransactionException("resumed transaction must be an instance of BitronixTransaction"); BitronixTransaction tx = (BitronixTransaction) transaction; BitronixTransaction currentTx = getCurrentTransaction(); if (currentTx != null) throw new IllegalStateException("a transaction is already running on this thread"); try { XAResourceManager resourceManager = tx.getResourceManager(); resourceManager.resume(); ThreadContext ctx = new ThreadContext(); ctx.setTransaction(tx); setCurrentContext(ctx); } catch (XAException ex) { String extraErrorDetails = TransactionManagerServices.getExceptionAnalyzer().extractExtraXAExceptionDetails(ex); throw new BitronixSystemException("cannot resume " + tx + ", error=" + Decoder.decodeXAExceptionErrorCode(ex) + (extraErrorDetails == null ? "" : ", extra error=" + extraErrorDetails), ex); } }
currentTx = createTransaction(); ThreadContext threadContext = ThreadContext.getThreadContext(); ClearContextSynchronization clearContextSynchronization = new ClearContextSynchronization(currentTx, threadContext); try { currentTx.getSynchronizationScheduler().add(clearContextSynchronization, Scheduler.ALWAYS_LAST_POSITION -1); currentTx.setActive(threadContext.getTimeout()); inFlightTransactions.put(currentTx, clearContextSynchronization); if (log.isDebugEnabled()) { log.debug("begun new transaction at " + new Date(currentTx.getResourceManager().getGtrid().extractTimestamp())); }
public void setTransactionTimeout(int seconds) throws SystemException { if (seconds < 0) throw new BitronixSystemException("cannot set a timeout to less than 0 second (was: " + seconds + "s)"); getOrCreateCurrentContext().setTimeout(seconds); }
/** * Create a new transaction on the current thread's context. * @return the created transaction. */ private BitronixTransaction createTransaction() { BitronixTransaction transaction = new BitronixTransaction(); getOrCreateCurrentContext().setTransaction(transaction); inFlightTransactions.put(transaction.getResourceManager().getGtrid(), transaction); MDC.put(MDC_GTRID_KEY, transaction.getGtrid()); return transaction; }
/** * Start a new transaction and bind the context to the calling thread. * @throws NotSupportedException if a transaction is already bound to the calling thread. * @throws SystemException if the transaction manager is shutting down. */ public void begin() throws NotSupportedException, SystemException { if (log.isDebugEnabled()) log.debug("beginning a new transaction"); if (isShuttingDown()) throw new BitronixSystemException("cannot start a new transaction, transaction manager is shutting down"); dumpTransactionContexts(); BitronixTransaction currentTx = getCurrentTransaction(); if (currentTx != null) throw new NotSupportedException("nested transactions not supported"); currentTx = createTransaction(); ClearContextSynchronization clearContextSynchronization = new ClearContextSynchronization(currentTx); try { currentTx.getSynchronizationScheduler().add(clearContextSynchronization, Scheduler.ALWAYS_LAST_POSITION -1); currentTx.setActive(getOrCreateCurrentContext().getTimeout()); if (log.isDebugEnabled()) log.debug("begun new transaction at " + new Date(currentTx.getResourceManager().getGtrid().extractTimestamp())); } catch (RuntimeException ex) { clearContextSynchronization.afterCompletion(Status.STATUS_NO_TRANSACTION); throw ex; } catch (SystemException ex) { clearContextSynchronization.afterCompletion(Status.STATUS_NO_TRANSACTION); throw ex; } }
private Map<Object, Object> getResources() { ThreadContext currentContext = transactionManager.currentThreadContext(); if (currentContext == null) { return null; } return currentContext.getResources(); }
@Override public void afterCompletion(int status) { ThreadContext context = threadContext.get(); if (context != null) { if (log.isDebugEnabled()) { log.debug("clearing transaction from thread context: " + context); } context.clearTransaction(); } else { if (log.isDebugEnabled()) { log.debug("thread context was null when clear context synchronization executed"); } } if (log.isDebugEnabled()) { log.debug("removing transaction from in-flight transactions: " + currentTx); } inFlightTransactions.remove(currentTx); MDC.remove(MDC_GTRID_KEY); }
/** * Create a new transaction on the current thread's context. * @return the created transaction. */ private BitronixTransaction createTransaction() { BitronixTransaction transaction = new BitronixTransaction(); ThreadContext.getThreadContext().setTransaction(transaction); MDC.put(MDC_GTRID_KEY, transaction.getGtrid()); return transaction; }
public void resume(Transaction transaction) throws InvalidTransactionException, IllegalStateException, SystemException { if (log.isDebugEnabled()) log.debug("resuming " + transaction); if (transaction == null) throw new InvalidTransactionException("resumed transaction cannot be null"); if (!(transaction instanceof BitronixTransaction)) throw new InvalidTransactionException("resumed transaction must be an instance of BitronixTransaction"); BitronixTransaction tx = (BitronixTransaction) transaction; BitronixTransaction currentTx = getCurrentTransaction(); if (currentTx != null) throw new IllegalStateException("a transaction is already running on this thread"); try { XAResourceManager resourceManager = tx.getResourceManager(); resourceManager.resume(); ThreadContext ctx = new ThreadContext(); ctx.setTransaction(tx); setCurrentContext(ctx); } catch (XAException ex) { String extraErrorDetails = TransactionManagerServices.getExceptionAnalyzer().extractExtraXAExceptionDetails(ex); throw new BitronixSystemException("cannot resume " + tx + ", error=" + Decoder.decodeXAExceptionErrorCode(ex) + (extraErrorDetails == null ? "" : ", extra error=" + extraErrorDetails), ex); } }
/** * Get the transaction currently registered on the current thread context. * @return the current transaction or null if no transaction has been started on the current thread. */ public BitronixTransaction getCurrentTransaction() { return ThreadContext.getThreadContext().getTransaction(); }
currentTx = createTransaction(); ThreadContext threadContext = ThreadContext.getThreadContext(); ClearContextSynchronization clearContextSynchronization = new ClearContextSynchronization(currentTx, threadContext); try { currentTx.getSynchronizationScheduler().add(clearContextSynchronization, Scheduler.ALWAYS_LAST_POSITION -1); currentTx.setActive(threadContext.getTimeout()); inFlightTransactions.put(currentTx, clearContextSynchronization); if (log.isDebugEnabled()) { log.debug("begun new transaction at " + new Date(currentTx.getResourceManager().getGtrid().extractTimestamp())); }
/** * Unlink the transaction from the current thread's context. */ private void clearCurrentContextForSuspension() { if (log.isDebugEnabled()) { log.debug("clearing current thread context: " + ThreadContext.getThreadContext()); } ThreadContext.getThreadContext().clearTransaction(); }
public void setTransactionTimeout(int seconds) throws SystemException { if (seconds < 0) throw new BitronixSystemException("cannot set a timeout to less than 0 second (was: " + seconds + "s)"); getOrCreateCurrentContext().setTimeout(seconds); }
/** * Create a new transaction on the current thread's context. * @return the created transaction. */ private BitronixTransaction createTransaction() { BitronixTransaction transaction = new BitronixTransaction(); getOrCreateCurrentContext().setTransaction(transaction); inFlightTransactions.put(transaction.getResourceManager().getGtrid(), transaction); MDC.put(MDC_GTRID_KEY, transaction.getGtrid()); return transaction; }