Class TransactionManager

java.lang.Object
org.jpos.q2.QBeanSupport
org.jpos.transaction.TransactionManager
All Implemented Interfaces:
Runnable, Configurable, QBean, QBeanSupportMBean, QPersist, TransactionConstants, TransactionManagerMBean, Loggeable, MetricsProvider

Multi-participant transaction manager.

Reads contexts from a configured input space, walks each transaction through its prepare/commit/abort lifecycle, and persists state to a persistent space so in-flight transactions can be recovered after a restart.

  • Field Details

  • Constructor Details

    • TransactionManager

      Creates an unconfigured manager; configuration is supplied by Q2 at deploy time.
  • Method Details

    • defaultRealm

      protected String defaultRealm()
      Description copied from class: QBeanSupport
      Returns the default realm for this bean; subclasses may override.
      Overrides:
      defaultRealm in class QBeanSupport
      Returns:
      default realm string, or null
    • initService

      public void initService() throws ConfigurationException
      Description copied from class: QBeanSupport
      Called during the init lifecycle phase; subclasses may override.
      Overrides:
      initService in class QBeanSupport
      Throws:
      ConfigurationException
    • startService

      public void startService() throws Exception
      Description copied from class: QBeanSupport
      Called during the start lifecycle phase; subclasses may override.
      Overrides:
      startService in class QBeanSupport
      Throws:
      Exception - on error
    • stopService

      public void stopService()
      Description copied from class: QBeanSupport
      Called during the stop lifecycle phase; subclasses may override.
      Overrides:
      stopService in class QBeanSupport
    • queue

      public void queue(Serializable context)
      Enqueues a context at the tail of the input queue (FIFO ordering).
      Parameters:
      context - serialisable transaction context
    • push

      public void push(Serializable context)
      Pushes a context onto the head of the input queue (LIFO ordering).
      Parameters:
      context - serialisable transaction context
    • getQueueName

      public String getQueueName()
      Returns the configured input queue name.
      Returns:
      the name of the input queue this manager consumes from
    • getSpace

      public Space getSpace()
      Returns the volatile space used for in-flight transaction state.
      Returns:
      the working Space
    • getInputSpace

      public Space getInputSpace()
      Returns the externally visible input space used for queueing contexts.
      Returns:
      the input Space
    • getPersistentSpace

      Returns the persistent space used to recover in-flight transactions across restarts.
      Returns:
      the persistent Space
    • run

      public void run()
      Specified by:
      run in interface Runnable
    • getTail

      public long getTail()
      Description copied from interface: TransactionManagerMBean
      Returns the current tail index of the transaction queue.
      Specified by:
      getTail in interface TransactionManagerMBean
      Returns:
      queue tail index
    • getHead

      public long getHead()
      Description copied from interface: TransactionManagerMBean
      Returns the current head index of the transaction queue.
      Specified by:
      getHead in interface TransactionManagerMBean
      Returns:
      queue head index
    • getInTransit

      public long getInTransit()
      Returns the count of transactions that have been picked up but not yet completed.
      Returns:
      number of in-flight transactions
    • setConfiguration

      Description copied from interface: Configurable
      Configures this object with the supplied Configuration.
      Specified by:
      setConfiguration in interface Configurable
      Overrides:
      setConfiguration in class QBeanSupport
      Parameters:
      cfg - Configuration object
      Throws:
      ConfigurationException - if the configuration is invalid
    • addListener

      Registers a listener that observes transaction lifecycle transitions.
      Parameters:
      l - listener to add
    • removeListener

      Removes a previously registered status listener.
      Parameters:
      l - listener to remove
    • getTPS

      public TPS getTPS()
      Returns the TPS counter that tracks transactions per second for this manager.
      Returns:
      the TPS counter
    • getTPSAsString

      Description copied from interface: TransactionManagerMBean
      Returns a human-readable summary of TPS statistics.
      Specified by:
      getTPSAsString in interface TransactionManagerMBean
      Returns:
      TPS snapshot suitable for diagnostics
    • getTPSAvg

      public float getTPSAvg()
      Description copied from interface: TransactionManagerMBean
      Returns the average transactions-per-second since the last reset.
      Specified by:
      getTPSAvg in interface TransactionManagerMBean
      Returns:
      mean TPS
    • getTPSPeak

      public int getTPSPeak()
      Description copied from interface: TransactionManagerMBean
      Returns the peak transactions-per-second observed since the last reset.
      Specified by:
      getTPSPeak in interface TransactionManagerMBean
      Returns:
      peak TPS
    • getTPSPeakWhen

      public Date getTPSPeakWhen()
      Description copied from interface: TransactionManagerMBean
      Returns the wall-clock instant at which TransactionManagerMBean.getTPSPeak() was reached.
      Specified by:
      getTPSPeakWhen in interface TransactionManagerMBean
      Returns:
      timestamp of the TPS peak, or null if not yet observed
    • getTPSElapsed

      public long getTPSElapsed()
      Description copied from interface: TransactionManagerMBean
      Returns the elapsed time in milliseconds covered by the current TPS window.
      Specified by:
      getTPSElapsed in interface TransactionManagerMBean
      Returns:
      elapsed window in milliseconds
    • resetTPS

      public void resetTPS()
      Description copied from interface: TransactionManagerMBean
      Resets the TPS counters and start time.
      Specified by:
      resetTPS in interface TransactionManagerMBean
    • getMetrics

      public Metrics getMetrics()
      Description copied from interface: MetricsProvider
      Returns the metrics aggregator owned by this component.
      Specified by:
      getMetrics in interface MetricsProvider
      Returns:
      the live Metrics instance
    • dump

      public void dump(PrintStream ps, String indent)
      Description copied from interface: Loggeable
      Dumps a human-readable representation of this object to the print stream.
      Specified by:
      dump in interface Loggeable
      Parameters:
      ps - the output stream
      indent - indentation prefix
    • commit

      protected void commit(int session, long id, Serializable context, List<TransactionParticipant> members, boolean recover, LogEvent evt, Profiler prof)
      Walks members in order, invoking each participant's commit phase and recording trace and timer information.
      Parameters:
      session - session index that owns this transaction
      id - transaction identifier
      context - serialised transaction context
      members - participants whose commit hooks will run
      recover - when true, the manager is replaying a transaction after restart and should call ContextRecovery
      evt - optional log event to receive trace messages, or null
      prof - optional profiler to receive checkpoints, or null
    • abort

      protected void abort(int session, long id, Serializable context, List<TransactionParticipant> members, boolean recover, LogEvent evt, Profiler prof)
      Walks members in order, invoking each participant's abort phase and recording trace and timer information.
      Parameters:
      session - session index that owns this transaction
      id - transaction identifier
      context - serialised transaction context
      members - participants whose abort hooks will run
      recover - when true, the manager is replaying a transaction after restart and should call ContextRecovery
      evt - optional log event to receive trace messages, or null
      prof - optional profiler to receive checkpoints, or null
    • prepareForAbort

      protected int prepareForAbort(TransactionParticipant p, long id, Serializable context)
      Invokes AbortParticipant.prepareForAbort(long, Serializable) on p when applicable, swallowing exceptions and recording timer metrics.
      Parameters:
      p - participant
      id - transaction identifier
      context - serialised transaction context
      Returns:
      result code from the participant, or ABORTED | NO_JOIN when p is not an AbortParticipant or threw
    • prepare

      protected int prepare(TransactionParticipant p, long id, Serializable context)
      Invokes the prepare phase on p, swallowing exceptions (logged as warnings) and recording timer metrics.
      Parameters:
      p - participant
      id - transaction identifier
      context - serialised transaction context
      Returns:
      result code from the participant, or ABORTED when p threw
    • commit

      protected void commit(TransactionParticipant p, long id, Serializable context)
      Invokes the commit phase on p, swallowing exceptions (logged as warnings) and recording timer metrics.
      Parameters:
      p - participant
      id - transaction identifier
      context - serialised transaction context
    • abort

      protected void abort(TransactionParticipant p, long id, Serializable context)
      Invokes the abort phase on p, swallowing exceptions (logged as warnings) and recording timer metrics.
      Parameters:
      p - participant
      id - transaction identifier
      context - serialised transaction context
    • prepare

      protected int prepare(int session, long id, Serializable context, List<TransactionParticipant> members, Iterator<TransactionParticipant> iter, boolean abort, LogEvent evt, Profiler prof, Chronometer chronometer)
      Drives the prepare phase across iter, accumulating results into members and aborting on retry signals or participant exceptions.
      Parameters:
      session - session index that owns this transaction
      id - transaction identifier
      context - serialised transaction context
      members - accumulator of participants whose prepare ran
      iter - participant iterator (may include selectors / groups)
      abort - when true, drive the prepare-for-abort path
      evt - optional log event to receive trace messages, or null
      prof - optional profiler to receive checkpoints, or null
      chronometer - chronometer used to enforce max-time
      Returns:
      the bitwise OR of every participant result, suitable for the lifecycle dispatcher
    • getParticipants

      Returns the configured participants for a named group.
      Parameters:
      groupName - group name (use DEFAULT_GROUP for the default chain)
      Returns:
      participants in declaration order; an empty list when groupName is unknown
    • getParticipants

      Returns the participant chain for a specific transaction, combining the default group with any groups pushed onto the persistent execution stack.
      Parameters:
      id - transaction identifier
      Returns:
      participants to invoke in order
    • initStatusListeners

      protected void initStatusListeners(org.jdom2.Element config) throws ConfigurationException
      Instantiates and registers any <status-listener> children of the QBean descriptor.
      Parameters:
      config - descriptor element
      Throws:
      ConfigurationException - if a listener cannot be instantiated or configured
    • initParticipants

      protected void initParticipants(org.jdom2.Element config) throws ConfigurationException
      Builds the default participant group plus any named <group> children from the QBean descriptor.
      Parameters:
      config - descriptor element
      Throws:
      ConfigurationException - if a group is missing a name, duplicates an existing group, or a participant fails to instantiate
    • initGroup

      protected List<TransactionParticipant> initGroup(org.jdom2.Element e) throws ConfigurationException
      Instantiates the participants declared inside a <group> element, skipping any that are explicitly disabled.
      Parameters:
      e - group element
      Returns:
      participants in declaration order
      Throws:
      ConfigurationException - if a participant fails to instantiate
    • createParticipant

      Instantiates a participant from a <participant> element, configures it, registers per-participant timers, and tracks any Destroyable for cleanup at shutdown.
      Parameters:
      e - participant element
      Returns:
      the configured participant
      Throws:
      ConfigurationException - if instantiation, configuration, or timer registration fails
    • getOutstandingTransactions

      Description copied from interface: TransactionManagerMBean
      Returns the number of in-flight transactions (tail - head).
      Specified by:
      getOutstandingTransactions in interface TransactionManagerMBean
      Returns:
      outstanding transaction count
    • getKey

      protected String getKey(String prefix, long id)
      Builds a persistent-space key by combining the manager name, a prefix, and the transaction id.
      Parameters:
      prefix - key namespace (e.g. CONTEXT, STATE)
      id - transaction identifier
      Returns:
      the assembled space key
    • commitOff

      protected void commitOff(Space sp)
      Disables auto-commit on a JDBMSpace; no-op for other space types.
      Parameters:
      sp - the space whose auto-commit should be turned off
    • commitOn

      protected void commitOn(Space sp)
      Re-enables auto-commit on a JDBMSpace, flushing any pending writes; no-op for other space types.
      Parameters:
      sp - the space whose auto-commit should be turned back on
    • checkTail

      protected void checkTail()
      Advances the persistent tail pointer past any contiguous already-DONE transactions, purging their state from the space.
    • tailDone

      protected boolean tailDone()
      Returns whether the transaction at tail is already DONE; when true, removes its persistent state.
      Returns:
      true when the tail transaction has reached terminal state
    • snapshot

      protected void snapshot(long id, Serializable context)
      Snapshots the context for transaction id without changing its state.
      Parameters:
      id - transaction identifier
      context - serialised transaction context, or null to clear it
    • snapshot

      protected void snapshot(long id, Serializable context, Integer status)
      Snapshots the context and optionally updates the persisted state for transaction id. Skipped when recovery is disabled and the status is not DONE.
      Parameters:
      id - transaction identifier
      context - serialised transaction context, or null to clear it
      status - new persisted state, or null to leave it unchanged
    • setState

      protected void setState(long id, Integer state)
      Atomically replaces the persisted lifecycle state for id.
      Parameters:
      id - transaction identifier
      state - new state, or null to clear it
    • addGroup

      protected void addGroup(long id, String groupName)
      Pushes a group name onto the persistent execution stack for id.
      Parameters:
      id - transaction identifier
      groupName - group to schedule, or null for a no-op
    • purge

      protected void purge(long id, boolean full)
      Removes persistent state associated with a transaction.
      Parameters:
      id - transaction identifier
      full - when true, also removes the lifecycle state entry (otherwise only context and group entries are cleared)
    • recover

      protected void recover()
      Replays every in-flight transaction recorded in the persistent space, advancing the tail pointer as each completes. No-op when recovery is disabled.
    • recover

      protected void recover(long id)
      Replays a single transaction by inspecting its persisted state and driving either the commit or abort path before purging its state.
      Parameters:
      id - transaction identifier
    • checkRetryTask

      protected void checkRetryTask()
      Lazily starts the TransactionManager.RetryTask that drains the retry queue back into the input space.
    • freeze

      protected LogEvent freeze(Serializable context, LogEvent evt, Profiler prof)
      This method gives the opportunity to decorate a LogEvent right before it gets logged. When overriding it, unless you know what you're doing, you should return a FrozenLogEvent in order to prevent concurrency issues.
      Parameters:
      context - current Context
      evt - current LogEvent
      prof - profiler (may be null)
      Returns:
      FrozenLogEvent
    • getSessionsToStandUp

      protected int getSessionsToStandUp()
      This method returns the number of sessions that can be started at this point in time
      Returns:
      number of sessions
    • isSessionToStandDown

      protected boolean isSessionToStandDown()
      Hook used by subclasses to drain a session early. The default implementation never asks a session to stand down.
      Returns:
      true if the current session should stop accepting new transactions
    • getActiveSessions

      public int getActiveSessions()
      Description copied from interface: TransactionManagerMBean
      Returns the number of session threads currently processing transactions.
      Specified by:
      getActiveSessions in interface TransactionManagerMBean
      Returns:
      live session count
    • getMaxSessions

      public int getMaxSessions()
      Description copied from interface: TransactionManagerMBean
      Returns the configured maximum number of concurrent session threads.
      Specified by:
      getMaxSessions in interface TransactionManagerMBean
      Returns:
      the maximum number of sessions this manager may scale up to
    • getSerializable

      public static Serializable getSerializable()
      Returns the current thread's transaction context as a raw Serializable.
      Returns:
      the thread-local context, or null when no transaction is in progress
    • getContext

      public static <T extends Serializable> T getContext()
      Returns the current thread's transaction context, narrowed to the caller's expected type.
      Type Parameters:
      T - caller-supplied context type
      Returns:
      the thread-local context, or null when no transaction is in progress
    • getId

      public static Long getId()
      Returns the current thread's transaction id, when one is in progress.
      Returns:
      the thread-local transaction id, or null