Configuring C3P0 in Persistence.xml with JPA and Hibernate

为君一笑 提交于 2019-12-04 11:57:36

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.

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

Basically you could remove all your properties from the persistence.xml and move them to the spring based configuration.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!