spring-mybatis配置使用jdbc.properties導致數據庫連接獲取不到

在spring-mybatis的配置中使用jdbc配置數據源,卡在

Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@83ecde7] was not registered for synchronization because synchronization is not active

不運行,等待一段時間後報

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2fc16b69]
十月 07, 2015 6:05:23 下午 org.apache.catalina.core.StandardWrapperValve invoke
嚴重: Servlet.service() for servlet [SpringMVC] in context with path [/lemon] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!
### The error may exist in file [F:\Program Files (x86)\Apache\apache-tomcat-myeclipse\webapps\lemon\WEB-INF\classes\com\lemengxiangju\xiangju\mapper\AdmissionPlanMapper.xml]
### The error may involve com.lemengxiangju.xiangju.dao.AdmissionPlanMapper.selectAll
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!] with root cause
com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.

錯誤。

配置文件
spring-mybatis

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans      
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd      
                        http://www.springframework.org/schema/context      
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd      
                        http://www.springframework.org/schema/mvc      
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <bean id="dataSource" destroy-method="close"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClassName}"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="${username}"></property>
        <property name="password" value="${password}"></property>
        <property name="initialPoolSize" value="${initialSize}"></property>
        <property name="maxPoolSize" value="${maxIdle}"></property>
        <property name="maxStatements" value="${maxActive}"></property>
    </bean>

    <!-- 加載jdbc -->
    <context:property-placeholder location="classpath:properties/jdbc.properties" />

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:config/mybatis-config.xml"></property>

        <!-- 自動掃描mapping.xml文件 -->
        <property name="mapperLocations"
            value="classpath:com/lemengxiangju/xiangju/mapper/*Mapper.xml"></property>

    </bean>

    <!-- DAO接口所在包名,Spring會自動查找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lemengxiangju.xiangju.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>


    <!-- 聲明式事務管理 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>  

jdbc.properties

# MySQL JDBC
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/kindergarten?useSSL=true
username=root
password=123456
initialSize=10
maxActive=50
maxIdle=25
minIdle=6

解決辦法:
將spring-mybatis中的數據源配置改爲

<bean id="dataSource" destroy-method="close"
        class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="${url}"></property>
        <property name="user" value="root"></property>
        <property name="password" value="${password}"></property>
        <property name="initialPoolSize" value="${initialSize}"></property>
        <property name="maxPoolSize" value="${maxIdle}"></property>
        <property name="maxStatements" value="${maxActive}"></property>
    </bean>

也就是將驅動類和用戶名直接寫入。不是用properties配置。

具體原因不清楚,可能是因爲讀取properties配置文件導致連接超時。後臺連接數據庫不成功。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章