0

I want to configure HinkariCP data source with Spring4 Java config. My config looks like:

@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;
}

but i get an exception:

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

Can someone help my to configure HikariCP with Spring4, Hibernate and MySql Tech used: Java 8, Spring 4.1.0.RELEASE, Hibernate 4.3.6.Final, HikariCP 2.0.1

4

3 に答える 3

1

あなたが電話しているという事実は、あなたが電話entityManagerFactoryBean.setDataSource(dataSource())する必要がないことを意味するはずですjpaProperties.put(PROPERTY_NAME_H_CONNECTION_PROVIDER, env.getRequiredProperty(PROPERTY_NAME_H_CONNECTION_PROVIDER))。2 つのスタイルの初期化を混在させています。以下から始まるスタックトレース:

com.zaxxer.hikari.hibernate.HikariConnectionProvider.configure(HikariConnectionProvider.java:80)

HikariCP 自体の初期化から来ていjpaPropertiesます (明示的に設定した DataSource を無視します)。hibernate.properties接続プロバイダーは、ここに記載されているように、HikariCP プロパティが に設定されていることを想定しています

ちなみに、Hibernate 4.3.6 には独自の HikariCP ConnectionProvider が含まれるようになったため、それを使用する場合は、HikariCP が提供するものより優先する必要があります。

于 2014-09-21T13:48:16.697 に答える