use of com.google.api.services.sqladmin.model.Operation in project core by z1lc.
the class MasterEtl method getSessionFactory.
private SessionFactory getSessionFactory(Hbm2ddlType hbm2ddlType, ConnectionType connectionType) throws HibernateException, IOException, GeneralSecurityException, InterruptedException {
try {
config.setProperty("hibernate.hbm2ddl.auto", hbm2ddlType.getText());
String username;
String password;
String instanceName;
if (connectionType.equals(ConnectionType.RSANEK)) {
username = secretProvider.getSecret(GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_USERNAME);
password = secretProvider.getSecret(GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_PASSWORD);
instanceName = "rsanek-db";
} else if (connectionType.equals(ConnectionType.CRONUS)) {
username = secretProvider.getSecret(GOOGLE_CLOUD_SQL_CRONUS_POSTGRES_USERNAME);
password = secretProvider.getSecret(GOOGLE_CLOUD_SQL_CRONUS_POSTGRES_USERNAME);
instanceName = "cronus-pg-primary";
} else {
throw new RuntimeException(String.format("Don't know configuration for connection type %s.", connectionType));
}
config.setProperty("hibernate.connection.username", username);
config.setProperty("hibernate.connection.password", password);
config.setProperty("connection.url", "jdbc:postgresql://google/postgres?socketFactory=com.google.cloud.sql.postgres.SocketFactory&cloudSqlInstance=arctic-rite-143002:us-west1:" + instanceName);
return config.buildSessionFactory();
} catch (ServiceException e) {
log.info("Failed to connect to Cloud SQL Instance. Assuming instance is stopped, attempting to start...");
String project = "arctic-rite-143002";
String instance = "rsanek-db";
DatabaseInstance requestBody = new DatabaseInstance();
requestBody.setSettings(new Settings().setActivationPolicy("ALWAYS"));
SQLAdmin sqlAdminService = createSqlAdminService();
SQLAdmin.Instances.Patch request = sqlAdminService.instances().patch(project, instance, requestBody);
Operation response = request.execute();
log.info(response.toPrettyString());
log.info("Starting instance successful. Waiting for %s seconds to let instance start.", INSTANCE_STARTUP_WAIT_TIME.toMinutes());
Thread.sleep(INSTANCE_STARTUP_WAIT_TIME.toMillis());
return config.buildSessionFactory();
}
}
Aggregations