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. alert 多语言的处理

    1. code <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <m ...

  2. sauvola二值化算法研究

    sauvola二值化算法研究   sauvola是一种考虑局部均值亮度的图像二值化方法, 以局部均值为基准在根据标准差做些微调.算法实现上一般用积分图方法 来实现.这个方法能很好的解决全局阈值方法的短 ...

  3. Java中由Calendar类获取的月、天和小时的简单处理

    在Java中,Calendar是日期处理的一个重要的类.但是,我们使用Calendar获取的月份,天,小时等可能需要进行简单的处理才能满足我们的需要.比如,月份范围是0-11,而我们可能需要的是1-1 ...

  4. 【Python】面向对象编程思想

    概念 "笔"作为一个抽象的概念,可以被看成是一个类.而一支实实在在的笔,则是"笔"这种类型的对象. 一个类可以有属于它的函数,这种函数被称为类的"方法 ...

  5. idea整个项目乱码解决办法

    idea前几天还好好的,这两天一直出现乱码.不止一个文件,是整个项目都有这个问题.想想这几天什么也没做啊... 上网搜了下 解决. 经过多次排查寻找,终于,在项目的目录下有个.idea的文件夹,这个文 ...

  6. Vs2013 & .net framework 4.5.1 预览介绍

    微软发布了vs2013 preview 和fw4.5.1 下面简单介绍一下与大家共享 Developer productivity X64 edit and continue 在2013里面 可以在x ...

  7. delphi之socket通讯

    使用了2个组建: TServerSocket TClientSocket ------------------TServerSocket--------------------------- //开启 ...

  8. MyEclipse打开JSP文件报"Failed to create the part's controls"解决方法汇总

    有时候,打开别人的开发环境中导过来的项目的JSP文件,会出现“Failed to create the part's controls”的错误! 解决的方法有: 方法1:关闭myeclipse的jsp ...

  9. 小Y的轮回之路——攒机装机、B150装win7

    两个月前,陪伴我5年多的小Y(ideapad-y460N卡)突然大伤元气,硬盘跪了,显示屏也黑了一小块.本着经济实惠凑合用的态度换了个320G的硬盘,没想过几天显示屏情况加重,出现无数个红绿相间的线条 ...

  10. mongodb 3.4复制集详解

    1关闭数据库,打开三个mongodb数据库数据库实例 rs.printReplicationInfo() 2:原理 主库能够进行读写操作,一个复制集群只能有一个活跃的主库 一般情况下复制可以分为好几种 ...