gpt4 book ai didi

mysql - Hibernate 和 MySql 的 Hikaricp : one of either dataSource or dataSourceClassName must be specified

转载 作者:行者123 更新时间:2023-11-29 04:40:56 24 4
gpt4 key购买 nike

我正在与:

  • Spring 4.1.4.RELEASE
  • hibernate 4.3.8.FINAL
  • HikariCP 2.3.2
  • MySQL 5.6.22 自制软件

HikariCP 页面有两个关于 MySQL 和 Hibernate 的有趣文档/博客

阅读以下有关 MySQL 的教程后:

我有以下关于数据源的配置:

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 配置属性,第二个链接也提供了这种方式。

最佳答案

您有 2 个选项来配置 DataSource 以与 hibernate 一起使用,或者您在 spring 中完全配置 DataSource 并将其注入(inject) dataSource LocalSessionFactoryBean 的属性或您使用 hibernate 属性配置 DataSource 在这种情况下您必须设置 hibernate.connection.provider_class

关于mysql - Hibernate 和 MySql 的 Hikaricp : one of either dataSource or dataSourceClassName must be specified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28661742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com