我正在使用:
HikariCP 页面有两篇关于MySQL和Hibernate的有趣文档/博客
之后阅读以下关于MySQL的教程:
我有关于DataSource的以下配置:
Alpha (最佳推荐和首次尝试)
@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;
}
之后阅读以下有关Hibernate的教程:
我有以下配置:
@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;
}
观察我正在使用:
hibernateProperties.setProperty("hibernate.connection.provider_class","com.zaxxer.hikari.hibernate.HikariConnectionProvider");
但我总是收到:
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
即使 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;
}
我收到相同的错误消息:
我确实意识到我是否发表评论
hibernateProperties.setProperty("hibernate.connection.provider_class","com.zaxxer.hikari.hibernate.HikariConnectionProvider");
我有没有错误。为什么会出现这种情况?
我认为我的配置正确,因为第二个链接说:
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
这就是我所拥有的......
我不想直接在hibernate.properties中包含HikariCP配置属性,第二个链接也是如此。
答案 0 :(得分:4)
您可以使用2个选项配置DataSource
以便与hibernate一起使用,或者在spring中完全配置DataSource
并将其注入dataSource
的{{1}}属性或您使用hibernate属性来配置LocalSessionFactoryBean
,在这种情况下,您必须设置DataSource
。