Class ConnectionOptions
- java.lang.Object
-
- com.google.cloud.spanner.connection.ConnectionOptions
-
@InternalApi public class ConnectionOptions extends Object
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 Classes Modifier and Type Class Description static class
ConnectionOptions.Builder
Builder forConnectionOptions
instances.static class
ConnectionOptions.ConnectionProperty
Supported connection properties that can be included in the connection URI.static interface
ConnectionOptions.ExternalChannelProvider
ConnectionOptions.ExternalChannelProvider
can be used for to specify an external channel provider.
-
Field Summary
Fields Modifier and Type Field Description static String
AUTOCOMMIT_PROPERTY_NAME
Name of the 'autocommit' connection property.static String
CHANNEL_PROVIDER_PROPERTY_NAME
Name of the 'channelProvider' connection property.static String
CREDENTIALS_PROPERTY_NAME
Name of the 'credentials' connection property.static String
CREDENTIALS_PROVIDER_PROPERTY_NAME
Name of the 'credentialsProvider' connection property.static String
DATABASE_ROLE_PROPERTY_NAME
Name of the 'databaseRole' connection property.static String
ENCODED_CREDENTIALS_PROPERTY_NAME
Name of the 'encodedCredentials' connection property.static String
LENIENT_PROPERTY_NAME
Name of the 'lenientMode' connection property.static String
MAX_SESSIONS_PROPERTY_NAME
Name of the 'maxSessions' connection property.static String
MIN_SESSIONS_PROPERTY_NAME
Name of the 'minSessions' connection property.static String
NUM_CHANNELS_PROPERTY_NAME
Name of the 'numChannels' connection property.static String
OAUTH_TOKEN_PROPERTY_NAME
OAuth token to use for authentication.static String
READONLY_PROPERTY_NAME
Name of the 'readonly' connection property.static String
RETRY_ABORTS_INTERNALLY_PROPERTY_NAME
Name of the 'retry aborts internally' connection property.static String
RPC_PRIORITY_NAME
Name of the 'rpcPriority' connection property.static String
TRACK_CONNECTION_LEAKS_PROPERTY_NAME
Name of the 'trackStackTraceOfConnectionCreation' connection property.static String
TRACK_SESSION_LEAKS_PROPERTY_NAME
Name of the 'trackStackTraceOfSessionCheckout' connection property.static Set<ConnectionOptions.ConnectionProperty>
VALID_PROPERTIES
All valid connection properties.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static void
closeSpanner()
Closes allSpanner
instances that have been opened by connections during the lifetime of this JVM.com.google.api.gax.rpc.TransportChannelProvider
getChannelProvider()
Calls the getChannelProvider() method from the supplied class.Connection
getConnection()
Create a newConnection
from thisConnectionOptions
.com.google.auth.Credentials
getCredentials()
TheCredentials
of thisConnectionOptions
.String
getCredentialsUrl()
The credentials URL of thisConnectionOptions
DatabaseId
getDatabaseId()
The SpannerDatabaseId
that thisConnectionOptions
will connect toString
getDatabaseName()
The Spanner database name that thisConnectionOptions
will connect toString
getDatabaseRole()
The 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.String
getHost()
The host and port number that thisConnectionOptions
will connect toString
getInstanceId()
The Spanner Instance ID that thisConnectionOptions
will connect toInteger
getMaxSessions()
The maximum number of sessions in the backing session pool of this connection.Integer
getMinSessions()
The minimum number of sessions in the backing session pool of this connection.Integer
getNumChannels()
The number of channels to use for the connection.String
getProjectId()
The Google Project ID that thisConnectionOptions
will connect toSessionPoolOptions
getSessionPoolOptions()
TheSessionPoolOptions
of thisConnectionOptions
.String
getUri()
The URI of thisConnectionOptions
String
getWarnings()
Any warnings that were generated while creating theConnectionOptions
instance.boolean
isAutocommit()
The initial autocommit value for connections created by thisConnectionOptions
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.boolean
isReadOnly()
The initial readonly value for connections created by thisConnectionOptions
boolean
isRetryAbortsInternally()
The initial retryAbortsInternally value for connections created by thisConnectionOptions
boolean
isReturnCommitStats()
Whether connections created by thisConnectionOptions
return commit stats.static ConnectionOptions.Builder
newBuilder()
Create aConnectionOptions.Builder
forConnectionOptions
.String
toString()
-
-
-
Field Detail
-
AUTOCOMMIT_PROPERTY_NAME
public static final String AUTOCOMMIT_PROPERTY_NAME
Name of the 'autocommit' connection property.- See Also:
- Constant Field Values
-
READONLY_PROPERTY_NAME
public static final String READONLY_PROPERTY_NAME
Name of the 'readonly' connection property.- See Also:
- Constant Field Values
-
RETRY_ABORTS_INTERNALLY_PROPERTY_NAME
public static final String RETRY_ABORTS_INTERNALLY_PROPERTY_NAME
Name of the 'retry aborts internally' connection property.- See Also:
- Constant Field Values
-
CREDENTIALS_PROPERTY_NAME
public static final String CREDENTIALS_PROPERTY_NAME
Name of the 'credentials' connection property.- See Also:
- Constant Field Values
-
ENCODED_CREDENTIALS_PROPERTY_NAME
public static final String ENCODED_CREDENTIALS_PROPERTY_NAME
Name of the 'encodedCredentials' connection property.- See Also:
- Constant Field Values
-
CREDENTIALS_PROVIDER_PROPERTY_NAME
public static final String CREDENTIALS_PROVIDER_PROPERTY_NAME
Name of the 'credentialsProvider' connection property.- See Also:
- Constant Field Values
-
OAUTH_TOKEN_PROPERTY_NAME
public static final String OAUTH_TOKEN_PROPERTY_NAME
OAuth token to use for authentication. Cannot be used in combination with a credentials file.- See Also:
- Constant Field Values
-
MIN_SESSIONS_PROPERTY_NAME
public static final String MIN_SESSIONS_PROPERTY_NAME
Name of the 'minSessions' connection property.- See Also:
- Constant Field Values
-
MAX_SESSIONS_PROPERTY_NAME
public static final String MAX_SESSIONS_PROPERTY_NAME
Name of the 'maxSessions' connection property.- See Also:
- Constant Field Values
-
NUM_CHANNELS_PROPERTY_NAME
public static final String NUM_CHANNELS_PROPERTY_NAME
Name of the 'numChannels' connection property.- See Also:
- Constant Field Values
-
CHANNEL_PROVIDER_PROPERTY_NAME
public static final String CHANNEL_PROVIDER_PROPERTY_NAME
Name of the 'channelProvider' connection property.- See Also:
- Constant Field Values
-
LENIENT_PROPERTY_NAME
public static final String LENIENT_PROPERTY_NAME
Name of the 'lenientMode' connection property.- See Also:
- Constant Field Values
-
RPC_PRIORITY_NAME
public static final String RPC_PRIORITY_NAME
Name of the 'rpcPriority' connection property.- See Also:
- Constant Field Values
-
DATABASE_ROLE_PROPERTY_NAME
public static final String DATABASE_ROLE_PROPERTY_NAME
Name of the 'databaseRole' connection property.- See Also:
- Constant Field Values
-
TRACK_SESSION_LEAKS_PROPERTY_NAME
public static final String TRACK_SESSION_LEAKS_PROPERTY_NAME
Name of the 'trackStackTraceOfSessionCheckout' connection property.- See Also:
- Constant Field Values
-
TRACK_CONNECTION_LEAKS_PROPERTY_NAME
public static final String TRACK_CONNECTION_LEAKS_PROPERTY_NAME
Name of the 'trackStackTraceOfConnectionCreation' connection property.- See Also:
- Constant Field Values
-
VALID_PROPERTIES
public static final Set<ConnectionOptions.ConnectionProperty> VALID_PROPERTIES
All valid connection properties.
-
-
Method Detail
-
getDefaultProjectId
public 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.- 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
public static ConnectionOptions.Builder newBuilder()
Create aConnectionOptions.Builder
forConnectionOptions
. Use this method to createConnectionOptions
that can be used to obtain aConnection
.- Returns:
- a new
ConnectionOptions.Builder
-
getConnection
public Connection 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
public String getUri()
The URI of thisConnectionOptions
-
getCredentialsUrl
public String getCredentialsUrl()
The credentials URL of thisConnectionOptions
-
getSessionPoolOptions
public SessionPoolOptions getSessionPoolOptions()
TheSessionPoolOptions
of thisConnectionOptions
.
-
getMinSessions
public Integer 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
public Integer 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
public Integer 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
public String 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
public String getHost()
The host and port number that thisConnectionOptions
will connect to
-
getProjectId
public String getProjectId()
The Google Project ID that thisConnectionOptions
will connect to
-
getInstanceId
public String getInstanceId()
The Spanner Instance ID that thisConnectionOptions
will connect to
-
getDatabaseName
public String getDatabaseName()
The Spanner database name that thisConnectionOptions
will connect to
-
getDatabaseId
public DatabaseId 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
-
isRetryAbortsInternally
public boolean isRetryAbortsInternally()
The initial retryAbortsInternally value for connections created by thisConnectionOptions
-
getWarnings
@Nullable public String 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.
-
-