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连接池的更多相关文章

  1. DB数据源之SpringBoot+Mybatis踏坑过程实录系列(一)

    DB数据源之SpringBoot+MyBatis踏坑过程(一) liuyuhang原创,未经允许进制转载 系列目录 DB数据源之SpringBoot+Mybatis踏坑过程实录(一) DB数据源之Sp ...

  2. DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描

    DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许进制转载  吐槽之后应该有所改了,该方式可以作为一种过渡方式 ...

  3. DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描

    DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载    系列目录连接 DB数据源之Spr ...

  4. DB数据源之SpringBoot+MyBatis踏坑过程(四)没有使用连接池的后果

    DB数据源之SpringBoot+MyBatis踏坑过程(四)没有使用连接池的后果 liuyuhang原创,未经允许禁止转载  系列目录连接 DB数据源之SpringBoot+Mybatis踏坑过程实 ...

  5. DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池

    DB数据源之SpringBoot+MyBatis踏坑过程(五)手动使用Hikari连接池 liuyuhang原创,未经允许禁止转载  系列目录连接 DB数据源之SpringBoot+Mybatis踏坑 ...

  6. DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量

    DB数据源之SpringBoot+MyBatis踏坑过程(六)mysql中查看连接,配置连接数量 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之SpringBoot+Mybati ...

  7. shardingsphere多数据源(springboot + mybatis+shardingsphere+druid)

    org.springframeword.boot:spring-boot-starer-web: 2.0.4release io.shardingsphere:sharding-jdbc-spring ...

  8. Springboot+mybatis中整合过程访问Mysql数据库时报错

    报错原因如下:com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone.. 产生这个 ...

  9. MyBatis数据持久化(七)多表连接查询

    本节继续以多表连接查询的案例介绍使用resultMap的好处,对于两张以上的表进行关联查询,当我们有选择的从不同表查询所需字段时,使用resultMap是相当方便的.例如我们有两张表,分别为用户表Us ...

随机推荐

  1. asp.net core 错误定位 & vs2017 远程调试部署在centos上的asp.net core程序

        前言 程序运行中会出现各种BUG. 排除BUG有三种方式. 一.访问页面直接报错误信息 出于安全,服务器是关闭这个功能的.在centos上可以用 命令设置环境变量来解决:   export A ...

  2. python anaconda 安装 环境变量 升级 以及特殊库安装

    Anaconda 是一个旗舰版的python安装包, 因为普通的python没有库, 如果需要安装一些重要的库, 要经常一个一个下载,会非常麻烦. 所以这个一个集成的, 可以手动批量升级的软件. 而且 ...

  3. CSS属性: 阴影 轮廓 渐变

    注: 本文摘自 宁静致远 - CSDN / 但愿人长久 千里共婵娟 - CSDN 阴影 使用box-shadow属性可以为元素添加阴影效果, 比如 box-shadow: h-shadow v-sha ...

  4. ImageNet download

    Download Original Images Note: On Feb 8, 2014, our terms of access changed along with the APIs/URLs ...

  5. 20.远程分支&跟踪分支

    远程分支 远程引用是对远程仓库的引用(指针),包括分支.标签等等. 你可以通过 git ls-remote (remote) 来显式地获得远程引用的完整列表,或者通过 git remote show ...

  6. Studying TCP's Throughput and Goodput using NS

    Studying TCP's Throughput and Goodput using NS What is Throughput Throughput is the amount of data r ...

  7. ubuntu16.04下无法连接网络的bug

    首先介绍下Bug的情况,这个bug纠缠我整整一天,在命令行下ifconfig能够看到ip地址,不过我的不是eth0,而是enps03,然后Ping 本机和ping 网关都能够 ping 通,但是sud ...

  8. 【转】PBOC3.0和PBOC2.0标准规范异同分析

    2013年2月,中国人民银行发布了<中国金融集成电路(IC)卡规范(V3.0)>(以下简称PBOC3.0),PBOC3.0是在中国人民银行2005年颁布的<中国金融集成电路(IC)卡 ...

  9. shell逻辑运算符 1

    逻辑卷标 表示意思 1. 关于档案与目录的侦测逻辑卷标! -f 常用!侦测『档案』是否存在 eg: if [ -f filename ] -d 常用!侦测『目录』是否存在 -b 侦测是否为一个『 bl ...

  10. Jenkins安装 CentOS 7上安装Jenkins

    CentOS 7上安装Jenkins Jenkins 安装 只安装不介绍 步骤1:更新CentOS 7 Linux系统管理员的最佳做法之一是使系统保持最新.安装最新的稳定包,然后重新启动.   1 2 ...