/** * Extrait une DataSource d'une EntityManagerFactory */ public static DataSource applicationDataSource(EntityManagerFactory entityManagerFactory) { EntityManagerImpl entityManagerImpl = (EntityManagerImpl) entityManagerFactory.createEntityManager(); try { HikariCPConnectionProvider hikariCPConnectionProvider = (HikariCPConnectionProvider) entityManagerImpl.getFactory().getSessionFactory().getServiceRegistry().getService(ConnectionProvider.class); Field dataSourceField = HikariCPConnectionProvider.class.getDeclaredField("hds"); dataSourceField.setAccessible(true); return (HikariDataSource) dataSourceField.get(hikariCPConnectionProvider); } catch (NoSuchFieldException | IllegalAccessException e) { throw new RuntimeException(e); } finally { entityManagerImpl.close(); } }
@Override protected void appendDriverProperties(Properties properties) { DataSourceProvider dataSourceProvider = dataSourceProvider(); properties.put("hibernate.connection.provider_class", HikariCPConnectionProvider.class.getName()); properties.put("hibernate.hikari.minimumPoolSize", "1"); properties.put("hibernate.hikari.maximumPoolSize", "2"); properties.put("hibernate.hikari.transactionIsolation", "TRANSACTION_SERIALIZABLE"); properties.put("hibernate.hikari.dataSourceClassName", dataSourceProvider.dataSourceClassName().getName()); properties.put("hibernate.hikari.dataSource.url", dataSourceProvider.url()); properties.put("hibernate.hikari.dataSource.user", dataSourceProvider.username()); properties.put("hibernate.hikari.dataSource.password", dataSourceProvider.password()); }
@Override protected void appendDriverProperties(Properties properties) { DataSourceProvider dataSourceProvider = dataSourceProvider(); properties.put("hibernate.connection.provider_class", HikariCPConnectionProvider.class.getName()); properties.put("hibernate.hikari.maximumPoolSize", "5"); properties.put("hibernate.hikari.dataSourceClassName", dataSourceProvider.dataSourceClassName().getName()); properties.put("hibernate.hikari.dataSource.url", dataSourceProvider.url()); properties.put("hibernate.hikari.dataSource.user", dataSourceProvider.username()); properties.put("hibernate.hikari.dataSource.password", dataSourceProvider.password()); }