DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池
DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池
liuyuhang原创,未经允许禁止转载
系列目录连接
DB数据源之SpringBoot+Mybatis踏坑过程实录(一)
1.环境说明
springboot2.0以下版本,java7,myeclipse2017 C1,使用的是mySql数据库
pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Tit</groupId>
<artifactId>FM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies> <!-- spring boot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>7.0.62</version>
</dependency> <!-- 添加MySQL依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 添加JDBC依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mybaits基础依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.0</version>
</dependency>
<!-- mybatis插件依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mapper依赖 -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.3.7</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency> <!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<scope>true</scope>
</dependency>
<!-- end of 热部署 -->
</dependencies> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version> <webVersion>3.0</webVersion>
</properties>
<build>
<finalName>FM</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin </artifactId>
</plugin>
</plugins> </build>
</project>
2.配置思路
- 确保类单例,使用构造器实例化的sqlSessionFactory只设置一次
- 提供getSqlSessionFactory获取sqlSessionFactory
- setSqlSessionFactory时初始化数据源,并设置连接池
- setSqlSessionFactory方法提供参数可对数据源进行更改,以确保数据源故障时可进行重新设置
3.所需类与结构
3.1.pom,略
3.2.DataConfig.java配置数据源获取SqlSessionFactory类
3.3.mapper.xml,略
3.4.HelloExample.java测试,略
3.5.AppRun.java,Springboot启动类,略
4.代码
DataConfig.java代码如下:
package com.FM.config; import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
/**
* 单例数据源配置
* @author Liuyuhang
*
*/
@Configuration
public class DataConfig { private static final String url = "jdbc:mysql://xxx.xxx.xxx.xxx:3306/dataBaseName?cuseUnicode=true&characterEncoding=utf-8&useSSL=false";
private static final String driver = "com.mysql.jdbc.Driver";
private static final String username = "root";
private static final String password = "root"; /**
* SqlSessionFactory
*/
private SqlSessionFactory sqlSessionFactory = null; /**
* 单例的类对象
*/
private static volatile DataConfig dataConfig; /**
* 无参构造
* @throws Exception
*/
public DataConfig() throws Exception {
setSqlSessionFactory();
System.out.println("DataConfig init");
} /**
* 双验证单例模式
* @return
* @throws Exception
*/
public static DataConfig getInstence() throws Exception{
if(null==dataConfig){
synchronized (DataConfig.class) {
if(null==dataConfig){
dataConfig = new DataConfig(); }
}
}
return dataConfig;
} /**
* tomcat pool配置
* @param url
* @param dirver
* @param username
* @param password
* @return
*/
public DataSource dataSource(String url, String dirver, String username, String password) {
PoolProperties p = new PoolProperties();
p.setUrl(url);
p.setDriverClassName(dirver);
p.setUsername(username);
p.setPassword(password);
p.setTestWhileIdle(false);
p.setTestOnBorrow(true);
p.setValidationQuery("SELECT 1");
p.setTestOnReturn(false);
p.setValidationInterval(30000);
p.setTimeBetweenEvictionRunsMillis(30000);
p.setMaxActive(100);
p.setInitialSize(10);
p.setMaxWait(10000);
p.setRemoveAbandonedTimeout(60);
p.setMinEvictableIdleTimeMillis(30000);
p.setMinIdle(10);
p.setLogAbandoned(true);
p.setRemoveAbandoned(true);
DataSource datasource = new DataSource();
datasource.setPoolProperties(p);
return datasource;
} /**
* setSqlSessionFactory,构造的时候运行一次,可实例化以后也可手动调用修改SqlSessionFactory
* @throws Exception
*/
public void setSqlSessionFactory() throws Exception {
DataSource dataSource = dataSource(url, driver, username, password);
// 创建sessionFactory
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);// 加载数据源
// 扫描mapper.xml
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:com/FM/mapper/*.xml");
factoryBean.setMapperLocations(resources);
// 读取config
factoryBean.setConfigLocation(new DefaultResourceLoader().getResource("classpath:mybatis-config.xml"));
this.sqlSessionFactory = factoryBean.getObject();
System.out.println("setSqlSessionFactory init");
} /**
* 获取SqlSessionFactory
* @return
* @throws Exception
*/
public SqlSessionFactory getSessionFactory() throws Exception {
if (null == sqlSessionFactory) {
setSqlSessionFactory();
}
return this.sqlSessionFactory;
} }
5.说明
- 在不使用连接池情况下,直接加载数据源时,会导致mysql数据库开启连接数量持续增长到最大值,导致mysql数据库无法使用
6.测试
测试时应观察mysql连接数量增长情况,总数量,对数据库进行多次请求。
以上!
DB数据源之SpringBoot+MyBatis踏坑过程(七)手动使用Tomcat连接池的更多相关文章
- DB数据源之SpringBoot+Mybatis踏坑过程实录系列(一)
DB数据源之SpringBoot+MyBatis踏坑过程(一) liuyuhang原创,未经允许进制转载 系列目录 DB数据源之SpringBoot+Mybatis踏坑过程实录(一) DB数据源之Sp ...
- DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描
DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许进制转载 吐槽之后应该有所改了,该方式可以作为一种过渡方式 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描
DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之Spr ...
- DB数据源之SpringBoot+MyBatis踏坑过程(四)没有使用连接池的后果
DB数据源之SpringBoot+MyBatis踏坑过程(四)没有使用连接池的后果 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybatis踏坑过程实 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池
DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量
DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...
- shardingsphere多数据源(springboot + mybatis+shardingsphere+druid)
org.springframeword.boot:spring-boot-starer-web: 2.0.4release io.shardingsphere:sharding-jdbc-spring ...
- Springboot+mybatis中整合过程访问Mysql数据库时报错
报错原因如下:com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone.. 产生这个 ...
- MyBatis数据持久化(七)多表连接查询
本节继续以多表连接查询的案例介绍使用resultMap的好处,对于两张以上的表进行关联查询,当我们有选择的从不同表查询所需字段时,使用resultMap是相当方便的.例如我们有两张表,分别为用户表Us ...
随机推荐
- 1-5 Sass的基本特性-运算
[Sass运算]加法 程序中的运算是常见的一件事情,但在 CSS 中能做运算的,到目前为止仅有 calc() 函数可行.但在 Sass 中,运算只是其基本特性之一.在 Sass 中可以做各种数学计算, ...
- 中国gis100强
广州南方测绘仪器有限公司 北京四维图新科技股份有限公司 北京北斗星通导航技术股份有限公司 高德软件有限公司 北京合众思壮科技股份有限公司 中国地图出版社 广州中海达卫星导航技术股份有限公司 正元地理信 ...
- [转载]hive中order by,sort by, distribute by, cluster by作用以及用法
1. order by Hive中的order by跟传统的sql语言中的order by作用是一样的,会对查询的结果做一次全局排序,所以说,只有hive的sql中制定了order by所有的 ...
- RoadFlowCore工作流2.8.1 更新日志
1.2.8.1更新了2.8刚发布的一些小BUG. 2.2.8.1增加了移动端,基于微信企业号或企业微信. 详细请参阅官方网站:roadflow.net
- 【Python】多重赋值之值互换
右边的值先确定,然后再开始向左赋值 s = 1 t = 2 s,t = t,s print s print t >>> 2 >>> 1 区分 s = t t = s ...
- js 显示 base64编码 的二进制流 图片
Data URI scheme.Data URI scheme是在RFC2397中定义的,目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入.比如上面那串字符,其实是一张小图片,将这些字 ...
- BEM,SASS,LESS,bootstrap:如何有效地将这些方法,工具和框架聪明地整合?
https://medium.com/@andersonorui_/bem-sass-and-bootstrap-9f89dc07d20f Bootstrap是一个“HTML,CSS和Javascri ...
- 如何让LoadRunner实现多个场景运行?
如何让LoadRunner实现多个场景运行? 发布时间: 2013-11-29 10:59 作者: stevenlee 来源: 51Testing软件测试网博客 字体: 小 中 大 ...
- 【Leetcode】【Easy】Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- git error:【fatal: unable to access 'https://github.com/userId/prjName.git/': err or setting certificate verify locations:】
$ git pull origin master fatal: unable to access 'https://github.com/userId/prjName.git/': err or se ...