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

hibernate c3p0配置:mysql

 

 

<?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內容spring

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這個名字可能衝突了sql

最後改爲數據庫

 

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

運行經過了 哎~~~~session

相關文章
相關標籤/搜索