Package com.clickhouse.client
Class ClickHouseTransaction
java.lang.Object
com.clickhouse.client.ClickHouseTransaction
- All Implemented Interfaces:
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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
This class encapsulates transaction ID, which is defined asTuple(snapshotVersion UInt64, localTxCounter UInt64, hostId UUID)
. -
Field Summary
Modifier and TypeFieldDescriptionstatic final int
static final String
static final String
static final String
static final int
static final long
static final long
static final long
static final long
static final long
static final int
static final int
static final int
static final String
static final String
static final String
-
Constructor Summary
ModifierConstructorDescriptionprotected
ClickHouseTransaction
(ClickHouseNode server, int timeout, boolean implicit) Constructs a unique transaction inACTIVE
state.protected
ClickHouseTransaction
(ClickHouseNode server, String sessionId, int timeout, ClickHouseTransaction.XID id) Constructs a transaction inNEW
state, hencebegin()
orbegin(Map)
must be called before commit/rollback andisImplicit()
is alwaysfalse
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
abort()
Aborts the transaction.void
begin()
Starts a new transaction.void
begin
(Map<String, Serializable> settings) Starts a new transaction with optional server settings.void
commit()
Commits the transaction.void
commit
(Map<String, Serializable> settings) Commits the transaction with optional server settings.protected void
Ensures client and server are using the exact same transaction ID.boolean
getId()
Gets current transaction ID.Gets server of the transaction.Gets session id of the transaction.int
getState()
int
Gets transaction timeout in seconds.int
hashCode()
boolean
isActive()
Checks whether the transation's state isACTIVE
.boolean
Checks whether the transation's state isCOMMITTED
.boolean
isFailed()
Checks whether the transation's state isFAILED
.boolean
Checks if the transaction is implicit or not.boolean
isNew()
Checks whether the transation's state isNEW
.boolean
Checks whether the transation's state isROLLED_BACK
.protected final ClickHouseRecord
Issues transaction related query.protected ClickHouseRecord
issue
(String command, boolean sessionCheck, Map<String, Serializable> settings) Issues transaction related query.void
rollback()
Rolls back the transaction.void
rollback
(Map<String, Serializable> settings) Rolls back the transaction with optional server settings.void
snapshot
(long snapshotVersion) Sets transaction snapshot with optional server settings.void
snapshot
(long snapshotVersion, Map<String, Serializable> settings) Sets transaction snapshot with optional server settings, only works forACTIVE
transaction.toString()
-
Field Details
-
COMMAND_BEGIN
- See Also:
-
COMMAND_COMMIT
- See Also:
-
COMMAND_ROLLBACK
- See Also:
-
NEW
public static final int NEW- See Also:
-
ACTIVE
public static final int ACTIVE- See Also:
-
FAILED
public static final int FAILED- See Also:
-
COMMITTED
public static final int COMMITTED- See Also:
-
ROLLED_BACK
public static final int ROLLED_BACK- See Also:
-
CSN_UNKNOWN
public static final long CSN_UNKNOWN- See Also:
-
CSN_PREHISTORIC
public static final long CSN_PREHISTORIC- See Also:
-
CSN_COMMITTING
public static final long CSN_COMMITTING- See Also:
-
CSN_EVERYTHING_VISIBLE
public static final long CSN_EVERYTHING_VISIBLE- See Also:
-
CSN_MAX_RESERVED
public static final long CSN_MAX_RESERVED- See Also:
-
SETTING_IMPLICIT_TRANSACTION
- See Also:
-
SETTING_THROW_ON_UNSUPPORTED_QUERY_INSIDE_TRANSACTION
- See Also:
-
SETTING_WAIT_CHANGES_BECOME_VISIBLE_AFTER_COMMIT_MODE
- See Also:
-
-
Constructor Details
-
ClickHouseTransaction
protected ClickHouseTransaction(ClickHouseNode server, int timeout, boolean implicit) throws ClickHouseException Constructs a unique transaction inACTIVE
state.ClickHouseRequestManager.createSessionId()
will be used to ensure uniquness of the transaction.- Parameters:
server
- non-null server of the transactiontimeout
- transaction timeoutimplicit
- 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 inNEW
state, hencebegin()
orbegin(Map)
must be called before commit/rollback andisImplicit()
is alwaysfalse
.- Parameters:
server
- non-null server of the transactionsessionId
- non-empty session ID for the transactiontimeout
- transaction timeoutid
- optional transaction ID
-
-
Method Details
-
ensureTransactionId
Ensures client and server are using the exact same transaction ID.- Throws:
ClickHouseException
- when transaction ID is inconsistent between client and server
-
issue
Issues transaction related query. Same asissue(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 ClickHouseExceptionIssues transaction related query.- Parameters:
command
- non-empty transaction related querysessionCheck
- whether to enable session checksettings
- optional server settings- Returns:
- non-null record
- Throws:
ClickHouseException
- when failed to issue the query
-
getId
Gets current transaction ID.- Returns:
- non-null transaction ID
-
getServer
Gets server of the transaction.- Returns:
- non-null server
-
getSessionId
Gets session id of the transaction.- Returns:
- non-empty session id
-
getState
public int getState()- 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 isNEW
.- Returns:
- true if the state is
NEW
; false otherwise
-
isActive
public boolean isActive()Checks whether the transation's state isACTIVE
.- Returns:
- true if the state is
ACTIVE
; false otherwise
-
isCommitted
public boolean isCommitted()Checks whether the transation's state isCOMMITTED
.- Returns:
- true if the state is
COMMITTED
; false otherwise
-
isRolledBack
public boolean isRolledBack()Checks whether the transation's state isROLLED_BACK
.- Returns:
- true if the state is
ROLLED_BACK
; false otherwise
-
isFailed
public boolean isFailed()Checks whether the transation's state isFAILED
.- Returns:
- true if the state is
FAILED
; false otherwise
-
abort
public void abort()Aborts the transaction. -
begin
Starts a new transaction. Same asbegin(Collections.emptyMap())
.- Throws:
ClickHouseException
- when failed to begin new transaction
-
begin
Starts a new transaction with optional server settings. It's a no-op when calling against anACTIVE
transaction.- Parameters:
settings
- optional server settings- Throws:
ClickHouseException
- when failed to begin new transaction
-
commit
Commits the transaction. Same ascommit(Collections.emptyMap())
.- Throws:
ClickHouseException
- when failed to commit the transaction
-
commit
Commits the transaction with optional server settings. It's a no-op when calling against aCOMMITTED
transaction.- Parameters:
settings
- optional server settings- Throws:
ClickHouseException
- when failed to commit the transaction
-
rollback
Rolls back the transaction. Same asrollback(Collections.emptyMap())
.- Throws:
ClickHouseException
- when failed to roll back the transaction
-
rollback
Rolls back the transaction with optional server settings. It's a no-op when calling against aNEW
orROLLED_BACK
transaction.- Parameters:
settings
- optional server settings- Throws:
ClickHouseException
- when failed to roll back the transaction
-
snapshot
Sets transaction snapshot with optional server settings. Same assnapshot(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 ClickHouseExceptionSets transaction snapshot with optional server settings, only works forACTIVE
transaction. Usesnapshot(CSN_EVERYTHING_VISIBLE)
to read uncommitted data.- Parameters:
snapshotVersion
- snapshot versionsettings
- optional server settings- Throws:
ClickHouseException
- when failed to set transaction snapshot
-
hashCode
public int hashCode() -
equals
-
toString
-