Spring video learning (10) use XML to configure transactions

Source: Internet
Author: User

1. xml configuration

 <! -- Configure the Transaction Manager -->   <  Bean   ID = "Txmanager"   Class = "Org. springframework. JDBC. datasource. datasourcetransactionmanager"  >         <  Property   Name = "Datasource"   Ref = "Datasource" />       </  Bean  >      <! -- Configure the service Bean -->     <  Bean   ID = "Personservice"   Class = "Com. Persia. Service. impl. personserviceimpl"  >      <  Property   Name = "Ds"  Ref = "Datasource"  >  </  Property  >    </  Bean  >          <! -- Use XML for Transaction Management -->    <  AOP : Config  >        <! -- Configure a plane and the classes and methods to be intercepted -->         < AOP : Pointcut   ID = "Transactionpointcut"   Expression = "Execution (* COM. Persia. Service ..*.*(..))"  />        <  AOP : Advisor   Advice - Ref = "Txadvice"   Pointcut - Ref = "Transactionpointcut"  />    </  AOP : Config  >   <! -- Configure a transaction notification -->      <  TX : Advice   ID = "Txadvice"   Transaction - Manager = "Txmanager" >          <  TX : Attributes  >         <! -- The method starts with get and does not use transactions -->           <  TX : Method   Name = "Get *"   Read - Only = "True"   Propagation ="Not_supported"  />         <! -- Other methods are performed with default transactions -->           <  TX : Method   Name = "*"  />          </  TX : Attributes  >    </  TX : Advice >   

2. No annotations are required for the service bean.

 
Public VoidDelete (integer ID ){// Todo auto-generated method stubJdbctemplate. Update ("Delete from person where id =?",NewObject [] {ID },New Int[] {Java. SQL. types. Integer}); jdbctemplate. Update ("Delete from person1 where id =?",NewObject [] {ID },New Int[] {Java. SQL. types. Integer });// Throw new runtimeexception ("exception during runtime ");}

3. junit4 Testing

@ TestPublic VoidTestdelete () {ps. Delete (5 );}
4. Conclusion: It can be seen that the xml configuration transaction plays a role, but also uses AOP to add a transaction to the method when blocking the method. .

We recommend that you use annotations to use transactions.

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.