问题描述
我想用Spring4 Java配置来配置HinkariCP数据源。
我的配置如下所示:
@Configuration
@EnableJpaRepositories(com.app.dao.repository )
@EnableTransactionManagement
public class DataAccessConfig {
private static final String PROPERTY_NAME_HIBERNATE_DIALECT =hibernate.dialect;
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL =hibernate.format_sql;
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL =hibernate.show_sql;
private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO =hibernate.hbm2ddl.auto;
private static final String PROPERTY_NAME_H_CONNECTION_PROVIDER =hibernate.connection.provider_class;
@Autowired
private环境env;
@Bean(destroyMethod =close)
public HikariDataSource dataSource(){
HikariDataSource ds = new HikariDataSource();
ds.setMaximumPoolSize(100);
ds.setDataSourceClassName(com.mysql.jdbc.jdbc2.optional.MysqlDataSource);
ds.addDataSourceProperty(url,jdbc:mysql://127.0.0.1:3306 / mydb?useUnicode = true& characterEncoding = UTF-8& transformedBitIsBoolean = true);
ds.addDataSourceProperty(user,usr);
ds.addDataSourceProperty(password,pwd);
ds.addDataSourceProperty(cachePrepStmts,true);
ds.addDataSourceProperty(prepStmtCacheSize,250);
ds.addDataSourceProperty(prepStmtCacheSqlLimit,2048);
ds.addDataSourceProperty(useServerPrepStmts,true);
return ds;
}
$Be
@Autowired
public PlatformTransactionManager transactionManager()throws ClassNotFoundException {
return new JpaTransactionManager(entityManagerFactory()。getObject());
public LocalContainerEntityManagerFactoryBean entityManagerFactory()抛出ClassNotFoundException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan(com.app.dao.entity);
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
entityManagerFactoryBean.setJpaDialect(new FlushModeCommitHibernateJpaDialect(FlushMode.COMMIT));
属性jpaProperties = new Properties();
jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT,
env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL,
env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL,
env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO,
env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO));
jpaProperties.put(PROPERTY_NAME_H_CONNECTION_PROVIDER,
env.getRequiredProperty(PROPERTY_NAME_H_CONNECTION_PROVIDER));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
entityManagerFactoryBean.afterPropertiesSet();
返回entityManagerFactoryBean;
}
@Bean
public SharedEntityManagerBean sharedEntityManager()throws ClassNotFoundException {
SharedEntityManagerBean sharedEntityManagerBean = new SharedEntityManagerBean();
sharedEntityManagerBean.setEntityManagerFactory(entityManagerFactory()。getObject());
返回新的SharedEntityManagerBean();
@Bean
public JpaVendorAdapter jpaVendorAdapter(){
AbstractJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabasePlatform(env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaVendorAdapter.setShowSql(env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL,Boolean.class));
返回jpaVendorAdapter;
}
但我收到了一个异常:
引发:java.lang.IllegalArgumentException:必须在com.zaxxer.hikari.HikariConfig.validate中指定dataSource或dataSourceClassName之一
(HikariConfig.java :683)
at com.zaxxer.hikari.HikariDataSource。< init>(HikariDataSource.java:75)
at com.zaxxer.hikari.hibernate.HikariConnectionProvider.configure(HikariConnectionProvider.java:80)
... 86 more
有人可以帮助我用Spring4配置HikariCP,Hibernate和MySql
使用的技术:Java 8,Spring 4.1.0.RELEASE,Hibernate 4.3.6.Final,HikariCP 2.0.1
你调用 entityManagerFactoryBean.setDataSource(dataSource())
的事实应该意味着你不需要调用 jpaProperties.put( PROPERTY_NAME_H_CONNECTION_PROVIDER,env.getRequiredProperty(PROPERTY_NAME_H_CONNECTION_PROVIDER))
。您正在混合两种类型的初始化。源自以下内容的堆栈跟踪:
com.zaxxer.hikari.hibernate.HikariConnectionProvider.configure(HikariConnectionProvider.java:80)
来自 顺便提一下,Hibernate 4.3.6现在包含它自己的HikariCP ConnectionProvider,所以如果你使用它,它应该优先于HikariCP提供的。 I want to configure HinkariCP data source with Spring4 Java config.My config looks like: but i get an exception: Can someone help my to configure HikariCP with Spring4, Hibernate and MySqlTech used: Java 8, Spring 4.1.0.RELEASE, Hibernate 4.3.6.Final, HikariCP 2.0.1 The fact that you are calling is coming from the By the way, Hibernate 4.3.6 now includes it own HikariCP ConnectionProvider, so if you use that it should be in preference to the one provided by HikariCP. 这篇关于配置HikariCP + Spring4 + Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! jpaProperties
初始化HikariCP本身(忽略您明确设置的DataSource)。连接提供程序期望HikariCP属性已在 hibernate.properties
中设置,如文档
@Configuration
@EnableJpaRepositories("com.app.dao.repository")
@EnableTransactionManagement
public class DataAccessConfig {
private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
private static final String PROPERTY_NAME_HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
private static final String PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
private static final String PROPERTY_NAME_H_CONNECTION_PROVIDER = "hibernate.connection.provider_class";
@Autowired
private Environment env;
@Bean(destroyMethod = "close")
public HikariDataSource dataSource() {
HikariDataSource ds = new HikariDataSource();
ds.setMaximumPoolSize(100);
ds.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
ds.addDataSourceProperty("url", "jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8&transformedBitIsBoolean=true");
ds.addDataSourceProperty("user", "usr");
ds.addDataSourceProperty("password", "pwd");
ds.addDataSourceProperty("cachePrepStmts", true);
ds.addDataSourceProperty("prepStmtCacheSize", 250);
ds.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
ds.addDataSourceProperty("useServerPrepStmts", true);
return ds;
}
@Bean
@Autowired
public PlatformTransactionManager transactionManager() throws ClassNotFoundException {
return new JpaTransactionManager(entityManagerFactory().getObject());
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws ClassNotFoundException {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource());
entityManagerFactoryBean.setPackagesToScan("com.app.dao.entity");
entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter());
entityManagerFactoryBean.setJpaDialect(new FlushModeCommitHibernateJpaDialect(FlushMode.COMMIT));
Properties jpaProperties = new Properties();
jpaProperties.put(PROPERTY_NAME_HIBERNATE_DIALECT,
env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_FORMAT_SQL,
env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_FORMAT_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL,
env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
jpaProperties.put(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO,
env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_HBM2DDL_AUTO));
jpaProperties.put(PROPERTY_NAME_H_CONNECTION_PROVIDER,
env.getRequiredProperty(PROPERTY_NAME_H_CONNECTION_PROVIDER));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
entityManagerFactoryBean.afterPropertiesSet();
return entityManagerFactoryBean;
}
@Bean
public SharedEntityManagerBean sharedEntityManager() throws ClassNotFoundException {
SharedEntityManagerBean sharedEntityManagerBean = new SharedEntityManagerBean();
sharedEntityManagerBean.setEntityManagerFactory(entityManagerFactory().getObject());
return new SharedEntityManagerBean();
}
@Bean
public JpaVendorAdapter jpaVendorAdapter() {
AbstractJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setDatabasePlatform(env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
jpaVendorAdapter.setShowSql(env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL, Boolean.class));
return jpaVendorAdapter;
}
Caused by: java.lang.IllegalArgumentException: one of either dataSource or dataSourceClassName must be specified
at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:683)
at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:75)
at com.zaxxer.hikari.hibernate.HikariConnectionProvider.configure(HikariConnectionProvider.java:80)
... 86 more
entityManagerFactoryBean.setDataSource(dataSource())
should mean that you do not need to call jpaProperties.put(PROPERTY_NAME_H_CONNECTION_PROVIDER, env.getRequiredProperty(PROPERTY_NAME_H_CONNECTION_PROVIDER))
. You are mixing two styles of initialization. The stacktrace that originates with:com.zaxxer.hikari.hibernate.HikariConnectionProvider.configure(HikariConnectionProvider.java:80)
jpaProperties
initializing HikariCP itself (ignoring the DataSource that you set explicitly). The connection provider expects that HikariCP properties have been set in hibernate.properties
, as documented here.