public interface Connection extends Closeable
Connection
object can consist of any number of transport
connections to the underlying database or represent a session over a multiplexed transport connection. For maximum portability, connections should be used synchronously.
Connection
objects initiate database conversations for transaction management and statement execution. Objects created by a connection are only valid as long as the connection remains
open.
When configuring a connection, R2DBC applications should use the appropriate methods such as beginTransaction()
, setAutoCommit(boolean)
and
setTransactionIsolationLevel(IsolationLevel)
to change transaction properties instead. Applications should not execute SQL commands directly to change the connection configuration when
a R2DBC method is available.
New connections are by default created in auto-commit mode
.
Modifier and Type | Method and Description |
---|---|
Publisher<Void> |
beginTransaction()
Begins a new transaction.
|
Publisher<Void> |
beginTransaction(TransactionDefinition definition)
Begins a new transaction.
|
Publisher<Void> |
close()
Release any resources held by the
Connection . |
Publisher<Void> |
commitTransaction()
Commits the current transaction.
|
Batch |
createBatch()
Creates a new
Batch instance for building a batched request. |
Publisher<Void> |
createSavepoint(String name)
Creates a savepoint in the current transaction.
|
Statement |
createStatement(String sql)
Creates a new statement for building a statement-based request.
|
ConnectionMetadata |
getMetadata()
Returns the
ConnectionMetadata about the product this Connection is connected to. |
IsolationLevel |
getTransactionIsolationLevel()
Returns the
IsolationLevel for this connection. |
boolean |
isAutoCommit()
Returns the auto-commit mode for this connection.
|
Publisher<Void> |
releaseSavepoint(String name)
Releases a savepoint in the current transaction.
|
Publisher<Void> |
rollbackTransaction()
Rolls back the current transaction.
|
Publisher<Void> |
rollbackTransactionToSavepoint(String name)
Rolls back to a savepoint in the current transaction.
|
Publisher<Void> |
setAutoCommit(boolean autoCommit)
Configures the auto-commit mode for the current transaction.
|
Publisher<Void> |
setLockWaitTimeout(Duration timeout)
Configures the lock acquisition timeout for statements to be executed using the current connection.
|
Publisher<Void> |
setStatementTimeout(Duration timeout)
Configures the statement timeout for statements to be executed using the current connection.
|
Publisher<Void> |
setTransactionIsolationLevel(IsolationLevel isolationLevel)
Configures the isolation level for the current transaction.
|
Publisher<Boolean> |
validate(ValidationDepth depth)
Validates the connection according to the given
ValidationDepth . |
Publisher<Void> beginTransaction()
auto-commit
mode.Publisher
that indicates that the transaction is openPublisher<Void> beginTransaction(TransactionDefinition definition)
auto-commit
mode. Beginning the transaction may fail if the TransactionDefinition
conflicts with the
connection configuration.definition
- attributes for the transaction.Publisher
that indicates that the transaction is openPublisher<Void> close()
Connection
.Publisher<Void> commitTransaction()
Publisher
that indicates that a transaction has been committedBatch createBatch()
Batch
instance for building a batched request.Batch
instancePublisher<Void> createSavepoint(String name)
name
- the name of the savepoint to createPublisher
that indicates that a savepoint has been createdIllegalArgumentException
- if name
is null
UnsupportedOperationException
- if savepoints are not supportedStatement createStatement(String sql)
sql
- the SQL of the statementStatement
instanceIllegalArgumentException
- if sql
is null
boolean isAutoCommit()
ConnectionMetadata getMetadata()
ConnectionMetadata
about the product this Connection
is connected to.ConnectionMetadata
about the product this Connection
is connected toIsolationLevel getTransactionIsolationLevel()
IsolationLevel
for this connection.
Isolation level is typically one of the following constants:
IsolationLevel.READ_UNCOMMITTED
IsolationLevel.READ_COMMITTED
IsolationLevel.REPEATABLE_READ
IsolationLevel.SERIALIZABLE
IsolationLevel
is extensible so drivers can return a vendor-specific IsolationLevel
.
IsolationLevel
for this connection.Publisher<Void> releaseSavepoint(String name)
name
- the name of the savepoint to releasePublisher
that indicates that a savepoint has been releasedIllegalArgumentException
- if name
is null
Publisher<Void> rollbackTransaction()
Publisher
that indicates that a transaction has been rolled backPublisher<Void> rollbackTransactionToSavepoint(String name)
name
- the name of the savepoint to rollback toPublisher
that indicates that a savepoint has been rolled back toIllegalArgumentException
- if name
is null
UnsupportedOperationException
- if savepoints are not supportedPublisher<Void> setAutoCommit(boolean autoCommit)
Statement
s will be executed and committed as individual transactions.
Otherwise, in explicit transaction mode, transactions have to be started
explicitly.
A transaction needs to be either committed
or rolled back
to clean up the transaction state.
Calling this method during an active transaction and the auto-commit mode is changed, the transaction is committed. Calling this method without changing auto-commit mode this invocation results in a no-op.
autoCommit
- the isolation level for this transactionPublisher
that indicates that auto-commit mode has been configuredPublisher<Void> setLockWaitTimeout(Duration timeout)
ConnectionFactoryOptions
.
If the timeout is exceeded, a R2dbcTimeoutException
is raised.
In the case of Batch
/Statement
batching, it is vendor-specific as to whether the timeout is applied to individual SQL commands or the entire batch.
timeout
- the lock timeout for this connection. Support for no timeout
is vendor-specific.Publisher
that indicates that a lock timeout has been configuredIllegalArgumentException
- if timeout
is null
Publisher<Void> setStatementTimeout(Duration timeout)
ConnectionFactoryOptions
.
If the limit is exceeded, a R2dbcTimeoutException
is raised.
The minimum amount of time can be used to either let the data source cancel the statement or attempt a client-side cancellation.
The timeout applies to statements through a combination of Statement.execute()
and Result
consumption.
In the case of Batch
/Statement
batching, it is vendor-specific whether the timeout is applied to individual SQL commands or the entire batch.
Note that the timeout applies until receiving the first response from the data source and does not span until publisher completion
.
timeout
- the statement timeout for this connection. Duration.ZERO
indicates no timeout.Publisher
that indicates that a statement timeout has been configuredIllegalArgumentException
- if timeout
is null
Publisher<Void> setTransactionIsolationLevel(IsolationLevel isolationLevel)
Isolation level is typically one of the following constants:
IsolationLevel.READ_UNCOMMITTED
IsolationLevel.READ_COMMITTED
IsolationLevel.REPEATABLE_READ
IsolationLevel.SERIALIZABLE
IsolationLevel
is extensible so drivers can accept a vendor-specific IsolationLevel
.
isolationLevel
- the isolation level for this transactionPublisher
that indicates that a transaction level has been configuredIllegalArgumentException
- if isolationLevel
is null
Publisher<Boolean> validate(ValidationDepth depth)
ValidationDepth
.
Emits true if the validation was successful or false if the validation failed. Does not emit errors and does not complete empty.depth
- the validation depthPublisher
that indicates whether the validation was successfulIllegalArgumentException
- if depth
is null
Copyright © 2021. All rights reserved.