Search in sources :

Example 1 with DatabaseNotFoundException

use of com.google.cloud.spanner.DatabaseNotFoundException in project datarouter by hotpads.

the class SpannerDatabaseCreator method hasTables.

private boolean hasTables(Spanner spanner, DatabaseId databaseId) {
    // use a throw-away client as it won't be able to see the database after it's created
    DatabaseClient databaseClient = spanner.getDatabaseClient(databaseId);
    Statement anyOperation = Statement.of(tableOperationsGenerator.getListOfTables());
    try {
        // this sometimes throws DatabaseNotFoundException, but it seems not always, so check if there's a table
        ReadOnlyTransaction txn = databaseClient.singleUseReadOnlyTransaction();
        try (ResultSet resultSet = txn.executeQuery(anyOperation)) {
            return resultSet.next();
        }
    } catch (DatabaseNotFoundException dnfe) {
        return false;
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) Statement(com.google.cloud.spanner.Statement) DatabaseNotFoundException(com.google.cloud.spanner.DatabaseNotFoundException) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet)

Example 2 with DatabaseNotFoundException

use of com.google.cloud.spanner.DatabaseNotFoundException in project java-spanner by googleapis.

the class ITDatabaseTest method databaseDeletedTest.

@Test
public void databaseDeletedTest() throws Exception {
    // Create a test db, do a query, then delete it and verify that it returns
    // DatabaseNotFoundExceptions.
    Database db = env.getTestHelper().createTestDatabase();
    DatabaseClient client = env.getTestHelper().getClient().getDatabaseClient(db.getId());
    try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
        assertThat(rs.next()).isTrue();
        assertThat(rs.getLong(0)).isEqualTo(1L);
        assertThat(rs.next()).isFalse();
    }
    // Delete the database.
    db.drop();
    // We need to wait a little before Spanner actually starts sending DatabaseNotFound errors.
    ExponentialBackOff backoff = new ExponentialBackOff.Builder().setInitialIntervalMillis(1000).setMaxElapsedTimeMillis(65000).setMaxIntervalMillis(5000).build();
    DatabaseNotFoundException notFoundException = null;
    long millis = 0L;
    while ((millis = backoff.nextBackOffMillis()) != ExponentialBackOff.STOP) {
        Thread.sleep(millis);
        // Queries to this database should eventually return DatabaseNotFoundExceptions.
        try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
            rs.next();
        } catch (DatabaseNotFoundException e) {
            // This is what we expect.
            notFoundException = e;
            break;
        }
    }
    assertThat(notFoundException).isNotNull();
    // Now re-create a database with the same name.
    OperationFuture<Database, CreateDatabaseMetadata> op = env.getTestHelper().getClient().getDatabaseAdminClient().createDatabase(db.getId().getInstanceId().getInstance(), db.getId().getDatabase(), Collections.emptyList());
    Database newDb = op.get();
    // Queries using the same DatabaseClient should still return DatabaseNotFoundExceptions.
    try (ResultSet rs = client.singleUse().executeQuery(Statement.of("SELECT 1"))) {
        rs.next();
        fail("Missing expected DatabaseNotFoundException");
    } catch (DatabaseNotFoundException e) {
    // This is what we expect.
    }
    // Now get a new DatabaseClient for the database. This should now result in a valid
    // DatabaseClient.
    DatabaseClient newClient = env.getTestHelper().getClient().getDatabaseClient(newDb.getId());
    try (ResultSet rs = newClient.singleUse().executeQuery(Statement.of("SELECT 1"))) {
        assertThat(rs.next()).isTrue();
        assertThat(rs.getLong(0)).isEqualTo(1L);
        assertThat(rs.next()).isFalse();
    }
}
Also used : DatabaseClient(com.google.cloud.spanner.DatabaseClient) DatabaseNotFoundException(com.google.cloud.spanner.DatabaseNotFoundException) Database(com.google.cloud.spanner.Database) ResultSet(com.google.cloud.spanner.ResultSet) CreateDatabaseMetadata(com.google.spanner.admin.database.v1.CreateDatabaseMetadata) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff) ParallelIntegrationTest(com.google.cloud.spanner.ParallelIntegrationTest) Test(org.junit.Test)

Aggregations

DatabaseClient (com.google.cloud.spanner.DatabaseClient)2 DatabaseNotFoundException (com.google.cloud.spanner.DatabaseNotFoundException)2 ResultSet (com.google.cloud.spanner.ResultSet)2 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)1 Database (com.google.cloud.spanner.Database)1 ParallelIntegrationTest (com.google.cloud.spanner.ParallelIntegrationTest)1 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)1 Statement (com.google.cloud.spanner.Statement)1 CreateDatabaseMetadata (com.google.spanner.admin.database.v1.CreateDatabaseMetadata)1 Test (org.junit.Test)1