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