简体   繁体   English

使用Spring,Hibernate,JPA和C3P0的数据库连接异常

[英]Database connection exception using Spring, Hibernate, JPA e C3P0

I'm trying to configure my app to use C3P0 datasource (I can't use the datasource provided by the container). 我正在尝试将我的应用程序配置为使用C3P0数据源(我无法使用容器提供的数据源)。

But, when my app is starting, an exception has occurred: 但是,当我的应用程序启动时,发生了异常:

The user must supply a JDBC connection 用户必须提供JDBC连接

Bellow is my config files: 波纹管是我的配置文件:

spring-config.xml: spring-config.xml:

<bean id="pum" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="persistenceXmlLocations">
        <list>
            <value>classpath*:META-INF/persistence.xml</value>
        </list>
    </property>
</bean>

<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
    <property name="jdbcUrl" value="jdbc:oracle:thin:@host:1521:db" />
    <property name="user" value="dbuser" />
    <property name="password" value="passwd" />
    <property name="minPoolSize" value="5" />
    <property name="maxPoolSize" value="10" />
    <property name="maxStatements" value="0" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceUnitManager" ref="pum" />
    <property name="dataSource" ref="pooledDataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
    </property>
</bean>

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

<tx:annotation-driven transaction-manager="transactionManager" />

persistence.xml: persistence.xml:

<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="AppPU" transaction-type="RESOURCE_LOCAL">

    <properties>

        <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />            
        <property name="hibernate.show_sql" value="false" />
        <property name="hibernate.jdbc.batch_size" value="50" />

        <property name="hibernate.archive.autodetection" value="class"/>    
        <property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory" />
        <property name="hibernate.cache.use_query_cache" value="true" />
        <property name="hibernate.cache.use_second_level_cache" value="true" />

    </properties>

</persistence-unit> 

</persistence>

Exception: 例外:

java.lang.UnsupportedOperationException: The user must supply a JDBC connection
at org.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:54)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1573)
at org.hibernate.loader.Loader.doQuery(Loader.java:696)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2228)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2157)
at org.hibernate.loader.Loader.list(Loader.java:2117)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:310)
at $Proxy286.getResultList(Unknown Source)
at com.embraer.core.jpa.DaoSupport.execQueryIndexedParameters(DaoSupport.java:327)
at com.embraer.core.jpa.DaoSupport.findByNamedQuery(DaoSupport.java:280)
at com.embraer.core.jpa.DaoSupport.findAll(DaoSupport.java:132)
at br.com.embraer.aheadpro.dao.imp.ConfigDAOImpl.findAll(ConfigDAOImpl.java:41)
at br.com.embraer.aheadpro.dao.ConfigRepoDAO.getAll(ConfigRepoDAO.java:35)
at br.com.embraer.aheadpro.service.ConfigService.refresh(ConfigService.java:27)
at br.com.embraer.aheadpro.service.ConfigService.getConfig(ConfigService.java:35)
at br.com.embraer.aheadpro.service.EmailService.init(EmailService.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:340)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:293)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:130)
... 91 more

Thanks everybody! 谢谢大家!

Maybe this might help: http://thejavablog.wordpress.com/2010/11/26/java-lang-unsupportedoperationexception-the-user-must-supply-a-jdbc-connection/ , it basically says that there's something missing from persistence.xml ( <provider>org.hibernate.ejb.HibernatePersistence</provider> or maybe hibernate.connection.provider_class -property ?), and Hibernate's ConnectionProviderFactory chooses to create a UserSuppliedConnectionProvider, which has getConnection defined as such: 也许这可能有所帮助: http : //thejavablog.wordpress.com/2010/11/26/java-lang-unsupportedoperationexception-the-user-must-supply-a-jdbc-connection/ ,它基本上说有些东西缺少persistence.xml( <provider>org.hibernate.ejb.HibernatePersistence</provider>hibernate.connection.provider_class -property吗?),然后Hibernate的ConnectionProviderFactory选择创建一个UserSuppliedConnectionProvider,其getConnection定义如下:

public Connection getConnection() {
    throw new UnsupportedOperationException("The user must supply a JDBC connection");
}

I also compared this with one of our own Hibernate/JPA -setups that works just fine with C3PO, and the only differences I spotted were that we pass some more properties to c3p0.ComboPooledDataSource and some properties to org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter when defining EntityManagerFactory in the Spring configuration xml: 我还将其与我们自己的Hibernate / JPA设置之一进行了比较,该设置可与C3PO一起正常使用,我发现的唯一区别是,我们将更多属性传递给c3p0.ComboPooledDataSource,并将一些属性传递给org.springframework.orm.jpa。在Spring配置xml中定义EntityManagerFactory时的vendor.HibernateJpaVendorAdapter:

<property name="jpaVendorAdapter">          
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="false" />
        <property name="generateDdl" value="false" />
        <property name="databasePlatform" value="${jdbc.dialectClass}" />
    </bean>
</property>         

But I doubt that could be it. 但我怀疑可能是这样。 There are no properties defined in our persistence.xml (at all). 在persistence.xml中根本没有定义任何属性。

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

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