1

请告诉我。如何配置“Hikari 连接池 + Hibernate 4.3.8 + Spring Data JPA 配置”?这是我的配置,但出于某种原因,我确信此配置不正确。因为控制台没有出现“连接池”之类的东西。在互联网上我找不到初学者的教程。谢谢。

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource" p:packagesToScan="com.history"
    p:jpaVendorAdapter-ref="jpaVendorAdapter">
    <property name="jpaProperties" ref="hibernateProperties" />
</bean>

<bean id="jpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true" />
    <property name="generateDdl" value="true" />
    <property name="database" value="ORACLE" />
    <property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" />
</bean>

<util:map id="hibernateProperties">
    <entry key="hibernate.connection.driver_class"
        value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider" />
    <entry key="connection.url" value="${dataSource.url}" />
    <entry key="connection.driver_class" value="${dataSource.driverClassName}" />
    <entry key="connection.username" value="${dataSource.username}" />
    <entry key="connection.password" value="${dataSource.password}" />
    <entry key="hibernate.jdbc.batch_size" value="100" />
    <entry key="hibernate.order_inserts" value="true" />
    <entry key="hibernate.order_updates" value="true" />
</util:map>

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${dataSource.driverClassName}" />
    <property name="url" value="${dataSource.url}" />
    <property name="username" value="${dataSource.username}" />
    <property name="password" value="${dataSource.password}" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    p:entityManagerFactory-ref="entityManagerFactory">
</bean>
4

3 回答 3

0

如果你想使用 Hikari,你需要声明它,所以你的 dataSource 配置应该是

<bean id="dataSource"
    class="com.zaxxer.hikari.HikariDataSource">
    <property name="driverClassName" value="${dataSource.driverClassName}" />
    <property name="jdbcUrl" value="${dataSource.url}" />
    <property name="username" value="${dataSource.username}" />
    <property name="password" value="${dataSource.password}" />
</bean>
于 2015-04-11T00:51:40.890 回答
0

你的数据源配置应该是这样的:

 <beans:bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource"  destroy-method="close">
                <beans:property name="dataSourceClassName" value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"/>
                <beans:property name="maximumPoolSize" value="5" />
                <beans:property name="maxLifetime" value="30000" />
                <beans:property name="idleTimeout" value="30000" />
                <beans:property name="dataSourceProperties">
                          <beans:props>
                              <beans:prop key="url">jdbc:mysql://localhost:3306/exampledb</beans:prop>
                              <beans:prop key="user">root</beans:prop>
                              <beans:prop key="password"></beans:prop>
                               <beans:prop key="prepStmtCacheSize">250</beans:prop>
                               <beans:prop key="prepStmtCacheSqlLimit">2048</beans:prop>
                               <beans:prop key="cachePrepStmts">true</beans:prop>
                               <beans:prop key="useServerPrepStmts">true</beans:prop>
                          </beans:props>
                </beans:property>
</beans:bean>

可以在链接中找到带有示例项目下载的完整示例:http: //frameworkonly.com/hikaricp-connection-pooling-in-spring-hibernate-jpa/

于 2015-07-07T12:50:33.630 回答
0

我们也可以像下面这样在自定义 JPAConfiguration 类中使用它,您可以在其中建立连接并创建实体 Manager Factory 对象。

         import com.zaxxer.hikari.HikariConfig;
         import com.zaxxer.hikari.HikariDataSource;


        @Bean
        public DataSource dataSource() {
            // In classpath from spring-boot-starter-web
            final Properties props = new Properties();
            props.put("driverClassName", "com.mysql.jdbc.Driver");
            props.put("jdbcUrl", "jdbc:mysql://localhost:3306/master?createDatabaseIfNotExist=true");
            props.put("username", "root");
            props.put("password", "mysql");
            HikariConfig hc = new HikariConfig(props);
            HikariDataSource ds = new HikariDataSource(hc);
            return ds;
        }
于 2016-12-29T04:34:43.930 回答