簡體   English   中英

使用JPA和Hibernate在Persistence.xml中配置C3P0

[英]Configuring C3P0 in Persistence.xml with JPA and Hibernate

好吧,我試圖用JPA + Hibernate + Spring第一次配置C3P0。 在persistence.xml中我有:

<properties>
    <property name="hibernate.show_sql" value="false" />
    <property name="hibernate.format_sql" value="false" />
    <property name="hibernate.hbm2ddl.auto" value="update" />
    <property name="hibernate.cache.use_second_level_cache"
        value="false" />
    <property name="hibernate.cache.use_query_cache" value="false" />
    <property name="hibernate.jdbc.batch_size" value="50" />

    <!-- Important -->
    <property name="hibernate.connection.provider_class"
        value="org.hibernate.connection.C3P0ConnectionProvider" />

    <property name="hibernate.c3p0.max_size" value="20" />
    <property name="hibernate.c3p0.min_size" value="5" />
    <property name="hibernate.c3p0.acquire_increment" value="1" />
    <property name="hibernate.c3p0.idle_test_period" value="3000" />
    <property name="hibernate.c3p0.max_statements" value="50" />
    <property name="hibernate.c3p0.timeout" value="300" />
</properties>

但是當我嘗試初始化tomcat時,我收到以下錯誤:

Caused by: com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
    at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)
    at com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)
    at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)
    ... 58 more
50570 [Thread-4] ERROR org.hibernate.tool.hbm2ddl.SchemaUpdate - could not complete schema update
java.sql.SQLException: Connections could not be acquired from the underlying database!
    at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)

編輯1:

這是我的applicationContext.xml,我怎么能在里面配置C3P0?

<?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-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <!-- Seta anotaçoes para serem usadas pelo Spring -->
    <context:annotation-config />

    <!-- Define o pacote onde o Spring vai procurar por beans anotados -->
    <context:component-scan base-package="br.com.myapp" />

    <!-- define que as transaçoes irao ser anotadas -->
    <tx:annotation-driven proxy-target-class="true" />

    <!-- Configuracao do Banco de Dados -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost/mydatabase" />
        <property name="username" value="postgres" />
        <property name="password" value="pgadmin" />
    </bean>

    <!-- Configuracao do Hibernate -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="senderPU" />
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="org.hibernate.dialect.PostgreSQLDialect" />
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

    <!-- Configuracao do gerente de transacoes do Spring -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

</beans>

您的配置存在缺陷。 您正在應用程序上下文中配置DataSource 所以基本上所有hibernate.c3po屬性是沒有用的,旁邊的的設置hibernate.connection.provider_class財產傷了你的應用程序。 C3P0ConnectionProvider需要C3P0連接,但是您使用的是基本的DriverManagerDataSource

我建議不要試圖讓hibernate管理池只需在你的applicationcontext中配置它。 用以下內容替換數據源定義

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <!-- Connection properties -->
    <property name="driverClass" value="org.postgresql.Driver" />
    <property name="jdbcUrl" value="jdbc:postgresql://localhost/mydatabase" />
    <property name="user" value="postgres" />
    <property name="password" value="pgadmin" />
    <!-- Pool properties -->
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="20" />
    <property name="acquireIncrement" value="1" />
    <property name="maxStatements" value="50" />
    <property name="idleConnectionTestPeriod" value="3000" />
    <property name="loginTimeout" value="300" />
</bean>

並從persistence.xml中刪除hibernate.c3p0hibernate.connection.provider_class 將配置移動到Spring的優點是,您可以使用屬性文件來包含屬性,並將它們替換為PropertyPlaceHolderConfigurer而不是將它們固定在persistence.xml中

基本上,您可以從persistence.xml中刪除所有屬性,並將它們移動到基於彈簧的配置。

2個其他非相關建議。 您可以刪除<context:annotation-config /> <context:component-scan />已隱含的<context:annotation-config /> <context:component-scan /> 建議在標題中使用無版本的xsd文件,因此我建議刪除這些版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM