Spring Transaction Management

Source: Internet
Author: User

What is a transaction, many from only the beginning of Java friends certainly do not know this concept, after all, spring is for enterprise application development is the product of the Java EE specification, so a lot of things, beginners are certainly not heard of.

Because I used to be a web dog. SQL injection is a self-cooked, so the concept of transaction, but it is actually a concept of database operations, whether it is an ORM or JDBC data source, its own database operations are SQL to implement,

SQL mentions a very important concept, that is, the transaction, because the SQL command CRUD database is likely to go wrong, so you have to start a transaction, and then after modifying the query, to determine the success of the modification before committing the transaction, this time the database will persist the data to

Drive, or you can discard the transaction, and the modification will not be persisted to the hard disk.

For the transaction configuration, I read the video in Mu class, better understand and easier to configure is the annotation, here I put the annotated declaration code

 PackagePw.jonwinters.demo4;Importorg.springframework.transaction.annotation.Isolation;Importorg.springframework.transaction.annotation.Propagation;Importorg.springframework.transaction.annotation.Transactional;//The propagation behavior isolation level of transactions that are configured for transactions after the annotation is logged off, the transaction is not rolled back and the method for database operations is persisted before the exception is generated .@Transactional (propagation=propagation.required,isolation=Isolation.default) Public classAccountserviceimplImplementsAccountservice {//@Autowired    PrivateAccountdao Accountdao;  Public voidSetaccountdao (Accountdao Accountdao) { This. Accountdao =Accountdao; } @Override Public voidTransferFinalString out,FinalString in,FinalDouble Money)        {Accountdao.outmoney (out, money); //int result = 1/0; Comment out exceptions except 0
Accountdao.inmoney (in, money);//Because there are annotations above, this class will be proxied, even if an exception occurs, the above database-specific operation will not be persisted
} }
  <BeanID= "TransactionManager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager">     < Propertyname= "DataSource"ref= "DataSource"></ Property> </Bean>  <Tx:annotation-drivenTransaction-manager= "TransactionManager" /> 

Above is configure transaction management, then turn on default note driver scan, configure transaction manager for Datasourcetransactionmanager

Spring Transaction Management

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.