# Class ConnectionOptions.Builder (6.118.0)

6.118.0 (latest) 6.116.0 6.111.1 6.108.0 6.107.0 6.103.0 6.102.1 6.101.1 6.100.0 6.99.0 6.98.1 6.97.1 6.96.1 6.95.1 6.94.0 6.93.0 6.89.0 6.88.0 6.87.0 6.86.0 6.85.0 6.83.0 6.82.0 6.80.1 6.79.0 6.77.0 6.74.1 6.72.0 6.71.0 6.69.0 6.68.0 6.66.0 6.65.1 6.62.0 6.60.0 6.58.0 6.57.0 6.56.0 6.55.0 6.54.0 6.53.0 6.52.1 6.51.0 6.50.1 6.49.0 6.25.1 6.24.0 6.23.4 6.22.0 6.21.2 6.20.0 6.19.1 6.18.0 6.17.4 6.14.1

    public static class ConnectionOptions.Builder

Builder for [ConnectionOptions](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions) instances.

## Inheritance

[java.lang.Object](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) \> ConnectionOptions.Builder

## Inherited Members

[Object.clone()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#clone--) [Object.equals(Object)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-) [Object.finalize()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#finalize--) [Object.getClass()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--) [Object.hashCode()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [Object.notify()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notify--) [Object.notifyAll()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#notifyAll--) [Object.toString()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#toString--) [Object.wait()](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait--) [Object.wait(long)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-) [Object.wait(long,int)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-int-)

## Static Fields

### EXTERNAL_HOST_FORMAT

    public static final String EXTERNAL_HOST_FORMAT

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### SPANNER_OMNI_FORMAT

    public static final String SPANNER_OMNI_FORMAT

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

### SPANNER_URI_FORMAT

    public static final String SPANNER_URI_FORMAT

Spanner [ConnectionOptions](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions) URI format.

| **Field Value** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |   |

## Methods

### build()

    public ConnectionOptions build()

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions` | the [ConnectionOptions](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions) |

### setCredentialsUrl(String credentialsUrl)

    public ConnectionOptions.Builder setCredentialsUrl(String credentialsUrl)

Sets the URL of the credentials file to use for this connection. The URL may be a reference
to a file on the local file system, or to a file on Google Cloud Storage. References to
Google Cloud Storage files are only allowed when the application is running on Google Cloud
and the environment has access to the specified storage location. It also requires that the
Google Cloud Storage client library is present on the class path. The Google Cloud Storage
library is not automatically added as a dependency by the JDBC driver.

If you do not specify a credentialsUrl (either by using this setter, or by specifying on
the connection URI), the credentials returned by GoogleCredentials#getApplicationDefault() will be used for the connection.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `credentialsUrl` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` A valid file or Google Cloud Storage URL for the credentials file to be used. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder` | this builder |

### setOAuthToken(String oauthToken)

    public ConnectionOptions.Builder setOAuthToken(String oauthToken)

Sets the OAuth token to use with this connection. The token must be a valid token with access
to the resources (project/instance/database) that the connection will be accessing. This
authentication method cannot be used in combination with a credentials file. If both an OAuth
token and a credentials file is specified, the [#build()](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder#com_google_cloud_spanner_connection_ConnectionOptions_Builder_build__) method will throw an
exception.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `oauthToken` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` A valid OAuth token for the Google Cloud project that is used by this connection. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder` | this builder |

### setOpenTelemetry(OpenTelemetry openTelemetry)

    public ConnectionOptions.Builder setOpenTelemetry(OpenTelemetry openTelemetry)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `openTelemetry` | `io.opentelemetry.api.OpenTelemetry` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder` |   |

### setSessionPoolOptions(SessionPoolOptions sessionPoolOptions)

    public ConnectionOptions.Builder setSessionPoolOptions(SessionPoolOptions sessionPoolOptions)

Sets the [SessionPoolOptions](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.SessionPoolOptions) to use for the connection.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `sessionPoolOptions` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.SessionPoolOptions` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder` |   |

### setStatementExecutorType(StatementExecutor.StatementExecutorType statementExecutorType)

    public ConnectionOptions.Builder setStatementExecutorType(StatementExecutor.StatementExecutorType statementExecutorType)

Sets the executor type to use for connections. See StatementExecutorType for more
information on what the different options mean.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `statementExecutorType` | `com.google.cloud.spanner.connection.StatementExecutor.StatementExecutorType` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder` |   |

### setTracingPrefix(String tracingPrefix)

    public ConnectionOptions.Builder setTracingPrefix(String tracingPrefix)

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `tracingPrefix` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder` |   |

### setType(SpannerOptions.InstanceType instanceType)

    public ConnectionOptions.Builder setType(SpannerOptions.InstanceType instanceType)

Specifies the type of Spanner instance to connect to (cloud or omni). Setting it to omni is
mandatory when connecting to a Spanner Omni instance.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `instanceType` | `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.SpannerOptions.InstanceType` |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder` |   |

### setUri(String uri)

    public ConnectionOptions.Builder setUri(String uri)

Sets the URI of the Cloud Spanner database to connect to. A connection URI must be specified
in this format:

cloudspanner:\[//host\[:port\]\]/projects/project-id\[/instances/instance-id\[/databases/database-name\]\]\[?property-name=property-value\[;property-name=property-value\]\*\]?

The property-value strings should be url-encoded.

The project-id part of the URI may be filled with the placeholder DEFAULT_PROJECT_ID. This
placeholder will be replaced by the default project id of the environment that is requesting
a connection.

The supported properties are:

- credentials (String): URL for the credentials file to use for the connection. This property is only used if no credentials have been specified using the [ConnectionOptions.Builder#setCredentialsUrl(String)](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder#com_google_cloud_spanner_connection_ConnectionOptions_Builder_setCredentialsUrl_java_lang_String_) method. If you do not specify any credentials at all, the default credentials of the environment as returned by GoogleCredentials#getApplicationDefault() will be used.
- encodedCredentials (String): A Base64 encoded string containing the Google credentials to use. You should only set either this property or the `credentials` (file location) property, but not both at the same time.
- credentialsProvider (String): Class name of the com.google.api.gax.core.CredentialsProvider that should be used to get credentials for a connection that is created by this [ConnectionOptions](https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions). The credentials will be retrieved from the com.google.api.gax.core.CredentialsProvider when a new connection is created. A connection will use the credentials that were obtained at creation during its lifetime.
- autocommit (boolean): Sets the initial autocommit mode for the connection. Default is true.
- readonly (boolean): Sets the initial readonly mode for the connection. Default is false.
- minSessions (int): Sets the minimum number of sessions in the backing session pool.
- maxSessions (int): Sets the maximum number of sessions in the backing session pool.
- numChannels (int): Sets the number of gRPC channels to use for the connection.
- retryAbortsInternally (boolean): Sets the initial retryAbortsInternally mode for the connection. Default is true.
- optimizerVersion (string): Sets the query optimizer version to use for the connection.
- autoConfigEmulator (boolean): Automatically configures the connection to connect to the Cloud Spanner emulator. If no host and port is specified in the connection string, the connection will automatically use the default emulator host/port combination (localhost:9010). Plain text communication will be enabled and authentication will be disabled. The instance and database in the connection string will automatically be created on the emulator if any of them do not yet exist. Any existing instance or database on the emulator will remain untouched. No other configuration is needed in order to connect to the emulator than setting this property.
- routeToLeader (boolean): Sets the routeToLeader flag to route requests to leader (true) or any region (false) in read/write transactions and Partitioned DML. Default is true.

| **Parameter** ||
|---|---|
| **Name** | **Description** |
| `uri` | `https://docs.oracle.com/javase/8/docs/api/java/lang/String.html` The URI of the Spanner database to connect to. |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.cloud.google.com/java/docs/reference/google-cloud-spanner/latest/com.google.cloud.spanner.connection.ConnectionOptions.Builder` | this builder |