Could not obtain connection to query metadata : An attempt by a client to checkout a Connection has ...

hibernate 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
       <context:property-placeholder location="classpath:/META-INF/properties/hibernate.properties" />
       <!-- 使用C3P0数据源,MySQL数据库 -->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
             destroy-method="close">
              <!-- MySQL5 -->
              <property name="driverClass" value="${driverClassName}"/>
              <property name="jdbcUrl" value="${url}"/>
              <property name="user" value="${username}"/>
              <property name="password" value="${password}"/>
              <property name="maxPoolSize" value="300"/>
              <property name="minPoolSize" value="5"/>
              <!-- 请求超时时间 -->
              <property name="checkoutTimeout" value="300" />
              <!-- 每300秒检查所有连接池中的空闲连接。默认值: 0,不检查 -->
              <property name="idleConnectionTestPeriod" value="300" />
               <!-- 连接池初始化连接数 -->
               <property name="initialPoolSize" value="3"/>
               <!-- 连接数据库连接池最大空闲时间 -->
               <property name="maxIdleTime" value="30"/>
              <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。默认值: 3 -->
              <property name="acquireIncrement" value="5" />
            <!--<property name="unreturnedConnectionTimeout" value="25"/>-->
       </bean>
       <!-- session工厂 -->
       <!-- spring与hibernate整合配置,扫描所有dao -->
       <bean id="sessionFactory"
             class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
              <property name="dataSource" ref="dataSource" />
              <property name="packagesToScan"><list><value>com.wawagame.backend.hbentity</value></list>
              </property>
              <property name="hibernateProperties">
                     <props>
                            <!-- 它包含4个属性: * create : 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变
                                * create-drop : 根据model类生成表,但是sessionFactory一关闭,表就自动删除 * update : 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行
                                * validate : 只会和数据库中的表进行比较,不会创建新表,但是会插入新值 -->
                            <!--<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>-->
                            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                            <prop key="hibernate.use_sql_comments">${hibernate.use_sql_comments}</prop>
                            <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                     </props>
              </property>
       </bean>
       <bean id="transactionManager"
             class="org.springframework.orm.hibernate4.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory">
              </property>
       </bean>
       <!-- 对数据源进行事务管理 -->
       <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

properties内容

hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8
username=root
password=root
hibernate.use_sql_comments=true
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true

 

原因排查:配置的值反复检查了好几遍都没问题,将 properties中的值直接放到xml中就可以运行,经过在网上没找到好的解决办法最后慢慢的试试最终问题出现在properties中的名称上了晕死username=root 不能用username这个名字可能冲突了

最后改成

 

hibernate.dialect=org.hibernate.dialect.MySQLDialect
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
hibernate.use_sql_comments=true
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=true

运行通过了 哎~~~~

 

转载于:https://www.cnblogs.com/sy-liu/p/8968198.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值