@InternalApi public class ConnectionOptions extends Object
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
}
}
}
Modifier and Type | Class and Description |
---|---|
static class |
ConnectionOptions.Builder
Builder for
ConnectionOptions 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. |
Modifier and Type | Field and 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 |
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 Set<ConnectionOptions.ConnectionProperty> |
VALID_PROPERTIES
All valid connection properties.
|
Modifier and Type | Method and Description |
---|---|
static void |
closeSpanner()
Closes all
Spanner instances that have been opened by connections
during the lifetime of this JVM. |
TransportChannelProvider |
getChannelProvider()
Calls the getChannelProvider() method from the supplied class.
|
Connection |
getConnection()
Create a new
Connection from this ConnectionOptions . |
Credentials |
getCredentials()
The
Credentials of this ConnectionOptions . |
String |
getCredentialsUrl()
The credentials URL of this
ConnectionOptions |
DatabaseId |
getDatabaseId()
The Spanner
DatabaseId that this ConnectionOptions will connect to |
String |
getDatabaseName()
The Spanner database name that this
ConnectionOptions will connect to |
static String |
getDefaultProjectId(Credentials credentials)
Gets the default project-id for the current environment as defined by
ServiceOptions.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 this
ConnectionOptions will connect to |
String |
getInstanceId()
The Spanner Instance ID that this
ConnectionOptions will connect to |
Integer |
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 this
ConnectionOptions will connect to |
SessionPoolOptions |
getSessionPoolOptions()
The
SessionPoolOptions of this ConnectionOptions . |
String |
getUri()
The URI of this
ConnectionOptions |
String |
getWarnings()
Any warnings that were generated while creating the
ConnectionOptions instance. |
boolean |
isAutocommit()
The initial autocommit value for connections created by this
ConnectionOptions |
boolean |
isAutoConfigEmulator()
Whether connections created by this
ConnectionOptions 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 this
ConnectionOptions |
boolean |
isRetryAbortsInternally()
The initial retryAbortsInternally value for connections created by this
ConnectionOptions |
boolean |
isReturnCommitStats()
Whether connections created by this
ConnectionOptions return commit stats. |
static ConnectionOptions.Builder |
newBuilder()
Create a
ConnectionOptions.Builder for ConnectionOptions . |
String |
toString() |
public static final String AUTOCOMMIT_PROPERTY_NAME
public static final String READONLY_PROPERTY_NAME
public static final String RETRY_ABORTS_INTERNALLY_PROPERTY_NAME
public static final String CREDENTIALS_PROPERTY_NAME
public static final String ENCODED_CREDENTIALS_PROPERTY_NAME
public static final String CREDENTIALS_PROVIDER_PROPERTY_NAME
public static final String OAUTH_TOKEN_PROPERTY_NAME
public static final String MIN_SESSIONS_PROPERTY_NAME
public static final String MAX_SESSIONS_PROPERTY_NAME
public static final String NUM_CHANNELS_PROPERTY_NAME
public static final String CHANNEL_PROVIDER_PROPERTY_NAME
public static final String LENIENT_PROPERTY_NAME
public static final String RPC_PRIORITY_NAME
public static final Set<ConnectionOptions.ConnectionProperty> VALID_PROPERTIES
public static String getDefaultProjectId(Credentials credentials)
ServiceOptions.getDefaultProjectId()
, and if none could be found, the project-id of the given
credentials if it contains any.credentials
- The credentials to use to get the default project-id if none could be found
in the environment.public static void closeSpanner()
Spanner
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 all Connection
s that have been opened by your application
before calling this method. Failing to do so, will cause this method to throw a SpannerException
.
This method is also automatically called by a shutdown hook (see Runtime.addShutdownHook(Thread)
) when the JVM is shutdown gracefully.
public static ConnectionOptions.Builder newBuilder()
ConnectionOptions.Builder
for ConnectionOptions
. Use this method to create ConnectionOptions
that can be used to obtain a Connection
.ConnectionOptions.Builder
public Connection getConnection()
Connection
from this ConnectionOptions
. Calling this method
multiple times for the same ConnectionOptions
will return multiple instances of Connection
s to the same database.Connection
to the database referenced by this ConnectionOptions
public String getUri()
ConnectionOptions
public String getCredentialsUrl()
ConnectionOptions
public SessionPoolOptions getSessionPoolOptions()
SessionPoolOptions
of this ConnectionOptions
.public Integer getMinSessions()
public Integer getMaxSessions()
public Integer getNumChannels()
public TransportChannelProvider getChannelProvider()
public String getHost()
ConnectionOptions
will connect topublic String getProjectId()
ConnectionOptions
will connect topublic String getInstanceId()
ConnectionOptions
will connect topublic String getDatabaseName()
ConnectionOptions
will connect topublic DatabaseId getDatabaseId()
DatabaseId
that this ConnectionOptions
will connect topublic Credentials getCredentials()
Credentials
of this ConnectionOptions
. This is either the credentials
specified in the credentialsUrl or the default Google application credentialspublic boolean isAutocommit()
ConnectionOptions
public boolean isReadOnly()
ConnectionOptions
public boolean isRetryAbortsInternally()
ConnectionOptions
@Nullable public String getWarnings()
ConnectionOptions
instance.public boolean isReturnCommitStats()
ConnectionOptions
return commit stats.public boolean isAutoConfigEmulator()
ConnectionOptions
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.Copyright © 2022 Google LLC. All rights reserved.