Class 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:
    Serialized Form
    • Constructor Detail

      • 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 Detail

      • 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
      • 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
      • 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.
      • commit

        public void commit()
                    throws ClickHouseException
        Commits the transaction. Same as commit(Collections.emptyMap()).
        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
      • 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