use of space.npstr.sqlsauce.DatabaseConnection in project Backend by FredBoat.
the class DatabaseManager method initCacheConn.
public DatabaseConnection initCacheConn(String jdbc) throws DatabaseException {
Flyway flyway = null;
if (this.migrateAndValidate) {
flyway = buildFlyway("classpath:com/fredboat/backend/quarterdeck/db/migrations/cache");
}
DatabaseConnection databaseConnection = getBasicConnectionBuilder(CACHE_PERSISTENCE_UNIT_NAME, jdbc).setHibernateProps(buildHibernateProps("ehcache_cache.xml")).addEntityPackage("com.fredboat.backend.quarterdeck.db.entities.cache").setFlyway(flyway).build();
log.debug(CacheManager.getCacheManager("CACHE_CACHEMANAGER").getActiveConfigurationText());
return databaseConnection;
}
use of space.npstr.sqlsauce.DatabaseConnection in project Backend by FredBoat.
the class DatabaseManager method getCacheDbConn.
// may return null if no cache db is configured
@Nullable
public DatabaseConnection getCacheDbConn() {
if (this.cacheJdbc == null || this.cacheJdbc.isEmpty()) {
return null;
}
DatabaseConnection singleton = this.cacheDbConn;
if (singleton == null) {
synchronized (this.cacheDbConnInitLock) {
singleton = this.cacheDbConn;
if (singleton == null) {
this.cacheDbConn = singleton = initCacheConn(this.cacheJdbc);
this.cacheConnBuilt = true;
}
}
}
return singleton;
}
use of space.npstr.sqlsauce.DatabaseConnection in project Backend by FredBoat.
the class DatabaseConfiguration method mainDbConn.
@Primary
@Bean
public DatabaseConnection mainDbConn(DatabaseManager databaseManager) throws InterruptedException {
// attempt to connect to the database a few times
// this is relevant in a dockerized environment because after a reboot there is no guarantee that the db
// container will be started before the fredboat one
int dbConnectionAttempts = 0;
DatabaseConnection mainDbConn = null;
while ((mainDbConn == null || !mainDbConn.isAvailable()) && dbConnectionAttempts++ < 10) {
try {
if (mainDbConn != null) {
mainDbConn.shutdown();
}
mainDbConn = databaseManager.getMainDbConn();
} catch (Exception e) {
log.info("Could not connect to the database. Retrying in a moment...", e);
Thread.sleep(6000);
}
}
if (mainDbConn == null || !mainDbConn.isAvailable()) {
String message = "Could not establish database connection. Exiting...";
log.error(message);
System.exit(1);
}
return mainDbConn;
}
use of space.npstr.sqlsauce.DatabaseConnection in project Backend by FredBoat.
the class DatabaseConfiguration method databaseManager.
@Bean
public DatabaseManager databaseManager(HibernateStatisticsCollector hibernateStats, PrometheusMetricsTrackerFactory hikariStats) {
DatabaseManager databaseManager = new DatabaseManager(hibernateStats, hikariStats, this.dbConf.getHikariPoolSize(), "Quarterdeck", true, this.dbConf.getMainJdbcUrl(), this.dbConf.getCacheJdbcUrl(), (puName, dataSource, properties, entityPackages) -> {
LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource);
emfb.setPackagesToScan(entityPackages.toArray(new String[entityPackages.size()]));
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
emfb.setJpaVendorAdapter(vendorAdapter);
emfb.setJpaProperties(properties);
// initiate creation of the native emf
emfb.afterPropertiesSet();
return emfb.getNativeEntityManagerFactory();
});
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (databaseManager.isCacheConnBuilt()) {
DatabaseConnection cacheDbConn = databaseManager.getCacheDbConn();
if (cacheDbConn != null) {
cacheDbConn.shutdown();
}
}
if (databaseManager.isMainConnBuilt()) {
databaseManager.getMainDbConn().shutdown();
}
}, "databasemanager-shutdown-hook"));
return databaseManager;
}
use of space.npstr.sqlsauce.DatabaseConnection in project Backend by FredBoat.
the class DatabaseManager method getMainDbConn.
public DatabaseConnection getMainDbConn() {
DatabaseConnection singleton = this.mainDbConn;
if (singleton == null) {
synchronized (this.mainDbConnInitLock) {
singleton = this.mainDbConn;
if (singleton == null) {
this.mainDbConn = singleton = initMainDbConn();
this.mainConnBuilt = true;
}
}
}
return singleton;
}
Aggregations