Interface CloudSpannerJdbcConnection (2.7.12)

public interface CloudSpannerJdbcConnection extends Connection

JDBC connection with a number of additional Cloud Spanner specific methods. JDBC connections that are returned by the Cloud Spanner JdbcDriver will implement this interface.

Calling Connection#unwrap(Class) with CloudSpannerJdbcConnection#getClass() as input on a Connection returned by the Cloud Spanner JDBC Driver will return a CloudSpannerJdbcConnection instance.

Implements

Connection

Methods

addTransactionRetryListener(TransactionRetryListener listener)

public abstract void addTransactionRetryListener(TransactionRetryListener listener)

See Also: com.google.cloud.spanner.connection.Connection#addTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener)

Parameter
NameDescription
listenercom.google.cloud.spanner.connection.TransactionRetryListener
Exceptions
TypeDescription
SQLException

if the Connection is closed.

addTransactionRetryListener(TransactionRetryListener listener)

public abstract void addTransactionRetryListener(TransactionRetryListener listener)
Parameter
NameDescription
listenerTransactionRetryListener
Exceptions
TypeDescription
SQLException

bufferedWrite(Mutation mutation)

public abstract void bufferedWrite(Mutation mutation)

Buffers the given mutation locally on the current transaction of this Connection. The mutation will be written to the database at the next call to Connection#commit(). The value will not be readable on this Connection before the transaction is committed.

Calling this method is only allowed when not in autocommit mode. See CloudSpannerJdbcConnection#write(Mutation) for writing mutations in autocommit mode.

Parameter
NameDescription
mutationcom.google.cloud.spanner.Mutation

the Mutation to buffer for writing to the database on the next commit.

Exceptions
TypeDescription
SQLException

if the Connection is in autocommit mode or the Connection is closed.

bufferedWrite(Iterable<Mutation> mutations)

public abstract void bufferedWrite(Iterable<Mutation> mutations)

Buffers the given mutations locally on the current transaction of this Connection. The mutations will be written to the database at the next call to Connection#commit(). The values will not be readable on this Connection before the transaction is committed.

Calling this method is only allowed when not in autocommit mode. See CloudSpannerJdbcConnection#write(Iterable) for writing mutations in autocommit mode.

Parameter
NameDescription
mutationsIterable<com.google.cloud.spanner.Mutation>

the Mutations to buffer for writing to the database on the next commit.

Exceptions
TypeDescription
SQLException

if the Connection is in autocommit mode or the Connection is closed.

getAutocommitDmlMode()

public abstract AutocommitDmlMode getAutocommitDmlMode()
Returns
TypeDescription
com.google.cloud.spanner.connection.AutocommitDmlMode

the current AutocommitDmlMode setting for this connection. This method may only be called on a connection that is in autocommit mode and not while in a temporary transaction.

Exceptions
TypeDescription
SQLException

getCommitResponse()

public abstract CommitResponse getCommitResponse()
Returns
TypeDescription
com.google.cloud.spanner.CommitResponse

the CommitResponse of the last read/write transaction. If the last transaction was not a read/write transaction, or a read/write transaction that did not return a CommitResponse because the transaction was not committed, the method will throw a SQLException. The CommitResponse will include CommitStats if #isReturnCommitStats() returns true.

Exceptions
TypeDescription
SQLException

getCommitTimestamp()

public abstract Timestamp getCommitTimestamp()
Returns
TypeDescription
Timestamp

the commit Timestamp of the last read/write transaction. If the last transaction was not a read/write transaction, or a read/write transaction that did not return a commit timestamp because the transaction was not committed, the method will throw a SQLException.

Exceptions
TypeDescription
SQLException

getConnectionUrl()

public abstract String getConnectionUrl()
Returns
TypeDescription
String

a connection URL that can be used to create a new Connection that is equal to the initial state of this connection. If this connection was initially opened in read-only mode, and later changed to read-write, this will not be reflected in the connection URL that is returned.

getDialect()

public default Dialect getDialect()
Returns
TypeDescription
com.google.cloud.spanner.Dialect

The Dialect that is used by this connection.

getOptimizerVersion()

public abstract String getOptimizerVersion()

Gets the current query optimizer version of this connection.

Returns
TypeDescription
String

The query optimizer version that is currently used by this connection.

Exceptions
TypeDescription
SQLException

getReadOnlyStaleness()

public abstract TimestampBound getReadOnlyStaleness()
Returns
TypeDescription
com.google.cloud.spanner.TimestampBound

the read-only staleness setting for the current read-only transaction. This method may only be called when the current transaction is a read-only transaction, or when the connection is in read-only and autocommit mode.

Exceptions
TypeDescription
SQLException

getReadTimestamp()

public abstract Timestamp getReadTimestamp()
Returns
TypeDescription
Timestamp

the read Timestamp of the last read-only transaction. If the last transaction was not a read-only transaction, or a read-only transaction that did not return a read timestamp because no data was read, the method will throw a SQLException.

Exceptions
TypeDescription
SQLException

getStatementTag()

public default String getStatementTag()
Returns
TypeDescription
String

The statement tag that will be used with the next statement that is executed on this connection.

Exceptions
TypeDescription
SQLException

getTransactionMode()

public abstract TransactionMode getTransactionMode()
Returns
TypeDescription
com.google.cloud.spanner.connection.TransactionMode

the transaction mode of the current transaction. This method may only be called when the connection is in a transaction.

Exceptions
TypeDescription
SQLException

getTransactionRetryListeners()

public abstract Iterator<TransactionRetryListener> getTransactionRetryListeners()
Returns
TypeDescription
Iterator<TransactionRetryListener>
Exceptions
TypeDescription
SQLException

getTransactionRetryListenersFromConnection()

public abstract Iterator<TransactionRetryListener> getTransactionRetryListenersFromConnection()

See Also: com.google.cloud.spanner.connection.Connection#getTransactionRetryListeners()

Returns
TypeDescription
Iterator<com.google.cloud.spanner.connection.TransactionRetryListener>
Exceptions
TypeDescription
SQLException

if the Connection is closed.

getTransactionTag()

public default String getTransactionTag()
Returns
TypeDescription
String

The transaction tag of the current transaction.

Exceptions
TypeDescription
SQLException

isInTransaction()

public abstract boolean isInTransaction()
Returns
TypeDescription
boolean

true if this connection has a transaction (that has not necessarily started). This method will only return false when the Connection is in autocommit mode and no explicit transaction has been started by executing BEGIN TRANSACTION. If the Connection is not in autocommit mode, there will always be a transaction.

Exceptions
TypeDescription
SQLException

isRetryAbortsInternally()

public abstract boolean isRetryAbortsInternally()
Returns
TypeDescription
boolean

true if this connection will automatically retry read/write transactions that abort. This method may only be called when the connection is in read/write transactional mode and no transaction has been started yet.

Exceptions
TypeDescription
SQLException

isReturnCommitStats()

public abstract boolean isReturnCommitStats()
Returns
TypeDescription
boolean

true if this connection requests commit statistics from Cloud Spanner.

Exceptions
TypeDescription
SQLException

isTransactionStarted()

public abstract boolean isTransactionStarted()
Returns
TypeDescription
boolean

true if this connection has a transaction that has started. A transaction is automatically started by the first statement that is executed in the transaction.

Exceptions
TypeDescription
SQLException

removeTransactionRetryListener(TransactionRetryListener listener)

public abstract boolean removeTransactionRetryListener(TransactionRetryListener listener)

See Also: com.google.cloud.spanner.connection.Connection#removeTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener)

Parameter
NameDescription
listenercom.google.cloud.spanner.connection.TransactionRetryListener
Returns
TypeDescription
boolean
Exceptions
TypeDescription
SQLException

if the Connection is closed.

removeTransactionRetryListener(TransactionRetryListener listener)

public abstract boolean removeTransactionRetryListener(TransactionRetryListener listener)
Parameter
NameDescription
listenerTransactionRetryListener
Returns
TypeDescription
boolean
Exceptions
TypeDescription
SQLException

setAutocommitDmlMode(AutocommitDmlMode mode)

public abstract void setAutocommitDmlMode(AutocommitDmlMode mode)

Sets the mode for executing DML statements in autocommit mode for this connection. This setting is only used when the connection is in autocommit mode, and may only be set while the transaction is in autocommit mode and not in a temporary transaction. The autocommit transaction mode is reset to its default value of AutocommitDmlMode#TRANSACTIONAL when autocommit mode is changed on the connection.

Parameter
NameDescription
modecom.google.cloud.spanner.connection.AutocommitDmlMode

The DML autocommit mode to use

  • AutocommitDmlMode#TRANSACTIONAL DML statements are executed as single read-write transaction. After successful execution, the DML statement is guaranteed to have been applied exactly once to the database
  • AutocommitDmlMode#PARTITIONED_NON_ATOMIC DML statements are executed as partitioned DML transactions. If an error occurs during the execution of the DML statement, it is possible that the statement has been applied to some but not all of the rows specified in the statement.

Exceptions
TypeDescription
SQLException

setOptimizerVersion(String optimizerVersion)

public abstract void setOptimizerVersion(String optimizerVersion)

Sets the query optimizer version to use for this connection.

Parameter
NameDescription
optimizerVersionString

The query optimizer version to use. Must be a valid optimizer version number, the string LATEST or an empty string. The empty string will instruct the connection to use the optimizer version that is defined in the environment variable SPANNER_OPTIMIZER_VERSION. If no value is specified in the environment variable, the default query optimizer of Cloud Spanner is used.

Exceptions
TypeDescription
SQLException

setReadOnlyStaleness(TimestampBound staleness)

public abstract void setReadOnlyStaleness(TimestampBound staleness)

Sets the staleness to use for the current read-only transaction. This method may only be called when the transaction mode of the current transaction is TransactionMode#READ_ONLY_TRANSACTION and there is no transaction that has started, or when the connection is in read-only and autocommit mode.

Parameter
NameDescription
stalenesscom.google.cloud.spanner.TimestampBound

The staleness to use for the current but not yet started read-only transaction

Exceptions
TypeDescription
SQLException

setRetryAbortsInternally(boolean retryAbortsInternally)

public abstract void setRetryAbortsInternally(boolean retryAbortsInternally)

Sets whether this connection will internally retry read/write transactions that abort. The default is true. When internal retry is enabled, the Connection will keep track of a running SHA256 checksum of all ResultSets that have been returned from Cloud Spanner. If the checksum that is calculated during an internal retry differs from the original checksum, the transaction will abort with an AbortedDueToConcurrentModificationException.

Note that retries of a read/write transaction that calls a non-deterministic function on Cloud Spanner, such as CURRENT_TIMESTAMP(), will never be successful, as the data returned during the retry will always be different from the original transaction.

It is also highly recommended that all queries in a read/write transaction have an ORDER BY clause that guarantees that the data is returned in the same order as in the original transaction if the transaction is internally retried. The most efficient way to achieve this is to always include the primary key columns at the end of the ORDER BY clause.

This method may only be called when the connection is in read/write transactional mode and no transaction has been started yet.

Parameter
NameDescription
retryAbortsInternallyboolean

Set to true to internally retry transactions that are aborted by Spanner. When set to false, any database call on a transaction that has been aborted by Cloud Spanner will throw an AbortedException instead of being retried. Set this to false if your application already uses retry loops to handle AbortedExceptions.

Exceptions
TypeDescription
SQLException

setReturnCommitStats(boolean returnCommitStats)

public abstract void setReturnCommitStats(boolean returnCommitStats)

Sets whether this connection should request commit statistics from Cloud Spanner for read/write transactions and for DML statements in autocommit mode.

Parameter
NameDescription
returnCommitStatsboolean
Exceptions
TypeDescription
SQLException

setStatementTag(String tag)

public default void setStatementTag(String tag)

Sets the statement tag to use for the next statement that will be executed. The tag is automatically cleared after the statement is executed. Statement tags can be used both with autocommit=true and autocommit=false, and can be used for partitioned DML.

Parameter
NameDescription
tagString

The statement tag to use with the next statement that will be executed on this connection.

Exceptions
TypeDescription
SQLException

setTransactionMode(TransactionMode transactionMode)

public abstract void setTransactionMode(TransactionMode transactionMode)

Sets the transaction mode to use for current transaction. This method may only be called when in a transaction, and before the transaction is actually started, i.e. before any statements have been executed in the transaction.

Parameter
NameDescription
transactionModecom.google.cloud.spanner.connection.TransactionMode

The transaction mode to use for the current transaction.

  • TransactionMode#READ_ONLY_TRANSACTION will create a read-only transaction and prevent any changes to written to the database through this transaction. The read timestamp to be used will be determined based on the current readOnlyStaleness setting of this connection. It is recommended to use TransactionMode#READ_ONLY_TRANSACTION instead of TransactionMode#READ_WRITE_TRANSACTION when possible, as read-only transactions do not acquire locks on Cloud Spanner, and read-only transactions never abort.
  • TransactionMode#READ_WRITE_TRANSACTION this value is only allowed when the connection is not in read-only mode and will create a read-write transaction. If CloudSpannerJdbcConnection#isRetryAbortsInternally() is true, each read/write transaction will keep track of a running SHA256 checksum for each ResultSet that is returned in order to be able to retry the transaction in case the transaction is aborted by Spanner.

Exceptions
TypeDescription
SQLException

setTransactionTag(String tag)

public default void setTransactionTag(String tag)

Sets the transaction tag to use for the current transaction. This method may only be called when in a transaction, and before the transaction is actually started, i.e. before any statements have been executed in the transaction.

The tag will be set as the transaction tag of all statements during the transaction, and as the transaction tag of the commit.

The transaction tag will automatically be cleared after the transaction has ended.

Parameter
NameDescription
tagString

The tag to use.

Exceptions
TypeDescription
SQLException

write(Mutation mutation)

public abstract void write(Mutation mutation)

Writes the specified mutation directly to the database and commits the change. The value is readable after the successful completion of this method. Writing multiple mutations to a database by calling this method multiple times mode is inefficient, as each call will need a round trip to the database. Instead, you should consider writing the mutations together by calling CloudSpannerJdbcConnection#write(Iterable).

Calling this method is only allowed in autocommit mode. See CloudSpannerJdbcConnection#bufferedWrite(Iterable) for writing mutations in transactions.

Parameter
NameDescription
mutationcom.google.cloud.spanner.Mutation

The Mutation to write to the database.

Exceptions
TypeDescription
SQLException

if the Connection is not in autocommit mode or if the Connection is closed.

write(Iterable<Mutation> mutations)

public abstract void write(Iterable<Mutation> mutations)

Writes the specified mutations directly to the database and commits the changes. The values are readable after the successful completion of this method.

Calling this method is only allowed in autocommit mode. See CloudSpannerJdbcConnection#bufferedWrite(Iterable) for writing mutations in transactions.

Parameter
NameDescription
mutationsIterable<com.google.cloud.spanner.Mutation>

The Mutations to write to the database.

Exceptions
TypeDescription
SQLException

if the Connection is not in autocommit mode or if the Connection is closed.