简体   繁体   English

Spring数据源+ JPA + Hibernate + c3p0 + ehCache

[英]Spring datasource + JPA + Hibernate +c3p0 + ehCache

I want to connect to my MySQL database using the folliowing libraries: 我想使用以下库连接到我的MySQL数据库:

  • spring: because my webapp use the spring-mvc framework 春天:因为我的webapp使用spring-mvc框架
  • hibernate (JPA): because it is a standard 休眠(JPA):因为它是标准
  • c3p0: for performance c3p0:性能
  • ehCache: for performance ehCache:性能

Here is my applicationContext.xml: 这是我的applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="dao,service" />

    <!-- Configuration du transaction manager -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="entity" />

        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">validate</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="current_session_context_class">thread</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
                <!-- used for debug -->
                <prop key="hibernate.show_sql">true</prop>
                <!-- EhCache -->
                <prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:ehcache.xml</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.SingletonEhCacheProvider</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
                <!-- configuration pool via c3p0, see https://community.jboss.org/wiki/HowToConfigureTheC3P0ConnectionPool -->
                <prop key="hibernate.connection.provider_class">org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider</prop> 
                    <prop key="hibernate.c3p0.acquire_increment">1</prop> <prop key="hibernate.c3p0.max_size">5</prop> 
                    <prop key="hibernate.c3p0.max_statements">100</prop> <prop key="hibernate.c3p0.min_size">1</prop> 
                    <prop key="hibernate.c3p0.timeout">100</prop> <prop key="hibernate.checkoutTimeout">1000</prop> 
                    <prop key="hibernate.c3p0.idleConnectionTestPeriod">30</prop> <prop key="hibernate.c3p0.preferredTestQuery">SELECT 1
                </prop>
            </props>
        </property>
    </bean>

    <!-- Configuration de la BDD -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/mydb?autoReconnect=true" />
        <property name="username" value="user" />
        <property name="password" value="password" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
</beans>

I dont get any error but I'm not sure that c3p0 and ehCache are working :/ 我没有收到任何错误,但是我不确定c3p0和ehCache是​​否正常工作:/

It is a bit late, but anyway. 有点晚了,但是无论如何。

One mistake in your XML is that you tried to configure both datasource and hibernate.c3p0 in entityManagerFactory bean. XML中的一个错误是您试图在entityManagerFactory bean中配置数据源和hibernate.c3p0 When you do this way, hibernate will not take into account any hibernate.c3p0 properties (because it will not instantiate DataSource in that case). 当您这样做时,hibernate将不会考虑任何hibernate.c3p0属性(因为在这种情况下它不会实例化DataSource )。

So, if you set datasource to EntityManagerFactory (or SessionFactory), you should consider configuring connection-pooling at Datasource side, like this (C3P0): 因此,如果将数据源设置为EntityManagerFactory(或SessionFactory),则应考虑在数据源侧配置连接池,例如(C3P0):

<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver" />
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydb?autoReconnect=true" />
    <property name="user" value="user" />
    <property name="password" value="password" />
    <property name="maxPoolSize" value="2"/>
    <property name="minPoolSize" value="1"/>
    <property name="idleConnectionTestPeriod" value="3000"/>
</bean>

or any other way you like. 或您喜欢的任何其他方式。

This behaviour is barely documented in Spring documentation, here is what I have found . Spring文档中几乎没有记录这种行为,这是我发现的

For ComboPooledDataSource, see c3p0 documentation 对于ComboPooledDataSource,请参阅c3p0文档

As related to hibernate caching, it should work properly. 与休眠缓存有关,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM