如何使用Hibernate和JNDI配置c3p0?

时间:2018-09-18 07:45:00

标签: java hibernate jndi c3p0 geronimo

我在geronimo服务器中部署了一个应用程序,并使用了Hibernate框架。我将C3P0库与它集成在一起,当我像这样在hibernate.cfg.xml中欺骗数据库URL时,它可以完美地工作:

<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@iddfrvexa.grouperci.com:1574:IDDFRV</property>
        <property name="hibernate.connection.username">iqa</property>
        <property name="hibernate.connection.password">iqaadmin</property>
        <!-- <property name="connection.datasource">java:comp/env/rci_cdofr/jdbc/Datasource</property> -->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="show_sql">false</property>
        <property name="max_fetch_depth">0</property>
        <property name="hibernate.jdbc.factory_class">net.bull.javamelody.HibernateBatcherFactory</property>

        <!-- <property name="hibernate.dbcp.validationQuery">SELECT 1 FROM DUAL</property> 
            <property name="hibernate.dbcp.testOnBorrow">true</property> -->


        <!-- c3p0 config http://www.hibernate.org/214.html -->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="c3p0.min_size">5</property>
        <property name="c3p0.max_size">20</property>
        <property name="c3p0.timeout">1800</property>
        <property name="c3p0.max_statements">50</property>
        <property name = "c3p0.testConnectionOnCheckout">true</property>

但是当我像这样使用jndi时:

 <property name="connection.datasource">java:comp/env/rci_cdofr/jdbc/Datasource</property>
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="show_sql">false</property>
        <property name="max_fetch_depth">0</property>

        <property name="hibernate.jdbc.factory_class">net.bull.javamelody.HibernateBatcherFactory</property>

        <!-- c3p0 config http://www.hibernate.org/214.html -->
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="c3p0.min_size">5</property>
        <property name="c3p0.max_size">20</property>
        <property name="c3p0.timeout">1800</property>
        <property name="c3p0.max_statements">50</property>
        <property name = "c3p0.testConnectionOnCheckout">true</property>

c3p0不起作用,并且出现了错误。

  

java.sql.SQLException:无法从服务器获取连接   基础数据库!在   com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)在   com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:529)     在   com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)     在   org.hibernate.connection.C3P0ConnectionProvider.getConnection(C3P0ConnectionProvider.java:78)     在   org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:84)     在   org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2009)     在   org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)     在   com.rcibanque.framework.persistance.hibernate.HibernateUtil。(HibernateUtil.java:45)     在   com.rcibanque.framework.persistance.hibernate.HibernateTransaction.begin(HibernateTransaction.java:62)     在   com.rcibanque.common.application.ReferencesService.loadReferences(ReferencesService.java:52)     在com.rcibanque.common.start.InitServlet.init(InitServlet.java:46)     在javax.servlet.GenericServlet.init(GenericServlet.java:215)在   org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1108)     在   org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1010)     在   org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4413)     在   org.apache.catalina.core.StandardContext.start(StandardContext.java:4723)     在   org.apache.geronimo.tomcat.GeronimoStandardContext.access $ 201(GeronimoStandardContext.java:63)     在   org.apache.geronimo.tomcat.GeronimoStandardContext $ SystemMethodValve.invoke(GeronimoStandardContext.java:398)     在   org.apache.geronimo.tomcat.valve.GeronimoBeforeAfterValve.invoke(GeronimoBeforeAfterValve.java:47)     在   org.apache.geronimo.tomcat.GeronimoStandardContext.start(GeronimoStandardContext.java:251)     在   org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)     在   org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)     在   org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)     在   org.apache.geronimo.tomcat.TomcatContainer.addContext(TomcatContainer.java:369)     在   org.apache.geronimo.tomcat.TomcatWebAppContext.doStart(TomcatWebAppContext.java:512)     在   org.apache.geronimo.gbean.runtime.GBeanInstance.createInstance(GBeanInstance.java:998)     在   org.apache.geronimo.gbean.runtime.GBeanInstanceState.attemptFullStart(GBeanInstanceState.java:268)     在   org.apache.geronimo.gbean.runtime.GBeanInstanceState.start(GBeanInstanceState.java:102)     在   org.apache.geronimo.gbean.runtime.GBeanInstance.start(GBeanInstance.java:541)     在   org.apache.geronimo.gbean.runtime.GBeanDependency.attemptFullStart(GBeanDependency.java:111)     在   org.apache.geronimo.gbean.runtime.GBeanDependency.addTarget(GBeanDependency.java:146)     在   org.apache.geronimo.gbean.runtime.GBeanDependency $ 1.running(GBeanDependency.java:120)     在   org.apache.geronimo.kernel.basic.BasicLifecycleMonitor.fireRunningEvent(BasicLifecycleMonitor.java:176)     在   org.apache.geronimo.kernel.basic.BasicLifecycleMonitor.access $ 300(BasicLifecycleMonitor.java:44)     在   org.apache.geronimo.kernel.basic.BasicLifecycleMonitor $ RawLifecycleBroadcaster.fireRunningEvent(BasicLifecycleMonitor.java:254)     在   org.apache.geronimo.gbean.runtime.GBeanInstanceState.attemptFullStart(GBeanInstanceState.java:294)     在   org.apache.geronimo.gbean.runtime.GBeanInstanceState.start(GBeanInstanceState.java:102)     在   org.apache.geronimo.gbean.runtime.GBeanInstanceState.startRecursive(GBeanInstanceState.java:124)     在   org.apache.geronimo.gbean.runtime.GBeanInstance.startRecursive(GBeanInstance.java:555)     在   org.apache.geronimo.kernel.basic.BasicKernel.startRecursiveGBean(BasicKernel.java:379)     在   org.apache.geronimo.kernel.config.ConfigurationUtil.startConfigurationGBeans(ConfigurationUtil.java:456)     在   org.apache.geronimo.kernel.config.ConfigurationUtil.startConfigurationGBeans(ConfigurationUtil.java:493)     在   org.apache.geronimo.kernel.config.KernelConfigurationManager.start(KernelConfigurationManager.java:188)     在   org.apache.geronimo.kernel.config.SimpleConfigurationManager.startConfiguration(SimpleConfigurationManager.java:563)     在   org.apache.geronimo.kernel.config.SimpleConfigurationManager.startConfiguration(SimpleConfigurationManager.java:544)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)在   org.apache.geronimo.gbean.runtime.ReflectionMethodInvoker.invoke(ReflectionMethodInvoker.java:34)     在   org.apache.geronimo.gbean.runtime.GBeanOperation.invoke(GBeanOperation.java:124)     在   org.apache.geronimo.gbean.runtime.GBeanInstance.invoke(GBeanInstance.java:867)     在   org.apache.geronimo.kernel.basic.BasicKernel.invoke(BasicKernel.java:239)     在   org.apache.geronimo.kernel.KernelGBean.invoke(KernelGBean.java:342)     在sun.reflect.GeneratedMethodAccessor163.invoke(未知源)在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)在   org.apache.geronimo.gbean.runtime.ReflectionMethodInvoker.invoke(ReflectionMethodInvoker.java:34)     在   org.apache.geronimo.gbean.runtime.GBeanOperation.invoke(GBeanOperation.java:124)     在   org.apache.geronimo.gbean.runtime.GBeanInstance.invoke(GBeanInstance.java:867)     在   org.apache.geronimo.kernel.basic.BasicKernel.invoke(BasicKernel.java:239)     在   org.apache.geronimo.system.jmx.MBeanGBeanBridge.invoke(MBeanGBeanBridge.java:172)     在   com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)     在   com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)     在   javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)     在   javax.management.remote.rmi.RMIConnectionImpl.access $ 300(RMIConnectionImpl.java:97)     在   javax.management.remote.rmi.RMIConnectionImpl $ PrivilegedOperation.run(RMIConnectionImpl.java:1328)     在java.security.AccessController.doPrivileged(本机方法)在   javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1427)     在   javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)     在sun.reflect.GeneratedMethodAccessor150.invoke(未知源)在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)在   sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)     在sun.rmi.transport.Transport $ 1.run(Transport.java:177)在   sun.rmi.transport.Transport $ 1.run(Transport.java:174)在   java.security.AccessController.doPrivileged(本机方法),位于   sun.rmi.transport.Transport.serviceCall(Transport.java:173)在   sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)     在   sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run0(TCPTransport.java:811)     在   sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run(TCPTransport.java:670)     在   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     在   java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)     在java.lang.Thread.run(Thread.java:745)造成原因:   com.mchange.v2.resourcepool.CannotAcquireResourceException:A   ResourcePool无法从其主要工厂获取资源或   资源。在   com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1319)     在   com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource(BasicResourcePool.java:557)     在   com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource(BasicResourcePool.java:477)     在   com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(C3P0PooledConnectionPool.java:525)     ...另外82个

,当我调试时,我发现数据库URL为空。  谁能帮我吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

如果您想使用c3p0池版本包装现有的JNDI数据源,则可能必须使用Java自己构建该数据源,然后配置Hibernate来寻找该DataSource的JNDI名称。我不认为hibernate支持通过配置DataSource将现有ConnectionProvider包装在c3p0池中。

包装未池化的​​DataSource的Java方面实际上是easy in c3p0。只需获取对您现有的未合并DataSource的引用,并使用它代替ds_unpooled