Java 使用 JPA 和 Hibernate 在 Persistence.xml 中配置 C3P0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23554873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuring C3P0 in Persistence.xml with JPA and Hibernate
提问by Mr.Lanhellas
Well, i'm trying to configure for first time the C3P0 with JPA + Hibernate + Spring. In persistence.xml i have:
好吧,我第一次尝试使用 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>
But when i try to initialize the tomcat, i got the following error:
但是当我尝试初始化 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)
EDIT 1:
编辑 1:
This is my applicationContext.xml, how can i configure C3P0 inside this ?
这是我的 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>
采纳答案by M. Deinum
Your configuration is flawed. You are configuring the DataSource
in the application context. So basically all hibernate.c3po
properties are useless, next to that the setting of the hibernate.connection.provider_class
property breaks your application. The C3P0ConnectionProvider
expects a C3P0 Connection however you are using a basic DriverManagerDataSource
.
你的配置有问题。您正在DataSource
应用程序上下文中配置。所以基本上所有的hibernate.c3po
属性都是无用的,接下来是hibernate.connection.provider_class
属性的设置会破坏你的应用程序。该C3P0ConnectionProvider
预计然而,您使用的是基本的一个C3P0连接DriverManagerDataSource
。
I would suggest instead of trying to get hibernate to manage the pool simply configure it inside your applicationcontext. Replace your datasource definition with the following
我建议不要尝试让休眠来管理池,只需在应用程序上下文中配置它即可。将您的数据源定义替换为以下内容
<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>
And remove the hibernate.c3p0
and hibernate.connection.provider_class
from your persistence.xml. Added advantage of moving the configuration to Spring is that you could use a properties file to contain your properties and have them replaced by a PropertyPlaceHolderConfigurer
instead of having them fixed in your persistence.xml
并从您的persistence.xml 中删除hibernate.c3p0
和hibernate.connection.provider_class
。将配置移动到 Spring 的另一个好处是您可以使用属性文件来包含您的属性,并将它们替换为 ,PropertyPlaceHolderConfigurer
而不是将它们固定在您的persistence.xml 中
Basically you could remove all your properties from the persistence.xml and move them to the spring based configuration.
基本上,您可以从 persistence.xml 中删除所有属性并将它们移动到基于 spring 的配置中。
2 other, non, related suggestions. You can remove <context:annotation-config />
that is already implied by <context:component-scan />
. It is recommended to use versionless xsd files in your header, so I would suggest removing the versions.
2 其他非相关建议。您可以删除<context:annotation-config />
一个已经被暗示<context:component-scan />
。建议在标题中使用无版本的 xsd 文件,因此我建议删除这些版本。