Ibatis framework to do batch processing problems

Source: Internet
Author: User

Recently, a colleague's project used Spring+ibatis as the data Persistence layer framework for MySQL data storage. Soon after the project was released, serious performance problems were encountered, and it was decided to submit the data in a batch manner. My previous article: in the detailed introduction of the Ibatis framework to do batch processing and possible problems and performance optimization, so colleagues refer to this biased article on the project has been transformed, the introduction of the batch processing mechanism. But things are not smooth, in the case of only 2000 data, with the Ibatis framework to do batch processing and do not do batch the time spent incredibly no difference, on average, about 50 seconds. And when I do the test, it only takes 1 seconds, or even less, to do batch. What the hell, is spring doing this? Because the only difference between my test and my colleague's project is that I'm using Ibatis's sqlmapclient directly, and my colleague's project is using spring to get an instance of sqlmapclient. Carefully check the friend's data source configuration, also did not find any problems. is as follows:

 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/test"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <bean id="sqlMapClient"
        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="SqlMapConfig.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

In helpless, a question suddenly came to mind: if the autocommit attribute of JDBC is set to true, it is futile to write a program in a batch way, and each statement executes directly. So with the attitude of trying to carry out the following statement:

System.out.println ("Current state of affairs:" + Sqlmapclient.getdatasource (). getconnection (). Getautocommit ()); As expected, the output value is: True, no wonder batch is not available! The problem has been found, how to solve it? Try to set the Autocommit property to false manually before the batch statement is executed, such as: Sqlmapclient.getdatasource (). getconnection (). Setautocommit (false); So I looked at Org.apache.commons.dbcp.BasicDataSource's source code and found that its Defaultautocommit property default value is True, which is no wonder that spring gets the Autocommit property of the Sqlmapclient True, now that the problem is found, it is much easier to solve, just add one line to the datasource configuration above: <property name= "Defaultautocommit" value= "false"/> on it.

Here to summarize: Ibatis default Autocommit property is False, so the Ibatis framework to do batch processing when the basic will not encounter any problems, in the spring environment, to see the specific datasource configuration.

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.