Class 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
         }
       }
     }
     
     
     
    • 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
    • Method Detail

      • getDefaultProjectId

        public static String getDefaultProjectId​(com.google.auth.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.
        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 all 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 Connections 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.

      • 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.
      • getInstanceId

        public String getInstanceId()
        The Spanner Instance ID that this ConnectionOptions will connect to
      • getDatabaseName

        public String getDatabaseName()
        The Spanner database name that this ConnectionOptions will connect to
      • getCredentials

        public com.google.auth.Credentials getCredentials()
        The Credentials of this ConnectionOptions. 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 this ConnectionOptions
      • isReadOnly

        public boolean isReadOnly()
        The initial readonly value for connections created by this ConnectionOptions
      • isRetryAbortsInternally

        public boolean isRetryAbortsInternally()
        The initial retryAbortsInternally value for connections created by this ConnectionOptions
      • isReturnCommitStats

        public boolean isReturnCommitStats()
        Whether connections created by this ConnectionOptions return commit stats.
      • isAutoConfigEmulator

        public 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.