spring + mybatis配置及网络异常设置
Spring引入mybatis
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- 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 ">
- <context:component-scan base-package="com.bf.health" />
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
- <property name="url" value="jdbc:oracle:thin:@192.168.0.69:1521:orcl"/>
- <property name="username" value="BFDEVDB_301"/>
- <property name="password" value="bfdevdb"/>
- </bean>
- <!-- 创建SqlSessionFactory,同时指定数据源-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="configLocation" value="classpath:mybatis-config.xml"/>
- </bean>
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="0" ref="sqlSessionFactory" />
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- <property name="basePackage" value="com.bf.health.dao" />
- </bean>
- </beans>
要加上事务控制,则在</beans>前加上
- <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
由于我们用的mybatis3的注解,因此spring的sqlSessionTemplate也不用配置了,sqlSessionTemplate也不用注入到我们的BaseDAO中了。
另外发现<property name="basePackage" value="com.bf.health.dao" />也可以不配置。
最后呈上完整xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
- <context:component-scan base-package="com.bf.health" />
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
- <property name="url" value="jdbc:oracle:thin:@192.168.101.21:1521:orcl"/>
- <property name="username" value="BF_HEALTH_01"/>
- <property name="password" value="winway_health_20161013"/>
- </bean>
- <!-- 创建SqlSessionFactory,同时指定数据源-->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="configLocation" value="classpath:mybatis-config.xml"/>
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- <property name="basePackage" value="com.bf.health.dao" />
- </bean>
- <!-- ================================事务相关控制===================================================== -->
- <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager" />
- </beans>
实际项目中发现当网络断开又重新连接上之后,系统连接数据库异常,原因应该是当前线程池中的连接实际上已经失效了,但dbcp仍认为它是有效的,因此仍在尝试用旧连接访问数据库,直至网络超时。
可参见:dbcp重连问题排查
需修改配置如下:
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
- <property name="url" value="jdbc:oracle:thin:@192.168.101.21:1521:orcl"/>
- <property name="username" value="BF_HEALTH_01"/>
- <property name="password" value="winway_health_20161013"/>
- <property name="testWhileIdle"><value>true</value></property> <!-- 打开检查,用异步线程evict进行检查 -->
- <property name="testOnBorrow"><value>false</value></property>
- <property name="testOnReturn"><value>false</value></property>
- <property name="validationQuery"><value>select sysdate from dual</value></property>
- <property name="validationQueryTimeout"><value>1</value></property>
- <property name="timeBetweenEvictionRunsMillis"><value>30000</value></property>
- <property name="numTestsPerEvictionRun"><value>20</value></property>
- </bean>
具体解释,参照dbcp基本配置和重连配置,
dbcp的链接validate配置
- dbcp是采用了commons-pool做为其连接池管理,testOnBorrow,testOnReturn, testWhileIdle是pool是提供的几种校验机制,通过外部钩子的方式回调dbcp的相关数据库链接(validationQuery)校验
- dbcp相关外部钩子类:PoolableConnectionFactory,继承于common-pool PoolableObjectFactory
- dbcp通过GenericObjectPool这一入口,进行连接池的borrow,return处理
- testOnBorrow : 顾明思义,就是在进行borrowObject进行处理时,对拿到的connection进行validateObject校验
- testOnReturn : 顾明思义,就是在进行returnObject对返回的connection进行validateObject校验,个人觉得对数据库连接池的管理意义不大
- testWhileIdle : 关注的重点,GenericObjectPool中针对pool管理,起了一个Evict的TimerTask定时线程进行控制(可通过设置参数timeBetweenEvictionRunsMillis>0),定时对线程池中的链接进行validateObject校验,对无效的链接进行关闭后,会调用ensureMinIdle,适当建立链接保证最小的minIdle连接数。
- timeBetweenEvictionRunsMillis,设置的Evict线程的时间,单位ms,大于0才会开启evict检查线程
- validateQuery, 代表检查的sql
- validateQueryTimeout, 代表在执行检查时,通过statement设置,statement.setQueryTimeout(validationQueryTimeout)
- numTestsPerEvictionRun,代表每次检查链接的数量,建议设置和maxActive一样大,这样每次可以有效检查所有的链接.
spring + mybatis配置及网络异常设置的更多相关文章
- Spring+Mybatis配置
Spring+Mybatis配置 之前做项目的时候用到了spring+mybatis框架,一直想抽空整理一下 Mybatis: mybatis是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架 ...
- spring+myBatis 配置多数据源,切换数据源
注:本文来源于 tianzhiwuqis <spring+myBatis 配置多数据源,切换数据源> 一个项目里一般情况下只会使用到一个数据库,但有的需求是要显示其他数据库的内容,像这样 ...
- maven spring mybatis配置注意点
以下对使用maven配置spring+mybatis项目时,完成基本的配置需要添加的一些信息进行说明.仅对mybatis部分进行列举. maven添加mybatis支持 <!-- mybatis ...
- Maven+Spring MVC Spring Mybatis配置
环境: Eclipse Neon JDK1.8.0 Tomcat8.0 先决条件: Eclipse先用maven向导创建web工程.参见本站之前随笔. 本机安装完成mysql5:新建用户xuxy03设 ...
- spring+mybatis 配置双数据源
配置好后,发现网上已经做好的了, 不过,跟我的稍有不同, 我这里再拿出来现个丑: properties 文件自不必说,关键是这里的xml: <?xml version="1.0&quo ...
- SpringMVC+Spring+MyBatis配置
今天配置项目时遇到一个问题,tomcat启动没有报错,但是访问页面的时总是报404,后台打印的日志是: 8080-exec-1] WARN springframework.web.servlet.Pa ...
- Eclipse Maven Spring MyBatis 配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- spring+mybatis配置多个数据源
http://www.cnblogs.com/lzrabbit/p/3750803.html
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
随机推荐
- vb编程学习之路之基础与概念总结
OOP (Object Oriented Programming)面向对象程序设计/面向对象编程 对象是代码和数据的集合,对象的三要素:属性.事件.方法 对象的命令规则:必须以字母或汉字开头,不能以数 ...
- springMVC参数传递实例
最好所有的post请求用postman这个工具就好了. postman传对象
- rxjs 常用的静态操作符
操作符文档 API create const { Observable } = require('rxjs'); // 创建 Observables var observable = Observab ...
- CRUD的操作,增删改查!
.注释语法:--,# .后缀是.sql的文件是数据库查询文件 .在创建查询里,那个需要保存的对话框只是,保存查询. .在数据库里面 列有个名字叫字段 行有个名字叫记录 CRUD操作: create 创 ...
- Linux下部署多个Tomcat(完整)
Linux下部署多个Tomcat 1.环境:1.1.Centos 5.01.2.apache-tomcat-6.0.18 2.需要解决一下几个问题2.1.不同的tomcat启动和关闭监听不同的端口2. ...
- 自动化运维工具-mussh工具安装配置及简单使用讲解
1.先决条件: 安装pssh工具的主机针对远程主机需要配置免秘钥认证: ssh-keygen -t rsa ssh-copy-id [remotehost] 2.下载mussh工具安装介质: http ...
- 一个列转行SQL示例(wm_concat函数和replace函数合用)
准备测试数据: create table test01( groupid number, a number, b number, c ...
- malloc函数 链表
https://baike.baidu.com/item/malloc函数 malloc的全称是memory allocation,中文叫动态内存分配,用于申请一块连续的指定大小的内存块区域以void ...
- 一种历史详细记录表,完整实现:CommonOperateLog 详细记录某用户、某时间、对某表、某主键、某字段的修改(新旧值
一种历史详细记录表,完整实现:CommonOperateLog 详细记录某用户.某时间.对某表.某主键.某字段的修改(新旧值). 特别适用于订单历史记录.重要财务记录.审批流记录 表设计: names ...
- 上传本地代码到github&&从github下载源码
最近在玩github,下面简单介绍下githup的使用 将本地代码同步到github. 使用Git GUI同步 1,先下载git,然后安装.右键如图所示. 2,在github里新建一个 reposi ...