Mybatis 打开连接池和关闭连接池性能对比

1  创建数据库表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- phpMyAdmin SQL Dump
-- version 4.2.11
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: 2016-08-02 18:13:50
-- 服务器版本: 5.6.21
-- PHP Version: 5.6.3
 
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
 
 
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
 
--
-- Database: `mybatis`
--
 
-- --------------------------------------------------------
 
--
-- 表的结构 `Student`
--
 
CREATE TABLE IF NOT EXISTS `Student` (
`id` int(10) NOT NULL,
  `name` varchar(256) NOT NULL,
  `age` int(4) NOT NULL
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
 
--
-- 转存表中的数据 `Student`
--
 
INSERT INTO `Student` (`id`, `name`, `age`) VALUES
(1, 'zhansan', 20);
 
--
-- Indexes for dumped tables
--
 
--
-- Indexes for table `Student`
--
ALTER TABLE `Student`
 ADD PRIMARY KEY (`id`);
 
--
-- AUTO_INCREMENT for dumped tables
--
 
--
-- AUTO_INCREMENT for table `Student`
--
ALTER TABLE `Student`
MODIFY `id` int(10) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

2.  测试代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package mybatisInsertDataDemo;
 
import java.io.InputStream;
 
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
import mybatisInsertDataDemo.Student;
import mybatisInsertDataDemo.demo;
 
public class demo {
     
    public static void main(String[] args) {
         
        String resource = "mybatisInsertDataDemo/conf.xml";
        InputStream is = demo.class.getClassLoader().getResourceAsStream(resource);
         
        if(null == is)
        {
            System.out.println("inputstream is null");
            return;
        }
         
         
         SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
         
          
          
          
         long start = System.currentTimeMillis();
          
         for(int i=0;i<10000;i++)
         {
             SqlSession session = sessionFactory.openSession();
             
             String statement = "mybatisInsertDataDemo.studentMapper.insertStudent";
              Student s = new Student();
             s.setAge(20);
             s.setName("lisi");
              
             session.insert(statement, s);
              
             session.close();
         }
          
         System.out.println("waste time  "+(System.currentTimeMillis()-start));
          
          
 
    }
}

 3.  打开与关闭连接池 配置

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 配置数据库连接信息 -->
            <dataSource type="UNPOOLED">  <!--UNPOOLED 是关闭连接池,POOLED 是打开连接池-->
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
                <property name="username" value="root" />
                <property name="password" value="" />
            </dataSource>
        </environment>
    </environments>
     
    <mappers>
        <mapper resource="mybatisInsertDataDemo/studentMapper.xml"/>
    </mappers>
     
</configuration>

  

4. 对比结果

插入一万条数据

打开连接池:  6s

关闭连接池:  36s

源码下载: http://download.csdn.net/detail/mtour/9594178

 

posted @   孤独的和弦  阅读(3482)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示