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
Modifier and TypeClassDescriptionstatic class
Builder forConnectionOptions
instances.static class
Supported connection properties that can be included in the connection URI.static interface
ConnectionOptions.ExternalChannelProvider
can be used for to specify an external channel provider. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
static final String
Name of the 'autocommit' connection property.static final String
Name of the 'channelProvider' connection property.static final String
Name of the 'credentials' connection property.static final String
Name of the 'credentialsProvider' connection property.static final String
static final String
Name of the 'databaseRole' connection property.static final String
Name of the 'delay transaction start until first write' property.static final String
static final String
static final String
static final String
Name of the 'encodedCredentials' connection property.static final String
Name of the 'lenientMode' connection property.static final String
static final String
static final String
Name of the 'maxSessions' connection property.static final String
Name of the 'minSessions' connection property.static final String
Name of the 'numChannels' connection property.static final String
OAuth token to use for authentication.static final String
Name of the 'readonly' connection property.static final String
Name of the 'retry aborts internally' connection property.static final String
Name of the 'routeToLeader' connection property.static final String
Name of the 'rpcPriority' connection property.static final String
Name of the 'trackStackTraceOfConnectionCreation' connection property.static final String
Name of the 'trackStackTraceOfSessionCheckout' connection property.static final Set<ConnectionOptions.ConnectionProperty>
All valid connection properties. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Closes allSpanner
instances that have been opened by connections during the lifetime of this JVM.com.google.api.gax.rpc.TransportChannelProvider
Calls the getChannelProvider() method from the supplied class.Create a newConnection
from thisConnectionOptions
.com.google.auth.Credentials
TheCredentials
of thisConnectionOptions
.The credentials URL of thisConnectionOptions
The SpannerDatabaseId
that thisConnectionOptions
will connect toThe Spanner database name that thisConnectionOptions
will connect toThe database role that is used for this connection.static String
getDefaultProjectId
(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 thisConnectionOptions
will connect toThe Spanner Instance ID that thisConnectionOptions
will connect toThe 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 thisConnectionOptions
will connect toTheSessionPoolOptions
of thisConnectionOptions
.getUri()
The URI of thisConnectionOptions
Any warnings that were generated while creating theConnectionOptions
instance.boolean
The initial autocommit value for connections created by thisConnectionOptions
boolean
Whether connections created by thisConnectionOptions
will 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.boolean
The initial readonly value for connections created by thisConnectionOptions
boolean
The initial retryAbortsInternally value for connections created by thisConnectionOptions
boolean
Whether connections created by thisConnectionOptions
return commit stats.boolean
Whether read/write transactions and partitioned DML are preferred to be routed to the leader region.static ConnectionOptions.Builder
Create aConnectionOptions.Builder
forConnectionOptions
.toString()
-
Field Details
-
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:
-
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:
-
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:
-
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:
-
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:
-
VALID_PROPERTIES
All valid connection properties.
-
-
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 allSpanner
instances 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 allConnection
s 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.Builder
forConnectionOptions
. Use this method to createConnectionOptions
that can be used to obtain aConnection
.- Returns:
- a new
ConnectionOptions.Builder
-
getConnection
Create a newConnection
from thisConnectionOptions
. Calling this method multiple times for the sameConnectionOptions
will return multiple instances ofConnection
s to the same database.- Returns:
- a new
Connection
to the database referenced by thisConnectionOptions
-
getUri
The URI of thisConnectionOptions
-
getCredentialsUrl
The credentials URL of thisConnectionOptions
-
getSessionPoolOptions
TheSessionPoolOptions
of 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 thisConnectionOptions
will connect to -
getProjectId
The Google Project ID that thisConnectionOptions
will connect to -
getInstanceId
The Spanner Instance ID that thisConnectionOptions
will connect to -
getDatabaseName
The Spanner database name that thisConnectionOptions
will connect to -
getDatabaseId
The SpannerDatabaseId
that thisConnectionOptions
will connect to -
getCredentials
public com.google.auth.Credentials getCredentials()TheCredentials
of 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. -
isRetryAbortsInternally
public boolean isRetryAbortsInternally()The initial retryAbortsInternally value for connections created by thisConnectionOptions
-
getWarnings
Any warnings that were generated while creating theConnectionOptions
instance. -
isReturnCommitStats
public boolean isReturnCommitStats()Whether connections created by thisConnectionOptions
return commit stats. -
isAutoConfigEmulator
public boolean isAutoConfigEmulator()Whether connections created by thisConnectionOptions
will 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
-