2014-09-21 83 views
0

我想用Spring4 Java配置配置HinkariCP数据源。 我的配置是这样的:配置HikariCP + Spring4 +休眠

@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 

有人能帮助我与Spring4,Hibernate和MySQL的 技术用于配置HikariCP:Java的8,春季4.1.0 .RELEASE,Hibernate 4.3.6.Final,HikariCP 2.0.1

回答

0

您打电话给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) 

jpaProperties来初始化HikariCP本身(忽略您设置明确的数据源)。连接提供商期望已将HikariCP属性设置为hibernate.properties,如文档here所述。

顺便说一句,Hibernate 4.3.6现在包含它自己的HikariCP ConnectionProvider,所以如果你使用它,它应该优先于由HikariCP提供的。

0

试评行

//hikariConfig.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); 

,并添加

hikariConfig.setDriverClassName("com.mysql.jdbc.Driver"); 
0

我已经发布了一个答案,如何配置HikariCPSpring Boot Start JPA另外一个问题,如果你是依靠application.propertiesSpring Boot要为您自动配置所有内容,则需要在application.properties中指定您的关键值,如下所示

# Spring data source needed for Spring boot to behave 
# Pre Spring Boot v2.0.0.M6 without below Spring Boot defaults to tomcat-jdbc connection pool included 
# in spring-boot-starter-jdbc and as compiled dependency under spring-boot-starter-data-jpa 
spring.datasource.type=com.zaxxer.hikari.HikariDataSource 
spring.datasource.url=jdbc:postgresql://localhost:5432/somedb 
spring.datasource.username=dbuser 
spring.datasource.password=dbpassword 

# Hikari will use the above plus the following to setup connection pooling 
spring.datasource.hikari.minimumIdle=5 
spring.datasource.hikari.maximumPoolSize=20 
spring.datasource.hikari.idleTimeout=30000 
spring.datasource.hikari.poolName=SpringBootJPAHikariCP 
spring.datasource.hikari.maxLifetime=2000000 
spring.datasource.hikari.connectionTimeout=30000 

# Without below HikariCP uses deprecated com.zaxxer.hikari.hibernate.HikariConnectionProvider 
# Surprisingly enough below ConnectionProvider is in hibernate-hikaricp dependency and not hibernate-core 
# So you need to pull that dependency but, make sure to exclude it's transitive dependencies or you will end up 
# with different versions of hibernate-core 
spring.jpa.hibernate.connection.provider_class=org.hibernate.hikaricp.internal.HikariCPConnectionProvider 

# JPA specific configs 
spring.jpa.properties.hibernate.show_sql=true 
spring.jpa.properties.hibernate.format_sql=true 
spring.jpa.properties.hibernate.use_sql=true 
spring.jpa.properties.hibernate.id.new_generator_mappings=false 
spring.jpa.properties.hibernate.default_schema=dbschema 
spring.jpa.properties.hibernate.search.autoregister_listeners=false 
spring.jpa.properties.hibernate.bytecode.use_reflection_optimizer=false 

查看如何命名属性键/值。有关这些属性的详细信息以及如何设置您的依赖关系,请参阅我对此SO question的回答。