当前位置: 首页 > 知识库问答 >
问题:

hibernate的C3P0连接池不能与cloudfoundry一起使用

丁鸿云
2023-03-14

我正在尝试推出一个使用STS开发的简单hibernate MYSQL应用程序。Hibernate使用c3P0进行连接池。当我推送应用程序并开始访问它时,我看到与c3P0相关的异常。我在本地测试了它,它运行良好。

Caused by: 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)

这是我正在使用的云配置文件。

 <beans profile="cloud">
         <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver" />
            <property name="jdbcUrl" value="${cloud.services.mysql.connection.jdbcUrl}" />
            <property name="user" value="${cloud.services.mysql.connection.username}" />
            <property name="password" value="${cloud.services.mysql.connection.password}" />
            <property name="minPoolSize" value="1" />
            <property name="maxPoolSize" value="3" />
            <property name="maxIdleTime" value="20" />
        </bean>
          <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="c3p0DataSource" />
    <property name="hibernateProperties">
    <props>
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop>
      <prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
    </props>
      </property>
      <property name="packagesToScan" value="com.iml.policytype.model" /> 
       </bean>
      <tx:annotation-driven /> 
     <context:component-scan base-package="com.iml.policytype.bo" /> 
     <context:component-scan base-package="com.iml.policytype.dao" />
 <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
p:sessionFactory-ref="sessionFactory" />
</beans>

有人能帮忙吗?

这是个例外

org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:514)
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:372)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:417)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:255)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy58.getPolicyType(Unknown Source)
    at com.symantec.netbackup.iml.webservice.PolicyTypeResource.getMsg(PolicyTypeResource.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:680)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
Caused by: org.hibernate.exception.GenericJDBCException: Could not open connection
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
    at org.hibernate.internal.SessionImpl.connection(SessionImpl.java:550)
    at org.springframework.orm.hibernate4.HibernateTransactionManager.doBegin(HibernateTransactionManager.java:428)
    ... 47 more
Caused by: 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)
    at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection(AbstractPoolBackedDataSource.java:128)
    at org.hibernate.service.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:141)
    at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
    at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
    ... 50 more
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)
    ... 54 more

共有1个答案

薛扬
2023-03-14

虽然我没有尝试使用个人资料,但我还是能够做到

下面是如果要使用 c3P0 时应使用的 xml。

        <cloud:data-source id="c3p0DataSource" service-name="mysql"/>
         <cloud:properties id="cloudProperties"/>
         <context:property-placeholder properties-ref="cloudProperties"/>

<bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver" />
            <property name="jdbcUrl" value="jdbc:mysql://${cloud.services.mysql.connection.hostname}:${cloud.services.mysql.connection.port}/${cloud.services.mysql.connection.name}" />

            <property name="user" value="${cloud.services.mysql.connection.username}" />
            <property name="password" value="${cloud.services.mysql.connection.password}" />
            <property name="minPoolSize" value="2" />
            <property name="maxPoolSize" value="3" />
            <property name="maxIdleTime" value="319" />

        </bean>
        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="c3p0DataSource" />

            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>


                </props>
            </property>
            <property name="packagesToScan" value="com.iml.policytype.model" />
        </bean>

        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"
            p:sessionFactory-ref="sessionFactory" />

我没有使用导致DB连接失败的变量{cloud.services.mysql.connection.jdbcUrl}。如果你观察环境。日志文件此项的值还包含用户名和密码,而不仅仅是主机名、端口名和数据库名。

另外,我看到的属性cloud . services . MySQL . connection . hostname(cloud . services . MySQL . connection . name)也导致了这个问题。我想这没什么大不了的,因为这取决于你使用的数据源。在某些情况下,它可以是名称,在某些情况下,它可以是主机名。

此外,突出显示的代码缺少我的xml中的条目。我现在将尝试使用配置文件功能,并根据我所看到的内容更新线程。

 类似资料:
  • 问题内容: 自上次发布以来,是否提出了所有建议的更改,但此问题仍然困扰着我。这是我得到的错误: 这是我的hibernate.cfg.xml 和c3p0.properties 问题答案: 对于我来说,您配置的c3p0错误。 诸如c3p0.preferredTestQuery之类的属性必须位于类路径中的c3p0.properties文件中(例如WEB-INF / classes)。 下面是我的c3p0

  • 问题内容: 可用于Java / JDBC的最佳连接池是什么? 我正在考虑2个主要候选人(免费/开源): Apache DBCP- http://commons.apache.org/dbcp/ C3P0- http://sourceforge.net/projects/c3p0 我已经在博客和其他论坛上阅读了很多有关它们的内容,但无法做出决定。 这两个有其他相关选择吗? 问题答案: DBCP已过时

  • 这与之前未解决的一个帖子有关:这里C3P0似乎在Heroku上初始化连接池后立即进入死锁。这个问题不会发生在我的本地邮件上。 [DEBUG]“com.mchange.v2.resourcepool.basicresourcepool”2015-07-05 07:12:59,132:成功终止获取系列。递减的pending_acquires 1,Attests_Retailing:30 [DEBUG]

  • 问题内容: 我有在执行过程中挂起的控制台应用程序。这是我的配置: 这是我的堆栈跟踪: 我只打开了一个锥形杯,但似乎没有泄漏。而且我也使用一个线程。除了内存使用率,我没有调整任何mysql设置。Mysql从控制台正常工作。为什么会发生这种情况?这是c3p0错误吗? 问题答案: 立即发生还是在一段时间后发生?也就是说,结帐最初是否成功,但随后却像这样挂起?如果是这样,它看起来像是连接泄漏。请尝试将c3

  • 我的应用程序有两个quartz作业同时从Oracle DB读取两个不同的表。我正在尝试使用c3p0与Hibernate和Oracle从池中获取连接。Tomcat7启动时,C3P0似乎已初始化: 2021-02-15 18:47:26,271[INFO]HibernateEntityManager 3.2.1.GA 2021-02-15 18:47:26,290[INFO]Hibernate注释3.

  • 问题内容: 我最近开始在应用程序中使用hibernate和c3p0作为ORM。但是,当我关闭会话工厂时,连接池不会自行关闭!这是我的应用程序中 唯一 可以进行会话操作的地方。 这是我的配置文件 请注意,空闲连接非常短的原因是它是我尚未通过集成测试的唯一方法。他们经常打开和关闭会话工厂,因此我总是用尽所有连接。正如我们在项目开始时一样,从长远来看,我认为这不是一个非常可持续的策略。 需要注意的“有趣