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配置文件导致连接超时。后台连接数据库不成功。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值