Class ClickHouseTransaction

java.lang.Object
com.clickhouse.client.ClickHouseTransaction
All Implemented Interfaces:
Serializable

public final class ClickHouseTransaction extends Object implements Serializable
This class represents a transaction in ClickHouse. Besides transaction ID Tuple(snapshotVersion UInt64, localTxCounter UInt64, hostId UUID), it also contains session ID and references to the connected server and client for issuing queries.
See Also:
  • Field Details

  • Constructor Details

    • ClickHouseTransaction

      protected ClickHouseTransaction(ClickHouseNode server, int timeout, boolean implicit) throws ClickHouseException
      Constructs a unique transaction in ACTIVE state. ClickHouseRequestManager.createSessionId() will be used to ensure uniquness of the transaction.
      Parameters:
      server - non-null server of the transaction
      timeout - transaction timeout
      implicit - whether it's an implicit transaction or not
      Throws:
      ClickHouseException - when failed to start transaction
    • ClickHouseTransaction

      protected ClickHouseTransaction(ClickHouseNode server, String sessionId, int timeout, ClickHouseTransaction.XID id)
      Constructs a transaction in NEW state, hence begin() or begin(Map) must be called before commit/rollback and isImplicit() is always false.
      Parameters:
      server - non-null server of the transaction
      sessionId - non-empty session ID for the transaction
      timeout - transaction timeout
      id - optional transaction ID
  • Method Details

    • ensureTransactionId

      protected void ensureTransactionId() throws ClickHouseException
      Ensures client and server are using the exact same transaction ID.
      Throws:
      ClickHouseException - when transaction ID is inconsistent between client and server
    • issue

      protected final ClickHouseRecord issue(String command) throws ClickHouseException
      Issues transaction related query. Same as issue(command, true, Collections.emptyMap()).
      Parameters:
      command - non-empty transaction related query
      Returns:
      non-null record
      Throws:
      ClickHouseException - when failed to issue the query
    • issue

      protected ClickHouseRecord issue(String command, boolean sessionCheck, Map<String,Serializable> settings) throws ClickHouseException
      Issues transaction related query.
      Parameters:
      command - non-empty transaction related query
      sessionCheck - whether to enable session check
      settings - optional server settings
      Returns:
      non-null record
      Throws:
      ClickHouseException - when failed to issue the query
    • getId

      public ClickHouseTransaction.XID getId()
      Gets current transaction ID.
      Returns:
      non-null transaction ID
    • getServer

      public ClickHouseNode getServer()
      Gets server of the transaction.
      Returns:
      non-null server
    • getSessionId

      public String getSessionId()
      Gets session id of the transaction.
      Returns:
      non-empty session id
    • getState

      public int getState()
      Gets transaction state, one of NEW, ACTIVE, COMMITTED, ROLLED_BACK, or FAILED.
      Returns:
      transaction state
    • getTimeout

      public int getTimeout()
      Gets transaction timeout in seconds.
      Returns:
      transaction timeout in seconds, zero or negative number means default_session_timeout as defined on server
    • isImplicit

      public boolean isImplicit()
      Checks if the transaction is implicit or not.
      Returns:
      true if it's an implicit transaction; false otherwise
    • isNew

      public boolean isNew()
      Checks whether the transation's state is NEW.
      Returns:
      true if the state is NEW; false otherwise
    • isActive

      public boolean isActive()
      Checks whether the transation's state is ACTIVE.
      Returns:
      true if the state is ACTIVE; false otherwise
    • isCommitted

      public boolean isCommitted()
      Checks whether the transation's state is COMMITTED.
      Returns:
      true if the state is COMMITTED; false otherwise
    • isRolledBack

      public boolean isRolledBack()
      Checks whether the transation's state is ROLLED_BACK.
      Returns:
      true if the state is ROLLED_BACK; false otherwise
    • isFailed

      public boolean isFailed()
      Checks whether the transation's state is FAILED.
      Returns:
      true if the state is FAILED; false otherwise
    • abort

      public void abort()
      Aborts the transaction.
    • begin

      public void begin() throws ClickHouseException
      Starts a new transaction. Same as begin(Collections.emptyMap()).
      Throws:
      ClickHouseException - when failed to begin new transaction
    • begin

      public void begin(Map<String,Serializable> settings) throws ClickHouseException
      Starts a new transaction with optional server settings. It's a no-op when calling against an ACTIVE transaction.
      Parameters:
      settings - optional server settings
      Throws:
      ClickHouseException - when failed to begin new transaction
    • commit

      public void commit() throws ClickHouseException
      Commits the transaction. Same as commit(Collections.emptyMap()).
      Throws:
      ClickHouseException - when failed to commit the transaction
    • commit

      public void commit(Map<String,Serializable> settings) throws ClickHouseException
      Commits the transaction with optional server settings. It's a no-op when calling against a COMMITTED transaction.
      Parameters:
      settings - optional server settings
      Throws:
      ClickHouseException - when failed to commit the transaction
    • rollback

      public void rollback() throws ClickHouseException
      Rolls back the transaction. Same as rollback(Collections.emptyMap()).
      Throws:
      ClickHouseException - when failed to roll back the transaction
    • rollback

      public void rollback(Map<String,Serializable> settings) throws ClickHouseException
      Rolls back the transaction with optional server settings. It's a no-op when calling against a NEW or ROLLED_BACK transaction.
      Parameters:
      settings - optional server settings
      Throws:
      ClickHouseException - when failed to roll back the transaction
    • snapshot

      public void snapshot(long snapshotVersion) throws ClickHouseException
      Sets transaction snapshot with optional server settings. Same as snapshot(snapshotVersion, Collections.emptyMap()).
      Parameters:
      snapshotVersion - snapshot version
      Throws:
      ClickHouseException - when failed to set transaction snapshot
    • snapshot

      public void snapshot(long snapshotVersion, Map<String,Serializable> settings) throws ClickHouseException
      Sets transaction snapshot with optional server settings, only works for ACTIVE transaction. Use snapshot(CSN_EVERYTHING_VISIBLE) to read uncommitted data.
      Parameters:
      snapshotVersion - snapshot version
      settings - optional server settings
      Throws:
      ClickHouseException - when failed to set transaction snapshot
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object