spring-mybatis配置使用jdbc.properties導致資料庫連接獲取不到
阿新 • • 發佈:2019-01-04
在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 [[email protected]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配置檔案導致連線超時。後臺連線資料庫不成功。