Search in sources :

Example 1 with DatabaseManager

use of com.fredboat.backend.quarterdeck.db.DatabaseManager 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)

Aggregations

DatabaseManager (com.fredboat.backend.quarterdeck.db.DatabaseManager)1 Bean (org.springframework.context.annotation.Bean)1 JpaVendorAdapter (org.springframework.orm.jpa.JpaVendorAdapter)1 LocalContainerEntityManagerFactoryBean (org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean)1 HibernateJpaVendorAdapter (org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter)1 DatabaseConnection (space.npstr.sqlsauce.DatabaseConnection)1