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
Nested ClassesModifier and TypeClassDescriptionstatic classThis class encapsulates transaction ID, which is defined asTuple(snapshotVersion UInt64, localTxCounter UInt64, hostId UUID). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final Stringstatic final Stringstatic final Stringstatic final intstatic final longstatic final longstatic final longstatic final longstatic final longstatic final intstatic final intstatic final intstatic final Stringstatic final Stringstatic final String -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedClickHouseTransaction(ClickHouseNode server, int timeout, boolean implicit) Constructs a unique transaction inACTIVEstate.protectedClickHouseTransaction(ClickHouseNode server, String sessionId, int timeout, ClickHouseTransaction.XID id) Constructs a transaction inNEWstate, hencebegin()orbegin(Map)must be called before commit/rollback andisImplicit()is alwaysfalse. -
Method Summary
Modifier and TypeMethodDescriptionvoidabort()Aborts the transaction.voidbegin()Starts a new transaction.voidbegin(Map<String, Serializable> settings) Starts a new transaction with optional server settings.voidcommit()Commits the transaction.voidcommit(Map<String, Serializable> settings) Commits the transaction with optional server settings.protected voidEnsures client and server are using the exact same transaction ID.booleangetId()Gets current transaction ID.Gets server of the transaction.Gets session id of the transaction.intgetState()intGets transaction timeout in seconds.inthashCode()booleanisActive()Checks whether the transation's state isACTIVE.booleanChecks whether the transation's state isCOMMITTED.booleanisFailed()Checks whether the transation's state isFAILED.booleanChecks if the transaction is implicit or not.booleanisNew()Checks whether the transation's state isNEW.booleanChecks whether the transation's state isROLLED_BACK.protected final ClickHouseRecordIssues transaction related query.protected ClickHouseRecordissue(String command, boolean sessionCheck, Map<String, Serializable> settings) Issues transaction related query.voidrollback()Rolls back the transaction.voidrollback(Map<String, Serializable> settings) Rolls back the transaction with optional server settings.voidsnapshot(long snapshotVersion) Sets transaction snapshot with optional server settings.voidsnapshot(long snapshotVersion, Map<String, Serializable> settings) Sets transaction snapshot with optional server settings, only works forACTIVEtransaction.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 inACTIVEstate.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 inNEWstate, 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_timeoutas 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 anACTIVEtransaction.- 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 aCOMMITTEDtransaction.- 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 aNEWorROLLED_BACKtransaction.- 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 forACTIVEtransaction. 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
-