Spring Programming Transaction Control

Source: Internet
Author: User

Step1: Configuration XML file

1 <!--Transaction Management Bean -2   <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">3     < Propertyname= "DataSource"ref= "DataSource" />4   </Bean>5   6     <BeanID= "Transactiontemplate"7 class= "Org.springframework.transaction.support.TransactionTemplate">8         < Propertyname= "TransactionManager">9             <refBean= "TransactionManager"/>Ten         </ Property> One     </Bean> A  -   <!--The driver for @transactional this annotation, which is the annotation-based way of using the transaction configuration declaration - -   <Tx:annotation-drivenTransaction-manager= "TransactionManager" />

Step2: Automatic injection of Transactiontemplate objects

1 Import org.springframework.transaction.support.TransactionTemplate;     2 @Autowired 3 Private Transactiontemplate transactiontemplate;

Step3: Managing transactions by using the Execute method

1Transactiontemplate.execute (NewTransactioncallbackwithoutresult () {2                 3 @Override4                 protected voidDointransactionwithoutresult (Transactionstatus arg0) {5String s =Userservice.newuser (User,currentuserid);6User newuser=Userservice.getuser (User.getusername ());7 Roleservice.createroleuser (Newuser.getuserid (), rolesid);8                 }9});

The complete process is as follows:

1  PackageCom.superb.mips.terminal.rest.controller;2 3 ImportJava.util.HashMap;4 5 Importorg.apache.commons.lang3.StringUtils;6 Importorg.springframework.beans.factory.annotation.Autowired;7 ImportOrg.springframework.stereotype.Controller;8 ImportOrg.springframework.transaction.TransactionStatus;9 Importorg.springframework.transaction.annotation.Transactional;Ten ImportOrg.springframework.transaction.support.TransactionCallbackWithoutResult; One Importorg.springframework.transaction.support.TransactionTemplate; A Importorg.springframework.web.bind.annotation.RequestMapping; - ImportOrg.springframework.web.bind.annotation.ResponseBody; -  the ImportCom.superb.mips.system.domain.User; - ImportCom.superb.mips.system.service.inf.RoleService; - ImportCom.superb.mips.system.service.inf.UserService; -  +  - @Controller +@RequestMapping ("Api/users") A  Public classRestusercontroller { at @Autowired -     PrivateUserService UserService; -&orgname= China Mobile &orgid=1 - @Autowired -     PrivateRoleService RoleService; - @Autowired in     Privatetransactiontemplate transactiontemplate; -  to@RequestMapping (value = "/createusers.json") + @ResponseBody - @Transactional the      PublicHashmap<string, Object> CreateUser (FinalUser User,Final intCurrentuserid,FinalString Rolesid) { *hashmap<string, object> map =NewHashmap<string, object>(); $String[] Strary=rolesid.split (",");Panax Notoginseng         if(strary.length==0){ -Map.put ("Result", 1); theMap.put ("description", "Bind role"); +             returnmap; A}Else{ the              for(inti=0;i<strary.length;i++){ +                 Try { - Integer.parseint (Strary[i]); $}Catch(Exception e) { $Map.put ("Result", 2); -Map.put ("description", "Please bind the correct role ID"); -                     returnmap; the                 } -             }Wuyi         } the         Try { -Transactiontemplate.execute (NewTransactioncallbackwithoutresult () { Wu                  - @Override About                 protected voidDointransactionwithoutresult (Transactionstatus arg0) { $String s =Userservice.newuser (User,currentuserid); -User newuser=Userservice.getuser (User.getusername ()); - Roleservice.createroleuser (Newuser.getuserid (), rolesid); -                 } A             }); +Map.put ("result", 0); theMap.put ("description", "New user success.") "); -}Catch(Exception e) { $Map.put ("Result", 3); theMap.put ("description", "New user failed!") "); the         } the          the         returnmap; -     } in}

Spring Programming Transaction Control

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.