Mybatis to open connection pooling and close connection pool performance comparison

Source: Internet
Author: User

1 Creating a database table

--phpMyAdmin SQL dump--version 4.2.11--http://www.phpmyadmin.net----host:localhost--Generation time:2016-08-02 18:1 3:50--Server version: 5.6.21--PHP version:5.6.3set sql_mode = "No_auto_value_on_zero"; Set time_zone = "+00:00";/*!40101 set @[email protected] @CHARACTER_SET_CLIENT */;/*!40101 set @[email  Protected] @CHARACTER_SET_RESULTS */;/*!40101 set @[email protected] @COLLATION_CONNECTION */;/*!40101 set NAMES UTF8 */;----Database: ' mybatis '----------------------------------------------------------------table structure ' Student '-- CREATE TABLE IF not EXISTS ' Student ' (' id ' int (ten) not null, ' name ' varchar (in) not NULL, ' age ' int (4) NOT null) ENGINE =myisam auto_increment=2 DEFAULT charset=latin1;----Dump the data in the table ' Student '--insert into ' Student ' (' id ', ' name ', ' age ') VALUE S (1, ' Zhansan ');----Indexes for dumped tables------Indexes for table ' Student '--alter table ' Student ' ADD PRIMARY KE Y (' id ');----auto_increment for dumped tables------auto_increment for table ' Student '--alTER TABLE ' Student ' MODIFY ' id ' int (ten) not NULL auto_increment,auto_increment=2;/*!40101 SET [email protected]_ Character_set_client */;/*!40101 Set [email protected]_character_set_results */;/*!40101 set [email  protected]_collation_connection * *;

2.   test code as follows

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 (). Getresourceass Tream (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. Open and close the connection pool configuration

<?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"/> <!--Configuring database connection Information--<datasource type= "unpooled"  ;                <!--unpooled is close connection pool, pooled is open connection pool--<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. Comparison results

Insert 10,000 piece of data

Open Connection pool: 6s

Close Connection pool: 36s

Mybatis to open connection pooling and close connection pool performance comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.