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());
}
}
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();
}
}
}
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();
}
}
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();
}
}
}
Aggregations