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
Connectionto 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 classConnectionOptions.BuilderBuilder forConnectionOptionsinstances.static classConnectionOptions.ConnectionPropertySupported connection properties that can be included in the connection URI.static interfaceConnectionOptions.ExternalChannelProviderConnectionOptions.ExternalChannelProvidercan be used for to specify an external channel provider.
-
Field Summary
Fields Modifier and Type Field Description static StringAUTOCOMMIT_PROPERTY_NAMEName of the 'autocommit' connection property.static StringCHANNEL_PROVIDER_PROPERTY_NAMEName of the 'channelProvider' connection property.static StringCREDENTIALS_PROPERTY_NAMEName of the 'credentials' connection property.static StringCREDENTIALS_PROVIDER_PROPERTY_NAMEName of the 'credentialsProvider' connection property.static StringDATABASE_ROLE_PROPERTY_NAMEName of the 'databaseRole' connection property.static StringENCODED_CREDENTIALS_PROPERTY_NAMEName of the 'encodedCredentials' connection property.static StringLENIENT_PROPERTY_NAMEName of the 'lenientMode' connection property.static StringMAX_SESSIONS_PROPERTY_NAMEName of the 'maxSessions' connection property.static StringMIN_SESSIONS_PROPERTY_NAMEName of the 'minSessions' connection property.static StringNUM_CHANNELS_PROPERTY_NAMEName of the 'numChannels' connection property.static StringOAUTH_TOKEN_PROPERTY_NAMEOAuth token to use for authentication.static StringREADONLY_PROPERTY_NAMEName of the 'readonly' connection property.static StringRETRY_ABORTS_INTERNALLY_PROPERTY_NAMEName of the 'retry aborts internally' connection property.static StringRPC_PRIORITY_NAMEName of the 'rpcPriority' connection property.static Set<ConnectionOptions.ConnectionProperty>VALID_PROPERTIESAll valid connection properties.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidcloseSpanner()Closes allSpannerinstances that have been opened by connections during the lifetime of this JVM.com.google.api.gax.rpc.TransportChannelProvidergetChannelProvider()Calls the getChannelProvider() method from the supplied class.ConnectiongetConnection()Create a newConnectionfrom thisConnectionOptions.com.google.auth.CredentialsgetCredentials()TheCredentialsof thisConnectionOptions.StringgetCredentialsUrl()The credentials URL of thisConnectionOptionsDatabaseIdgetDatabaseId()The SpannerDatabaseIdthat thisConnectionOptionswill connect toStringgetDatabaseName()The Spanner database name that thisConnectionOptionswill connect toStringgetDatabaseRole()The 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.StringgetHost()The host and port number that thisConnectionOptionswill connect toStringgetInstanceId()The Spanner Instance ID that thisConnectionOptionswill connect toIntegergetMaxSessions()The maximum number of sessions in the backing session pool of this connection.IntegergetMinSessions()The minimum number of sessions in the backing session pool of this connection.IntegergetNumChannels()The number of channels to use for the connection.StringgetProjectId()The Google Project ID that thisConnectionOptionswill connect toSessionPoolOptionsgetSessionPoolOptions()TheSessionPoolOptionsof thisConnectionOptions.StringgetUri()The URI of thisConnectionOptionsStringgetWarnings()Any warnings that were generated while creating theConnectionOptionsinstance.booleanisAutocommit()The initial autocommit value for connections created by thisConnectionOptionsbooleanisAutoConfigEmulator()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.booleanisReadOnly()The initial readonly value for connections created by thisConnectionOptionsbooleanisRetryAbortsInternally()The initial retryAbortsInternally value for connections created by thisConnectionOptionsbooleanisReturnCommitStats()Whether connections created by thisConnectionOptionsreturn commit stats.static ConnectionOptions.BuildernewBuilder()Create aConnectionOptions.BuilderforConnectionOptions.StringtoString()
-
-
-
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
-
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 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
public static ConnectionOptions.Builder newBuilder()
Create aConnectionOptions.BuilderforConnectionOptions. Use this method to createConnectionOptionsthat can be used to obtain aConnection.- Returns:
- a new
ConnectionOptions.Builder
-
getConnection
public Connection 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
public String getUri()
The URI of thisConnectionOptions
-
getCredentialsUrl
public String getCredentialsUrl()
The credentials URL of thisConnectionOptions
-
getSessionPoolOptions
public SessionPoolOptions getSessionPoolOptions()
TheSessionPoolOptionsof 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 thisConnectionOptionswill connect to
-
getProjectId
public String getProjectId()
The Google Project ID that thisConnectionOptionswill connect to
-
getInstanceId
public String getInstanceId()
The Spanner Instance ID that thisConnectionOptionswill connect to
-
getDatabaseName
public String getDatabaseName()
The Spanner database name that thisConnectionOptionswill connect to
-
getDatabaseId
public DatabaseId 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
-
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 theConnectionOptionsinstance.
-
isReturnCommitStats
public boolean isReturnCommitStats()
Whether connections created by thisConnectionOptionsreturn commit stats.
-
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.
-
-