zoukankan      html  css  js  c++  java
  • 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

    运行通过了 哎~~~~

  • 相关阅读:
    iOS
    iOS
    iOS
    iOS
    iOS(WKWebView)OC与JS交互 之三
    iOS(UIWebView 和WKWebView)OC与JS交互 之二
    CentOS VMware 下SSH配置方法详解
    15个nosql数据库
    MySql 优化
    Elasticsearch 相关名词理解
  • 原文地址:https://www.cnblogs.com/sy-liu/p/8968198.html
Copyright © 2011-2022 走看看