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


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

運行通過了 哎~~~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM