Search in sources :

Example 11 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.

the class JdbcDriver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    if (url != null && url.startsWith("jdbc:cloudspanner")) {
        try {
            Matcher matcher = URL_PATTERN.matcher(url);
            if (matcher.matches()) {
                // strip 'jdbc:' from the URL, add any extra properties and pass on to the generic
                // Connection API
                String connectionUri = appendPropertiesToUrl(url.substring(5), info);
                ConnectionOptions options = ConnectionOptions.newBuilder().setUri(connectionUri).build();
                JdbcConnection connection = new JdbcConnection(url, options);
                if (options.getWarnings() != null) {
                    connection.pushWarning(new SQLWarning(options.getWarnings()));
                }
                return connection;
            }
        } catch (SpannerException e) {
            throw JdbcSqlExceptionFactory.of(e);
        } catch (IllegalArgumentException e) {
            throw JdbcSqlExceptionFactory.of(e.getMessage(), Code.INVALID_ARGUMENT, e);
        } catch (Exception e) {
            throw JdbcSqlExceptionFactory.of(e.getMessage(), Code.UNKNOWN, e);
        }
        throw JdbcSqlExceptionFactory.of("invalid url: " + url, Code.INVALID_ARGUMENT);
    }
    return null;
}
Also used : SQLWarning(java.sql.SQLWarning) Matcher(java.util.regex.Matcher) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) SpannerException(com.google.cloud.spanner.SpannerException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SpannerException(com.google.cloud.spanner.SpannerException) SQLException(java.sql.SQLException)

Example 12 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions in project pgadapter by GoogleCloudPlatform.

the class ConnectionHandler method connectToSpanner.

@InternalApi
public void connectToSpanner(String database) {
    OptionsMetadata options = getServer().getOptions();
    String uri = options.hasDefaultConnectionUrl() ? options.getDefaultConnectionUrl() : options.buildConnectionURL(database);
    if (uri.startsWith("jdbc:")) {
        uri = uri.substring("jdbc:".length());
    }
    uri = appendPropertiesToUrl(uri, getServer().getProperties());
    if (System.getProperty(CHANNEL_PROVIDER_PROPERTY) != null) {
        uri = uri + ";" + ConnectionOptions.CHANNEL_PROVIDER_PROPERTY_NAME + "=" + System.getProperty(CHANNEL_PROVIDER_PROPERTY);
        // This forces the connection to use NoCredentials.
        uri = uri + ";usePlainText=true";
        try {
            Class.forName(System.getProperty(CHANNEL_PROVIDER_PROPERTY));
        } catch (ClassNotFoundException e) {
            throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Unknown or invalid channel provider: " + System.getProperty(CHANNEL_PROVIDER_PROPERTY));
        }
    }
    ConnectionOptions connectionOptions = ConnectionOptions.newBuilder().setUri(uri).build();
    Connection spannerConnection = connectionOptions.getConnection();
    try {
        // invalid, for example as a result of the credentials being wrong.
        if (spannerConnection.getDialect() != Dialect.POSTGRESQL) {
            throw SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, String.format("The database uses dialect %s. Currently PGAdapter only supports connections to PostgreSQL dialect databases. " + "These can be created using https://cloud.google.com/spanner/docs/quickstart-console#postgresql", spannerConnection.getDialect()));
        }
    } catch (SpannerException e) {
        spannerConnection.close();
        throw e;
    }
    this.spannerConnection = spannerConnection;
}
Also used : Connection(com.google.cloud.spanner.connection.Connection) OptionsMetadata(com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) SpannerException(com.google.cloud.spanner.SpannerException) InternalApi(com.google.api.core.InternalApi)

Example 13 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.

the class JdbcConnectionTest method testHoldability.

@Test
public void testHoldability() throws SQLException {
    ConnectionOptions options = mockOptions();
    try (JdbcConnection connection = createConnection(options)) {
        assertThat(connection.getHoldability()).isEqualTo(ResultSet.CLOSE_CURSORS_AT_COMMIT);
        // assert that setting it to this value is ok.
        connection.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT);
        // assert that setting it to something else is not ok.
        int[] settings = new int[] { ResultSet.HOLD_CURSORS_OVER_COMMIT, -100 };
        for (int setting : settings) {
            boolean exception = false;
            try {
                connection.setHoldability(setting);
            } catch (SQLException e) {
                if (setting == -100) {
                    exception = (e instanceof JdbcSqlException && ((JdbcSqlException) e).getCode() == Code.INVALID_ARGUMENT);
                } else {
                    exception = (e instanceof JdbcSqlException && ((JdbcSqlException) e).getCode() == Code.UNIMPLEMENTED);
                }
            }
            assertThat(exception).isTrue();
        }
    }
}
Also used : SQLException(java.sql.SQLException) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) Savepoint(java.sql.Savepoint) ConnectionImplTest(com.google.cloud.spanner.connection.ConnectionImplTest) AbstractConnectionImplTest(com.google.cloud.spanner.connection.AbstractConnectionImplTest) Test(org.junit.Test)

Example 14 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.

the class JdbcConnectionTest method testReadOnly.

@Test
public void testReadOnly() {
    ConnectionOptions options = mockOptions();
    when(options.isAutocommit()).thenReturn(true);
    when(options.isReadOnly()).thenReturn(true);
    try (Connection connection = createConnection(options)) {
        assertThat(connection.isReadOnly()).isTrue();
        connection.setReadOnly(false);
        assertThat(connection.isReadOnly()).isFalse();
        // start a transaction
        connection.createStatement().execute("begin transaction");
        // setting readonly should no longer be allowed
        connection.setReadOnly(true);
        fail("missing expected exception");
    } catch (SQLException e) {
        assertThat(JdbcExceptionMatcher.matchCode(Code.FAILED_PRECONDITION).matches(e)).isTrue();
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) ConnectionImplTest(com.google.cloud.spanner.connection.ConnectionImplTest) AbstractConnectionImplTest(com.google.cloud.spanner.connection.AbstractConnectionImplTest) Test(org.junit.Test)

Example 15 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.

the class JdbcConnectionTest method getDefaultClientInfo.

@Test
public void getDefaultClientInfo() throws SQLException {
    ConnectionOptions options = mockOptions();
    try (JdbcConnection connection = createConnection(options)) {
        Properties defaultProperties = connection.getClientInfo();
        assertThat(defaultProperties.stringPropertyNames()).containsExactly("APPLICATIONNAME", "CLIENTHOSTNAME", "CLIENTUSER");
    }
}
Also used : ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) Properties(java.util.Properties) ConnectionImplTest(com.google.cloud.spanner.connection.ConnectionImplTest) AbstractConnectionImplTest(com.google.cloud.spanner.connection.AbstractConnectionImplTest) Test(org.junit.Test)

Aggregations

ConnectionOptions (com.google.cloud.spanner.connection.ConnectionOptions)19 Test (org.junit.Test)16 AbstractConnectionImplTest (com.google.cloud.spanner.connection.AbstractConnectionImplTest)15 ConnectionImplTest (com.google.cloud.spanner.connection.ConnectionImplTest)15 SQLException (java.sql.SQLException)7 Properties (java.util.Properties)3 SpannerException (com.google.cloud.spanner.SpannerException)2 JdbcSqlExceptionImpl (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLWarning (java.sql.SQLWarning)2 Savepoint (java.sql.Savepoint)2 InternalApi (com.google.api.core.InternalApi)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 Statement (com.google.cloud.spanner.Statement)1 Connection (com.google.cloud.spanner.connection.Connection)1 ConnectionOptionsTest (com.google.cloud.spanner.connection.ConnectionOptionsTest)1 OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 DatabaseMetaData (java.sql.DatabaseMetaData)1