Class ConnectionOptions
java.lang.Object
com.google.cloud.spanner.connection.ConnectionOptions
Internal connection API for Google Cloud Spanner. This class may introduce breaking changes
without prior notice.
Options for creating a Connection to a Google Cloud Spanner database.
Usage:
ConnectionOptions options = ConnectionOptions.newBuilder()
.setUri("cloudspanner:/projects/my_project_id/instances/my_instance_id/databases/my_database_name?autocommit=false")
.setCredentialsUrl("/home/cloudspanner-keys/my-key.json")
.build();
try(Connection connection = options.getConnection()) {
try(ResultSet rs = connection.executeQuery(Statement.of("SELECT SingerId, AlbumId, MarketingBudget FROM Albums"))) {
while(rs.next()) {
// do something
}
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder forConnectionOptionsinstances.static interfaceConnectionOptions.ExternalChannelProvidercan be used for to specify an external channel provider. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final StringName of the 'autocommit' connection property.static final StringName of the 'channelProvider' connection property.static final StringName of the 'credentials' connection property.static final StringName of the 'credentialsProvider' connection property.static final Stringstatic final StringName of the 'databaseRole' connection property.static final Stringstatic final Stringstatic final StringName of the 'delay transaction start until first write' property.static final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic StringSet this system property to true to enable transactional connection state by default for PostgreSQL-dialect databases.static final StringName of the 'encodedCredentials' connection property.static final StringName of the 'endpoint' connection property.static final StringName of the 'keep transaction alive' property.static final StringName of the 'lenientMode' connection property.static final Stringstatic final Stringstatic final StringName of the 'maxSessions' connection property.static final StringName of the 'minSessions' connection property.static final StringName of the 'numChannels' connection property.static final StringOAuth token to use for authentication.static final StringName of the 'readonly' connection property.static final StringName of the 'retry aborts internally' connection property.static final StringName of the 'routeToLeader' connection property.static final StringName of the 'rpcPriority' connection property.static final StringName of the 'trackStackTraceOfConnectionCreation' connection property.static final StringName of the 'trackStackTraceOfSessionCheckout' connection property.static final StringName of the property to enable/disable virtual threads for gRPC transport.static final StringName of the property to enable/disable virtual threads for the statement executor. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCloses allSpannerinstances that have been opened by connections during the lifetime of this JVM.com.google.api.gax.rpc.TransportChannelProviderCalls the getChannelProvider() method from the supplied class.Create a newConnectionfrom thisConnectionOptions.com.google.auth.CredentialsTheCredentialsof thisConnectionOptions.The credentials URL of thisConnectionOptionsThe SpannerDatabaseIdthat thisConnectionOptionswill connect toThe Spanner database name that thisConnectionOptionswill connect toThe database role that is used for this connection.static StringgetDefaultProjectId(com.google.auth.Credentials credentials) Gets the default project-id for the current environment as defined byServiceOptions.getDefaultProjectId(), and if none could be found, the project-id of the given credentials if it contains any.getHost()The host and port number that thisConnectionOptionswill connect toThe Spanner Instance ID that thisConnectionOptionswill connect toThe max_commit_delay that should be applied to commit operations on this connection.The maximum number of sessions in the backing session pool of this connection.The minimum number of sessions in the backing session pool of this connection.The number of channels to use for the connection.The Google Project ID that thisConnectionOptionswill connect toTheSessionPoolOptionsof thisConnectionOptions.com.google.cloud.spanner.connection.StatementExecutor.StatementExecutorTypeReturns the executor type that is used by connections that are created from thisConnectionOptionsinstance.getUri()The URI of thisConnectionOptionsAny warnings that were generated while creating theConnectionOptionsinstance.booleanThe initial autocommit value for connections created by thisConnectionOptionsbooleanWhether connections created by thisConnectionOptionswill automatically try to connect to the emulator using the default host/port of the emulator, and automatically create the instance and database that is specified in the connection string if these do not exist on the emulator instance.booleanWhether end-to-end tracing is enabled.booleanThe initial readonly value for connections created by thisConnectionOptionsbooleanThe initial retryAbortsInternally value for connections created by thisConnectionOptionsbooleanWhether connections created by thisConnectionOptionsreturn commit stats.booleanWhether read/write transactions and partitioned DML are preferred to be routed to the leader region.booleanWhether virtual threads should be used for gRPC transport.booleanWhether connections should use virtual threads for connection executors.static ConnectionOptions.BuilderCreate aConnectionOptions.BuilderforConnectionOptions.toString()
-
Field Details
-
ENABLE_TRANSACTIONAL_CONNECTION_STATE_FOR_POSTGRESQL_PROPERTY
Set this system property to true to enable transactional connection state by default for PostgreSQL-dialect databases. The default is currently false. -
AUTOCOMMIT_PROPERTY_NAME
Name of the 'autocommit' connection property.- See Also:
-
READONLY_PROPERTY_NAME
Name of the 'readonly' connection property.- See Also:
-
ROUTE_TO_LEADER_PROPERTY_NAME
Name of the 'routeToLeader' connection property.- See Also:
-
RETRY_ABORTS_INTERNALLY_PROPERTY_NAME
Name of the 'retry aborts internally' connection property.- See Also:
-
USE_VIRTUAL_THREADS_PROPERTY_NAME
Name of the property to enable/disable virtual threads for the statement executor.- See Also:
-
USE_VIRTUAL_GRPC_TRANSPORT_THREADS_PROPERTY_NAME
Name of the property to enable/disable virtual threads for gRPC transport.- See Also:
-
CREDENTIALS_PROPERTY_NAME
Name of the 'credentials' connection property.- See Also:
-
ENCODED_CREDENTIALS_PROPERTY_NAME
Name of the 'encodedCredentials' connection property.- See Also:
-
ENABLE_ENCODED_CREDENTIALS_SYSTEM_PROPERTY
- See Also:
-
CREDENTIALS_PROVIDER_PROPERTY_NAME
Name of the 'credentialsProvider' connection property.- See Also:
-
ENABLE_CREDENTIALS_PROVIDER_SYSTEM_PROPERTY
- See Also:
-
OAUTH_TOKEN_PROPERTY_NAME
OAuth token to use for authentication. Cannot be used in combination with a credentials file.- See Also:
-
MIN_SESSIONS_PROPERTY_NAME
Name of the 'minSessions' connection property.- See Also:
-
MAX_SESSIONS_PROPERTY_NAME
Name of the 'maxSessions' connection property.- See Also:
-
NUM_CHANNELS_PROPERTY_NAME
Name of the 'numChannels' connection property.- See Also:
-
ENDPOINT_PROPERTY_NAME
Name of the 'endpoint' connection property.- See Also:
-
CHANNEL_PROVIDER_PROPERTY_NAME
Name of the 'channelProvider' connection property.- See Also:
-
ENABLE_CHANNEL_PROVIDER_SYSTEM_PROPERTY
- See Also:
-
LENIENT_PROPERTY_NAME
Name of the 'lenientMode' connection property.- See Also:
-
RPC_PRIORITY_NAME
Name of the 'rpcPriority' connection property.- See Also:
-
DDL_IN_TRANSACTION_MODE_PROPERTY_NAME
- See Also:
-
DEFAULT_SEQUENCE_KIND_PROPERTY_NAME
- See Also:
-
DATABASE_ROLE_PROPERTY_NAME
Name of the 'databaseRole' connection property.- See Also:
-
DELAY_TRANSACTION_START_UNTIL_FIRST_WRITE_NAME
Name of the 'delay transaction start until first write' property.- See Also:
-
KEEP_TRANSACTION_ALIVE_PROPERTY_NAME
Name of the 'keep transaction alive' property.- See Also:
-
TRACK_SESSION_LEAKS_PROPERTY_NAME
Name of the 'trackStackTraceOfSessionCheckout' connection property.- See Also:
-
TRACK_CONNECTION_LEAKS_PROPERTY_NAME
Name of the 'trackStackTraceOfConnectionCreation' connection property.- See Also:
-
DATA_BOOST_ENABLED_PROPERTY_NAME
- See Also:
-
AUTO_PARTITION_MODE_PROPERTY_NAME
- See Also:
-
MAX_PARTITIONS_PROPERTY_NAME
- See Also:
-
MAX_PARTITIONED_PARALLELISM_PROPERTY_NAME
- See Also:
-
ENABLE_EXTENDED_TRACING_PROPERTY_NAME
- See Also:
-
ENABLE_API_TRACING_PROPERTY_NAME
- See Also:
-
ENABLE_END_TO_END_TRACING_PROPERTY_NAME
- See Also:
-
AUTO_BATCH_DML_PROPERTY_NAME
- See Also:
-
AUTO_BATCH_DML_UPDATE_COUNT_PROPERTY_NAME
- See Also:
-
AUTO_BATCH_DML_UPDATE_COUNT_VERIFICATION_PROPERTY_NAME
- See Also:
-
-
Method Details
-
getDefaultProjectId
Gets the default project-id for the current environment as defined byServiceOptions.getDefaultProjectId(), and if none could be found, the project-id of the given credentials if it contains any.- Parameters:
credentials- The credentials to use to get the default project-id if none could be found in the environment.- Returns:
- the default project-id.
-
closeSpanner
public static void closeSpanner()Closes allSpannerinstances that have been opened by connections during the lifetime of this JVM. Call this method at the end of your application to free up resources. You must close allConnections that have been opened by your application before calling this method. Failing to do so, will cause this method to throw aSpannerException.This method is also automatically called by a shutdown hook (see
Runtime.addShutdownHook(Thread)) when the JVM is shutdown gracefully. -
newBuilder
Create aConnectionOptions.BuilderforConnectionOptions. Use this method to createConnectionOptionsthat can be used to obtain aConnection.- Returns:
- a new
ConnectionOptions.Builder
-
getConnection
Create a newConnectionfrom thisConnectionOptions. Calling this method multiple times for the sameConnectionOptionswill return multiple instances ofConnections to the same database.- Returns:
- a new
Connectionto the database referenced by thisConnectionOptions
-
getUri
The URI of thisConnectionOptions -
getCredentialsUrl
The credentials URL of thisConnectionOptions -
getStatementExecutorType
public com.google.cloud.spanner.connection.StatementExecutor.StatementExecutorType getStatementExecutorType()Returns the executor type that is used by connections that are created from thisConnectionOptionsinstance. -
getSessionPoolOptions
TheSessionPoolOptionsof thisConnectionOptions. -
getMinSessions
The minimum number of sessions in the backing session pool of this connection. The session pool is shared between all connections in the same JVM that connect to the same Cloud Spanner database using the same connection settings. -
getMaxSessions
The maximum number of sessions in the backing session pool of this connection. The session pool is shared between all connections in the same JVM that connect to the same Cloud Spanner database using the same connection settings. -
getNumChannels
The number of channels to use for the connection. -
getChannelProvider
public com.google.api.gax.rpc.TransportChannelProvider getChannelProvider()Calls the getChannelProvider() method from the supplied class. -
getDatabaseRole
The database role that is used for this connection. Assigning a role to a connection can be used to for example restrict the access of a connection to a specific set of tables. -
getHost
The host and port number that thisConnectionOptionswill connect to -
getProjectId
The Google Project ID that thisConnectionOptionswill connect to -
getInstanceId
The Spanner Instance ID that thisConnectionOptionswill connect to -
getDatabaseName
The Spanner database name that thisConnectionOptionswill connect to -
getDatabaseId
The SpannerDatabaseIdthat thisConnectionOptionswill connect to -
getCredentials
public com.google.auth.Credentials getCredentials()TheCredentialsof thisConnectionOptions. This is either the credentials specified in the credentialsUrl or the default Google application credentials -
isAutocommit
public boolean isAutocommit()The initial autocommit value for connections created by thisConnectionOptions -
isReadOnly
public boolean isReadOnly()The initial readonly value for connections created by thisConnectionOptions -
isRouteToLeader
public boolean isRouteToLeader()Whether read/write transactions and partitioned DML are preferred to be routed to the leader region. -
isEndToEndTracingEnabled
public boolean isEndToEndTracingEnabled()Whether end-to-end tracing is enabled. -
isRetryAbortsInternally
public boolean isRetryAbortsInternally()The initial retryAbortsInternally value for connections created by thisConnectionOptions -
isUseVirtualThreads
public boolean isUseVirtualThreads()Whether connections should use virtual threads for connection executors. -
isUseVirtualGrpcTransportThreads
public boolean isUseVirtualGrpcTransportThreads()Whether virtual threads should be used for gRPC transport. -
getWarnings
Any warnings that were generated while creating theConnectionOptionsinstance. -
isReturnCommitStats
public boolean isReturnCommitStats()Whether connections created by thisConnectionOptionsreturn commit stats. -
getMaxCommitDelay
The max_commit_delay that should be applied to commit operations on this connection. -
isAutoConfigEmulator
public boolean isAutoConfigEmulator()Whether connections created by thisConnectionOptionswill automatically try to connect to the emulator using the default host/port of the emulator, and automatically create the instance and database that is specified in the connection string if these do not exist on the emulator instance. -
getDialect
-
toString
-