dimanche 22 février 2015

Hikaricp with Hibernate and MySql: one of either dataSource or dataSourceClassName must be specified

I am working with:



  • Spring 4.1.4.RELEASE

  • Hibernate 4.3.8.FINAL

  • HikariCP 2.3.2

  • MySQL 5.6.22 Homebrew


HikariCP page has two interesting documentation/blog about MySQL and Hibernate


After to read the following tutorial about MySQL:



I have the following configuration about the DataSource:


Alpha (the best recommended and first try)



@Bean(name="dataSource", destroyMethod="close")
public DataSource dataSourceDevelopment() throws Exception{

HikariConfig hc = new HikariConfig();
hc.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
//hc.setDriverClassName("com.mysql.jdbc.Driver");
//hc.setJdbcUrl("jdbc:mysql://localhost:3306/manolodb_01");
hc.setUsername("user");
hc.setPassword("password");
hc.setPoolName("hikaricp-manolodb_01-pool");
hc.addDataSourceProperty("databaseName", "manolodb_01");
hc.addDataSourceProperty("cachePrepStmts", "true");
hc.addDataSourceProperty("prepStmtCacheSize", "250");
hc.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hc.addDataSourceProperty("useServerPrepStmts", "true");

HikariDataSource hds = new HikariDataSource(hc);
return hds;
}


After to read the following tutorial about Hibernate:



I have the following configuration:



@Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource){

LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();

localSessionFactoryBean.setDataSource(dataSource);
localSessionFactoryBean.setPackagesToScan("com.manuel.jordan.domain");

Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", environment.getRequiredProperty("database.hibernate.dialect", String.class));
hibernateProperties.setProperty("hibernate.connection.provider_class","com.zaxxer.hikari.hibernate.HikariConnectionProvider");
hibernateProperties.setProperty("hibernate.cache.provider_class","org.hibernate.cache.NoCacheProvider");
hibernateProperties.setProperty("hibernate.show_sql","true");
hibernateProperties.setProperty("hibernate.format_sql","true");
hibernateProperties.setProperty("hibernate.use_sql_comments","true");
hibernateProperties.setProperty("hibernate.max_fetch_depth","30");
hibernateProperties.setProperty("hibernate.default_batch_fetch_size","30");
hibernateProperties.setProperty("hibernate.jdbc.batch_size","30");//N + 1
hibernateProperties.setProperty("hibernate.order_updates", "true");

hibernateProperties.setProperty("org.hibernate.SQL","true");
hibernateProperties.setProperty("org.hibernate.type","true");


localSessionFactoryBean.setHibernateProperties(hibernateProperties);

return localSessionFactoryBean;

}


observe I am using: hibernateProperties.setProperty("hibernate.connection.provider_class","com.zaxxer.hikari.hibernate.HikariConnectionProvider");


But I always receive:



Caused by: java.lang.IllegalArgumentException: one of either dataSource or dataSourceClassName must be specified
at com.zaxxer.hikari.AbstractHikariConfig.validate(AbstractHikariConfig.java:747)
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:73)
at com.zaxxer.hikari.hibernate.HikariConnectionProvider.configure(HikariConnectionProvider.java:80)
... 54 more


Even with Beta



@Bean(name="dataSource", destroyMethod="close")
public DataSource dataSourceDevelopment() throws Exception{

HikariConfig hc = new HikariConfig();
//hc.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
hc.setDriverClassName("com.mysql.jdbc.Driver");
hc.setJdbcUrl("jdbc:mysql://localhost:3306/manolodb_01");
hc.setUsername("user");
hc.setPassword("password");
hc.setPoolName("hikaricp-manolodb_01-pool");
hc.addDataSourceProperty("databaseName", "manolodb_01");
hc.addDataSourceProperty("cachePrepStmts", "true");
hc.addDataSourceProperty("prepStmtCacheSize", "250");
hc.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
hc.addDataSourceProperty("useServerPrepStmts", "true");

HikariDataSource hds = new HikariDataSource(hc);
return hds;
}


I get the same error message:


I did realize if I comment



  • hibernateProperties.setProperty("hibernate.connection.provider_class","com.zaxxer.hikari.hibernate.HikariConnectionProvider");


I have no errors. Why this behavior?


I think my configuration is correct because the second link says:



In order to use the HikariConnectionProvider in Hibernate 4.x add the
following property to your hibernate.properties configuration file:

hibernate.connection.provider_class=com.zaxxer.hikari.hibernate.HikariConnectionProvider


And that is what I have…


I don't want include HikariCP configuration properties directly in the hibernate.properties how the second link offers too.


Aucun commentaire:

Enregistrer un commentaire