Ibatis performs batch operations (including transactions)

Source: Internet
Author: User

 

Public class test1 {/*** use ibatis for batch operations * @ Param ARGs */public static void main (string [] ARGs) throws exception {// ibatis processes reader = resources. getresourceasreader ("sqlmapconfig. XML "); sqlmapclient = sqlmapclientbuilder. buildsqlmapclient (Reader); reader. close (); // generate the Object List <user> List = new arraylist <user> (); For (INT I = 1; I <= 100; I ++) {user u = new user (); U. setname ("user" + I); U. setage (10 + I); U. setsex ("M"); list. add (U);} // batch operation, save 20 records each time, and use the transaction long T1 = system. currenttimemillis (); For (INT I = 1; I <= List. size (); I ++) {if (I % 20 = 0) {updatebatch (sqlmapclient, list. sublist (I-20, I), I/20) ;}} long T2 = system. currenttimemillis (); // when performing a computing operation, you can remove the transaction and compare it with system. out. println (T2-t1);}/*** @ Description: Use ibatis for batch operations, and use the transaction * The first two transactions of this method normally end; when the third transaction is halfway through, an exception is thrown, check whether the transaction is rolled back ** @ Param sqlmapclient * @ Param users the user set to be saved to the database * @ Param count method execution times * @ throws exception, an exception is thrown when the method is executed for the third time and the tenth data record is operated */Private Static void updatebatch (sqlmapclient, list <user> Users, int count) throws exception {try {// transaction starts sqlmapclient. starttransaction (); // start sqlmapclient in batches. startbatch (); For (INT I = 0; I <users. size (); I ++) {// throw an exception if (COUNT = 3 & I = 10) throw new runtimeexception (); // Save the data sqlmapclient. insert ("saveuser", users. get (I);} // batch operation execution sqlmapclient.exe cutebatch (); // transaction commit sqlmapclient. committransaction ();} catch (exception e) {Throw E;} finally {try {// sqlmapclient ends the transaction. endtransaction ();} catch (sqlexception e) {e. printstacktrace ();}}}}

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.