Search in sources :

Example 16 with ConnectionOptions

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

the class JdbcConnectionTest method testSetReturnCommitStats_throwsSqlException.

@Test
public void testSetReturnCommitStats_throwsSqlException() {
    ConnectionOptions options = mockOptions();
    com.google.cloud.spanner.connection.Connection spannerConnection = mock(com.google.cloud.spanner.connection.Connection.class);
    when(options.getConnection()).thenReturn(spannerConnection);
    Mockito.doThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, "test exception")).when(spannerConnection).setReturnCommitStats(any(boolean.class));
    try (JdbcConnection connection = new JdbcConnection("jdbc:cloudspanner://localhost/projects/project/instances/instance/databases/database;credentialsUrl=url", options)) {
        connection.setReturnCommitStats(true);
        fail("missing expected exception");
    } catch (SQLException e) {
        assertTrue(e instanceof JdbcSqlException);
        assertEquals(Code.FAILED_PRECONDITION, ((JdbcSqlException) e).getCode());
    }
}
Also used : SQLException(java.sql.SQLException) 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 17 with ConnectionOptions

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

the class JdbcConnectionTest method testTransactionIsolation.

@Test
public void testTransactionIsolation() throws SQLException {
    ConnectionOptions options = mockOptions();
    try (JdbcConnection connection = createConnection(options)) {
        assertThat(connection.getTransactionIsolation()).isEqualTo(Connection.TRANSACTION_SERIALIZABLE);
        // assert that setting it to this value is ok.
        connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        // assert that setting it to something else is not ok.
        int[] settings = new int[] { Connection.TRANSACTION_READ_COMMITTED, Connection.TRANSACTION_READ_UNCOMMITTED, Connection.TRANSACTION_REPEATABLE_READ, -100 };
        for (int setting : settings) {
            boolean exception = false;
            try {
                connection.setTransactionIsolation(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 18 with ConnectionOptions

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

the class JdbcConnectionTest method testIsValid.

@Test
public void testIsValid() throws SQLException {
    // Setup.
    ConnectionOptions options = mockOptions();
    com.google.cloud.spanner.connection.Connection spannerConnection = mock(com.google.cloud.spanner.connection.Connection.class);
    when(spannerConnection.getDialect()).thenReturn(dialect);
    when(options.getConnection()).thenReturn(spannerConnection);
    Statement statement = Statement.of(JdbcConnection.IS_VALID_QUERY);
    // Verify that an opened connection that returns a result set is valid.
    try (JdbcConnection connection = new JdbcConnection("url", options)) {
        when(spannerConnection.executeQuery(statement)).thenReturn(createSelect1ResultSet());
        assertThat(connection.isValid(1)).isTrue();
        try {
            // Invalid timeout value.
            connection.isValid(-1);
            fail("missing expected exception");
        } catch (JdbcSqlExceptionImpl e) {
            assertThat(e.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
        }
        // Now let the query return an error. isValid should now return false.
        when(spannerConnection.executeQuery(statement)).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, "the current transaction has been aborted"));
        assertThat(connection.isValid(1)).isFalse();
    }
}
Also used : JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) PreparedStatement(java.sql.PreparedStatement) Statement(com.google.cloud.spanner.Statement) 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 19 with ConnectionOptions

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

the class JdbcConnectionTest method testRollback.

@Test
public void testRollback() throws SQLException {
    ConnectionOptions options = mockOptions();
    try (JdbcConnection connection = createConnection(options)) {
        // verify that there is no transaction started
        assertThat(connection.getSpannerConnection().isTransactionStarted()).isFalse();
        // start a transaction
        connection.createStatement().execute(AbstractConnectionImplTest.SELECT);
        // verify that we did start a transaction
        assertThat(connection.getSpannerConnection().isTransactionStarted()).isTrue();
        // do a rollback
        connection.rollback();
        // verify that there is no transaction started anymore
        assertThat(connection.getSpannerConnection().isTransactionStarted()).isFalse();
        // verify that there is no commit timestamp
        String showCommitTimestamp = dialect == Dialect.POSTGRESQL ? "show spanner.commit_timestamp" : "show variable commit_timestamp";
        try (ResultSet rs = connection.createStatement().executeQuery(showCommitTimestamp)) {
            assertThat(rs.next()).isTrue();
            assertThat(rs.getTimestamp(1)).isNull();
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) 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)

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