zoukankan      html  css  js  c++  java
  • Mybatis 打开连接池和关闭连接池性能对比

    1  创建数据库表

    -- 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.  测试代码如下

    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.  打开与关闭连接池 配置

    <?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

  • 相关阅读:
    准确率99%!基于深度学习的二进制恶意样本检测——瀚思APT 沙箱恶意文件检测使用的是CNN,LSTM TODO
    借贷宝有多少人看得懂?借贷宝系统崩溃分析
    武汉Uber优步司机奖励政策
    杭州优步uber司机第三组奖励政策
    杭州优步uber司机第二组奖励政策
    杭州优步uber司机第一组奖励政策
    优步北京B组(8月10日-8月16日奖励规则)
    滴滴快车奖励政策,高峰奖励,翻倍奖励,按成交率,指派单数分级(8月4日)
    关于借贷宝“骗钱、推广是假的、传销、恶意盗取用户信息等”谣言的澄清
    借贷宝谣言制造者孟某承认造谣并公开致歉
  • 原文地址:https://www.cnblogs.com/mtour/p/5735242.html
Copyright © 2011-2022 走看看