Interface CloudSpannerJdbcConnection
- All Superinterfaces:
AutoCloseable,Connection,Wrapper
JdbcDriver will implement this interface.
Calling Wrapper.unwrap(Class) with CloudSpannerJdbcConnection class as
input on a Connection returned by the Cloud Spanner JDBC Driver will return a CloudSpannerJdbcConnection instance.
-
Field Summary
Fields inherited from interface java.sql.Connection
TRANSACTION_NONE, TRANSACTION_READ_COMMITTED, TRANSACTION_READ_UNCOMMITTED, TRANSACTION_REPEATABLE_READ, TRANSACTION_SERIALIZABLE -
Method Summary
Modifier and TypeMethodDescriptionvoidaddTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener listener) voidDeprecated.voidbufferedWrite(com.google.cloud.spanner.Mutation mutation) Buffers the given mutation locally on the current transaction of thisConnection.voidbufferedWrite(Iterable<com.google.cloud.spanner.Mutation> mutations) Buffers the given mutations locally on the current transaction of thisConnection.com.google.cloud.spanner.connection.AutocommitDmlModecom.google.cloud.spanner.CommitResponsedefault com.google.cloud.spanner.Dialectdefault intReturns the maximum degree of parallelism that is used for partitioned queries.default intGets the maximum number of partitions that should be included as a hint to Cloud Spanner when partitioning a query on this connection.Gets the current query optimizer version of this connection.default byte[]com.google.cloud.spanner.TimestampBoundcom.google.cloud.spanner.connection.SavepointSupportReturns the current savepoint support for this connection.default Stringcom.google.cloud.spanner.connection.TransactionModeDeprecated.Iterator<com.google.cloud.spanner.connection.TransactionRetryListener>default Stringdefault booleanReturns whether this connection will execute all queries as partitioned queries.default booleanReturns whether data boost is enabled for partitioned queries.booleanbooleanbooleanbooleanbooleanremoveTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener listener) booleanDeprecated.voidsetAutocommitDmlMode(com.google.cloud.spanner.connection.AutocommitDmlMode mode) Sets the mode for executing DML statements in autocommit mode for this connection.default voidsetAutoPartitionMode(boolean alwaysUsePartitionedQueries) Sets whether this connection should always use partitioned queries when a query is executed on this connection.default voidsetDataBoostEnabled(boolean dataBoostEnabled) Enable data boost for partitioned queries.default voidsetMaxPartitionedParallelism(int maxThreads) Sets the maximum degree of parallelism that is used when executing a partitioned query.default voidsetMaxPartitions(int maxPartitions) Sets the maximum number of partitions that should be included as a hint to Cloud Spanner when partitioning a query on this connection.voidsetOptimizerVersion(String optimizerVersion) Sets the query optimizer version to use for this connection.default voidsetProtoDescriptors(byte[] protoDescriptors) Sets the proto descriptors to use for the next DDL statement (single or batch) that will be executed.default voidsetProtoDescriptors(InputStream protoDescriptors) Sets the proto descriptors to use for the next DDL statement (single or batch) that will be executed.voidsetReadOnlyStaleness(com.google.cloud.spanner.TimestampBound staleness) Sets the staleness to use for the current read-only transaction.voidsetRetryAbortsInternally(boolean retryAbortsInternally) Sets whether this connection will internally retry read/write transactions that abort.voidsetReturnCommitStats(boolean returnCommitStats) Sets whether this connection should request commit statistics from Cloud Spanner for read/write transactions and for DML statements in autocommit mode.voidsetSavepointSupport(com.google.cloud.spanner.connection.SavepointSupport savepointSupport) Sets how savepoints should be supported on this connection.default voidsetStatementTag(String tag) Sets the statement tag to use for the next statement that will be executed.voidsetTransactionMode(com.google.cloud.spanner.connection.TransactionMode transactionMode) Sets the transaction mode to use for current transaction.default voidsetTransactionTag(String tag) Sets the transaction tag to use for the current transaction.voidwrite(com.google.cloud.spanner.Mutation mutation) Writes the specified mutation directly to the database and commits the change.voidWrites the specified mutations directly to the database and commits the changes.Methods inherited from interface java.sql.Connection
abort, clearWarnings, close, commit, createArrayOf, createBlob, createClob, createNClob, createSQLXML, createStatement, createStatement, createStatement, createStruct, getAutoCommit, getCatalog, getClientInfo, getClientInfo, getHoldability, getMetaData, getNetworkTimeout, getSchema, getTransactionIsolation, getTypeMap, getWarnings, isClosed, isReadOnly, isValid, nativeSQL, prepareCall, prepareCall, prepareCall, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, prepareStatement, releaseSavepoint, rollback, rollback, setAutoCommit, setCatalog, setClientInfo, setClientInfo, setHoldability, setNetworkTimeout, setReadOnly, setSavepoint, setSavepoint, setSchema, setTransactionIsolation, setTypeMapMethods inherited from interface java.sql.Wrapper
isWrapperFor, unwrap
-
Method Details
-
setTransactionTag
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.
- Parameters:
tag- The tag to use.- Throws:
SQLException
-
getTransactionTag
- Returns:
- The transaction tag of the current transaction.
- Throws:
SQLException
-
setStatementTag
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.- Parameters:
tag- The statement tag to use with the next statement that will be executed on this connection.- Throws:
SQLException
-
getStatementTag
- Returns:
- The statement tag that will be used with the next statement that is executed on this connection.
- Throws:
SQLException
-
setTransactionMode
void setTransactionMode(com.google.cloud.spanner.connection.TransactionMode transactionMode) throws SQLException 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.- Parameters:
transactionMode- The transaction mode to use for the current transaction.TransactionMode.READ_ONLY_TRANSACTIONwill 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 useTransactionMode.READ_ONLY_TRANSACTIONinstead ofTransactionMode.READ_WRITE_TRANSACTIONwhen possible, as read-only transactions do not acquire locks on Cloud Spanner, and read-only transactions never abort.TransactionMode.READ_WRITE_TRANSACTIONthis value is only allowed when the connection is not in read-only mode and will create a read-write transaction. IfisRetryAbortsInternally()istrue, each read/write transaction will keep track of a running SHA256 checksum for eachResultSetthat is returned in order to be able to retry the transaction in case the transaction is aborted by Spanner.
- Throws:
SQLException
-
getTransactionMode
- Returns:
- the transaction mode of the current transaction. This method may only be called when the connection is in a transaction.
- Throws:
SQLException
-
setAutocommitDmlMode
void setAutocommitDmlMode(com.google.cloud.spanner.connection.AutocommitDmlMode mode) throws SQLException 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 ofAutocommitDmlMode.TRANSACTIONALwhen autocommit mode is changed on the connection.- Parameters:
mode- The DML autocommit mode to useAutocommitDmlMode.TRANSACTIONALDML statements are executed as single read-write transaction. After successful execution, the DML statement is guaranteed to have been applied exactly once to the databaseAutocommitDmlMode.PARTITIONED_NON_ATOMICDML 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.
- Throws:
SQLException
-
getAutocommitDmlMode
- Returns:
- the current
AutocommitDmlModesetting for this connection. This method may only be called on a connection that is in autocommit mode and not while in a temporary transaction. - Throws:
SQLException
-
setReadOnlyStaleness
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 isTransactionMode.READ_ONLY_TRANSACTIONand there is no transaction that has started, or when the connection is in read-only and autocommit mode.- Parameters:
staleness- The staleness to use for the current but not yet started read-only transaction- Throws:
SQLException
-
getReadOnlyStaleness
- Returns:
- 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.
- Throws:
SQLException
-
setOptimizerVersion
Sets the query optimizer version to use for this connection.- Parameters:
optimizerVersion- The query optimizer version to use. Must be a valid optimizer version number, the stringLATESTor an empty string. The empty string will instruct the connection to use the optimizer version that is defined in the environment variableSPANNER_OPTIMIZER_VERSION. If no value is specified in the environment variable, the default query optimizer of Cloud Spanner is used.- Throws:
SQLException
-
getOptimizerVersion
Gets the current query optimizer version of this connection.- Returns:
- The query optimizer version that is currently used by this connection.
- Throws:
SQLException
-
isInTransaction
- Returns:
trueif this connection has a transaction (that has not necessarily started). This method will only return false when theConnectionis in autocommit mode and no explicit transaction has been started by executing `BEGIN TRANSACTION`. If theConnectionis not in autocommit mode, there will always be a transaction.- Throws:
SQLException
-
isTransactionStarted
- Returns:
trueif this connection has a transaction that has started. A transaction is automatically started by the first statement that is executed in the transaction.- Throws:
SQLException
-
getCommitTimestamp
- Returns:
- the commit
Timestampof 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 aSQLException. - Throws:
SQLException
-
getCommitResponse
- Returns:
- the
CommitResponseof the last read/write transaction. If the last transaction was not a read/write transaction, or a read/write transaction that did not return aCommitResponsebecause the transaction was not committed, the method will throw aSQLException. TheCommitResponsewill includeCommitStatsifisReturnCommitStats()returns true. - Throws:
SQLException
-
setReturnCommitStats
Sets whether this connection should request commit statistics from Cloud Spanner for read/write transactions and for DML statements in autocommit mode.- Throws:
SQLException
-
isReturnCommitStats
- Returns:
- true if this connection requests commit statistics from Cloud Spanner.
- Throws:
SQLException
-
getReadTimestamp
- Returns:
- the read
Timestampof 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 aSQLException. - Throws:
SQLException
-
isRetryAbortsInternally
- Returns:
trueif 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.- Throws:
SQLException
-
setRetryAbortsInternally
Sets whether this connection will internally retry read/write transactions that abort. The default istrue. When internal retry is enabled, theConnectionwill keep track of a running SHA256 checksum of allResultSets 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 anAbortedDueToConcurrentModificationException.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.
- Parameters:
retryAbortsInternally- Set totrueto internally retry transactions that are aborted by Spanner. When set tofalse, any database call on a transaction that has been aborted by Cloud Spanner will throw anAbortedExceptioninstead of being retried. Set this to false if your application already uses retry loops to handleAbortedExceptions.- Throws:
SQLException
-
getSavepointSupport
Returns the current savepoint support for this connection.- Throws:
SQLException
-
setSavepointSupport
void setSavepointSupport(com.google.cloud.spanner.connection.SavepointSupport savepointSupport) throws SQLException Sets how savepoints should be supported on this connection.- Throws:
SQLException
-
write
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 callingwrite(Iterable).Calling this method is only allowed in autocommit mode. See
bufferedWrite(Iterable)for writing mutations in transactions.- Parameters:
mutation- TheMutationto write to the database.- Throws:
SQLException- if theConnectionis not in autocommit mode or if theConnectionis closed.
-
write
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
bufferedWrite(Iterable)for writing mutations in transactions.- Parameters:
mutations- TheMutations to write to the database.- Throws:
SQLException- if theConnectionis not in autocommit mode or if theConnectionis closed.
-
bufferedWrite
Buffers the given mutation locally on the current transaction of thisConnection. The mutation will be written to the database at the next call toConnection.commit(). The value will not be readable on thisConnectionbefore the transaction is committed.Calling this method is only allowed when not in autocommit mode. See
write(Mutation)for writing mutations in autocommit mode.- Parameters:
mutation- theMutationto buffer for writing to the database on the next commit.- Throws:
SQLException- if theConnectionis in autocommit mode or theConnectionis closed.
-
bufferedWrite
Buffers the given mutations locally on the current transaction of thisConnection. The mutations will be written to the database at the next call toConnection.commit(). The values will not be readable on thisConnectionbefore the transaction is committed.Calling this method is only allowed when not in autocommit mode. See
write(Iterable)for writing mutations in autocommit mode.- Parameters:
mutations- theMutations to buffer for writing to the database on the next commit.- Throws:
SQLException- if theConnectionis in autocommit mode or theConnectionis closed.
-
getConnectionUrl
String getConnectionUrl()- Returns:
- a connection URL that can be used to create a new
Connectionthat 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
default com.google.cloud.spanner.Dialect getDialect()- Returns:
- The
Dialectthat is used by this connection.
-
setDataBoostEnabled
Enable data boost for partitioned queries. See alsoCloudSpannerJdbcStatement.partitionQuery(String, PartitionOptions, QueryOption...)andCloudSpannerJdbcPreparedStatement.partitionQuery(PartitionOptions, QueryOption...).- Throws:
SQLException
-
isDataBoostEnabled
Returns whether data boost is enabled for partitioned queries. See alsoCloudSpannerJdbcStatement.partitionQuery(String, PartitionOptions, QueryOption...)andCloudSpannerJdbcPreparedStatement.partitionQuery(PartitionOptions, QueryOption...).- Throws:
SQLException
-
setAutoPartitionMode
Sets whether this connection should always use partitioned queries when a query is executed on this connection. Setting this flag totrueand then executing a query that cannot be partitioned, or executing a query in a read/write transaction, will cause an error. Use this flag in combination withsetDataBoostEnabled(boolean)to force all queries on this connection to use data boost.- Throws:
SQLException
-
isAutoPartitionMode
Returns whether this connection will execute all queries as partitioned queries.- Throws:
SQLException
-
setMaxPartitions
Sets the maximum number of partitions that should be included as a hint to Cloud Spanner when partitioning a query on this connection. Note that this is only a hint and Cloud Spanner might choose to ignore the hint.- Throws:
SQLException
-
getMaxPartitions
Gets the maximum number of partitions that should be included as a hint to Cloud Spanner when partitioning a query on this connection. Note that this is only a hint and Cloud Spanner might choose to ignore the hint.- Throws:
SQLException
-
setMaxPartitionedParallelism
Sets the maximum degree of parallelism that is used when executing a partitioned query. A partitioned query will use up tomaxThreadsto execute and retrieve the results from Cloud Spanner. Set this value to0> to use the number of available processors as returned byRuntime.availableProcessors().- Throws:
SQLException
-
getMaxPartitionedParallelism
Returns the maximum degree of parallelism that is used for partitioned queries.- Throws:
SQLException
-
addTransactionRetryListener
void addTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener listener) throws SQLException - Throws:
SQLException- if theConnectionis closed.- See Also:
-
Connection.addTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener)
-
addTransactionRetryListener
Deprecated.UseaddTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener)instead.- Throws:
SQLException
-
removeTransactionRetryListener
boolean removeTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener listener) throws SQLException - Throws:
SQLException- if theConnectionis closed.- See Also:
-
Connection.removeTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener)
-
removeTransactionRetryListener
@Deprecated boolean removeTransactionRetryListener(TransactionRetryListener listener) throws SQLException Deprecated.UseremoveTransactionRetryListener(com.google.cloud.spanner.connection.TransactionRetryListener)instead.- Throws:
SQLException
-
getTransactionRetryListeners
Deprecated.UsegetTransactionRetryListenersFromConnection()instead.- Throws:
SQLException
-
getTransactionRetryListenersFromConnection
Iterator<com.google.cloud.spanner.connection.TransactionRetryListener> getTransactionRetryListenersFromConnection() throws SQLException- Throws:
SQLException- if theConnectionis closed.- See Also:
-
Connection.getTransactionRetryListeners()
-
setProtoDescriptors
Sets the proto descriptors to use for the next DDL statement (single or batch) that will be executed. The proto descriptor is automatically cleared after the statement is executed.- Parameters:
protoDescriptors- The proto descriptors to use with the next DDL statement (single or batch) that will be executed on this connection.- Throws:
SQLException
-
setProtoDescriptors
default void setProtoDescriptors(@Nonnull InputStream protoDescriptors) throws SQLException, IOException Sets the proto descriptors to use for the next DDL statement (single or batch) that will be executed. The proto descriptor is automatically cleared after the statement is executed.- Parameters:
protoDescriptors- The proto descriptors to use with the next DDL statement (single or batch) that will be executed on this connection.- Throws:
SQLExceptionIOException
-
getProtoDescriptors
- Returns:
- The proto descriptor that will be used with the next DDL statement (single or batch) that is executed on this connection.
- Throws:
SQLException
-