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

hibernate c3p0 configuration:

 

 

<?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" />
       <!-- Use C3P0 data source, MySQL database -->
       <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"/>
              <!-- request timeout-->
              <property name="checkoutTimeout" value="300" />
              <!-- Check all connection pools for idle connections every 300 seconds. Default: 0, do not check -->
              <property name="idleConnectionTestPeriod" value="300" />
               <!-- Connection pool initialization connection number-->
               <property name="initialPoolSize" value="3"/>
               <!-- The maximum idle time for connecting to the database connection pool-->
               <property name="maxIdleTime" value="30"/>
              <!--The number of connections obtained by c3p0 at one time when the connections in the connection pool are exhausted. Default: 3 -->
              <property name="acquireIncrement" value="5" />
            <!--<property name="unreturnedConnectionTimeout" value="25"/>-->
       </bean>
       <!-- session factory-->
       <!-- Spring integrates configuration with hibernate, scans all 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>
                            <!-- It contains 4 attributes: * create : It will generate a table based on your model class, but each run will delete the previous table and regenerate the table, even if there is no change after 2 times
                                * create-drop : The table is generated according to the model class, but the table is automatically deleted as soon as the sessionFactory is closed. * update : The most commonly used attributes are also generated according to the model class. Even if the table structure changes, the rows in the table still exist, no delete previous line
                                * validate : will only compare with the table in the database, will not create a new table, but will insert new values ​​-->
                            <!--<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>
       <!-- Transaction management of the data source-->
       <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

properties content

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

 

Troubleshooting: The configured value has been checked several times and there is no problem. Put the value in the properties directly into the xml and it can be run. After no good solution was found on the Internet, I finally tried it slowly. The final problem appeared in the properties. The name is on the faint username = root can not use the name username may conflict

Finally changed to

 

hibernate.dialect=org.hibernate.dialect.MySQLDialect
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&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

It passed the operation~~~~

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325003897&siteId=291194637