An ibatis framework for Batch Processing

Source: Internet
Author: User

Recently, a colleague's project uses spring + ibatis as the data persistence layer framework and MySQL data storage. Shortly after the project was released, it encountered a serious performance problem. Therefore, we decided to submit data in batch mode. My previous article:Ibatis framework Batch Processing OptimizationThis section describes in detail how the ibatis framework performs batch processing, as well as possible problems and performance optimization. Therefore, my colleagues refer to this article to rebuild the project and introduce the batch processing mechanism. However, the process was not smooth. In the case of only 2000 pieces of data, the time consumed by batch and no batch was no different. The average time required was about 50 seconds. It takes only one second or even less time for me to perform a test. It's a ghost. Is spring a ghost? The only difference between my tests and my colleagues' projects is that I directly use the sqlmapclient of ibatis, while my colleagues' projects use spring to obtain sqlmapclient instances. I checked my friend's data source configuration carefully and did not find any problems. 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>

When I was helpless, I suddenly thought of a problem: If the autocommit attribute of JDBC is set to true, it would be futile to write programs in batch mode, each statement is executed directly. So I tried to execute the following statement:

System. Out. println ("current transaction status:" + sqlmapclient. getdatasource (). getconnection (). getautocommit ());

As expected, the output value is true. No wonder batch is unavailable! How can I solve the problem? Manually set the autocommit attribute to false before executing the batch statement, for example, sqlmapclient. getdatasource (). getconnection (). setautocommit (false); the result is not useful, so I checked Org. apache. commons. DBCP. the source code of basicdatasource finds that its default defaultautocommit attribute is true. It is no wonder that the autocommit attribute of sqlmapclient obtained by spring is true. Since the problem is found, it is much easier to solve it, you only need to add a line in the datasource configuration above: <property name = "defaultautocommit" value = "false"/>.

Here, we will summarize: the default autocommit attribute of ibatis is false, so there is basically no problem during batch processing. In the spring environment, we need to check 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.