Search in sources :

Example 1 with DatabaseConnection

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;
}
Also used : Flyway(org.flywaydb.core.Flyway) DatabaseConnection(space.npstr.sqlsauce.DatabaseConnection)

Example 2 with 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;
}
Also used : DatabaseConnection(space.npstr.sqlsauce.DatabaseConnection) Nullable(javax.annotation.Nullable)

Example 3 with DatabaseConnection

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;
}
Also used : DatabaseConnection(space.npstr.sqlsauce.DatabaseConnection) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 4 with DatabaseConnection

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;
}
Also used : DatabaseManager(com.fredboat.backend.quarterdeck.db.DatabaseManager) JpaVendorAdapter(org.springframework.orm.jpa.JpaVendorAdapter) HibernateJpaVendorAdapter(org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) HibernateJpaVendorAdapter(org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter) DatabaseConnection(space.npstr.sqlsauce.DatabaseConnection) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean) Bean(org.springframework.context.annotation.Bean) LocalContainerEntityManagerFactoryBean(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)

Example 5 with DatabaseConnection

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;
}
Also used : DatabaseConnection(space.npstr.sqlsauce.DatabaseConnection)

Aggregations

DatabaseConnection (space.npstr.sqlsauce.DatabaseConnection)6 Flyway (org.flywaydb.core.Flyway)2 Bean (org.springframework.context.annotation.Bean)2 LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)2 DatabaseManager (com.fredboat.backend.quarterdeck.db.DatabaseManager)1 Nullable (javax.annotation.Nullable)1 Primary (org.springframework.context.annotation.Primary)1 JpaVendorAdapter (org.springframework.orm.jpa.JpaVendorAdapter)1 HibernateJpaVendorAdapter (org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter)1