Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring数据源&x2B;JPA&x2B;冬眠+;c3p0+;ehCache_Spring_Hibernate_Spring Mvc_Jpa_C3p0 - Fatal编程技术网

Spring数据源&x2B;JPA&x2B;冬眠+;c3p0+;ehCache

Spring数据源&x2B;JPA&x2B;冬眠+;c3p0+;ehCache,spring,hibernate,spring-mvc,jpa,c3p0,Spring,Hibernate,Spring Mvc,Jpa,C3p0,我想使用以下库连接到我的MySQL数据库: spring:因为我的webapp使用SpringMVC框架 hibernate(JPA):因为它是一种标准 c3p0:用于性能 ehCache:为了提高性能 这是我的applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:x

我想使用以下库连接到我的MySQL数据库:

  • spring:因为我的webapp使用SpringMVC框架
  • hibernate(JPA):因为它是一种标准
  • c3p0:用于性能
  • ehCache:为了提高性能
这是我的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>

验证
org.hibernate.dialogue.mysql5dialogue
线
org.hibernate.cache.internal.NoCacheProvider
真的
类路径:ehcache.xml
org.hibernate.cache.ehcache.EhCacheRegionFactory
org.hibernate.cache.SingletonEhCacheProvider
真的
真的
真的
org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider
1 5 
100 1 
100 1000 
30选择1

我没有收到任何错误,但我不确定c3p0和ehCache是否正常工作://

虽然有点晚,但无论如何

XML中的一个错误是,您试图在
entityManagerFactory
bean中配置datasource和
hibernate.c3p0
。当您这样做时,hibernate将不考虑任何
hibernate.c3p0
属性(因为在这种情况下它不会实例化
DataSource

这样,如果将数据源设置为EntIdMauleFr工厂(或SeaStudio),则应考虑在数据源侧配置连接池,如(C3P0):


或者你喜欢的其他方式

这种行为在Spring文档中几乎没有记录,这里是

有关ComboPooledDataSource,请参阅


与hibernate缓存相关,它应该可以正常工作

是什么让你认为这些成分不起作用?此外,您还应该查看
HikariCP
,了解比c3p0更好的连接池性能。谢谢,当我还是学生时,老师使用c3p0教我们连接池。但我同意,与HikariCPIt相比,HikariCPIt看起来不太受欢迎,但HikariCPIt似乎非常非常快!我将尝试用HikaricpSunds配置它,这是一个不错的选择!!
<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>