Spring Transaction Note 3.1

Source: Internet
Author: User

Spring transaction

The previous transaction was a programmatic transaction that needed to be turned on and off, and then the program was written here

Spring, declarative transactions

Spring Transaction ISOLATION LEVEL

Default use database defaults isolation level
Read_uncommitted allows reading of data that has not yet been committed. may result in dirty reads, Phantom reads, or non-repeatable reads.
Read_committed allows reading from a concurrent transaction that has already been committed. You can prevent dirty reads, but there will still be phantom reads and non-repeatable reads.
Repeatable_read multiple reads for the same field are the same, unless the data is changed by the current transaction. can prevent dirty reads and non-heavy
Repetition, but the phantom reads still appear.
The SERIALIZABLE fully complies with the ACID isolation level, ensuring that no dirty reads, Phantom reads, and non-repeatable reads occur.


Dirty reads: One transaction reads data that is not committed to another transaction.
Non-repeatable READ: In the same transaction, multiple reads of the same data return different results.
Phantom read: One transaction reads a transaction that has been committed by another transaction.

==============================================

Spring Transaction Propagation Properties

REQUIRED
the business method needs to run in a transaction. If the method is running and is already in a transaction, then join the transaction,
Otherwise, create a transaction yourself. (used in most cases)
not-supported
declaring a method requires a transaction. If the method is not associated to a transaction, the container will open a transaction for it if the method
called in a transaction, the transaction is suspended and the original transaction resumes execution after the method call ends.
requirednew
The business method must run in its own transaction. A new transaction will be started, and if a transaction is running,
The transaction is suspended, and after the method finishes running, the new transaction finishes and the original transaction resumes running.
MANDATORY The method must be running in an existing transaction, itself cannot create a transaction, if the method is called in an environment without transactions ,
the exception is thrown.
SUPPORTS
If the method is running in a transactional environment, it is run in this transaction, and if it is called outside the scope of the transaction, then the
run in an environment without transactions.
never
indicates that the method cannot run in a transactional environment and throws an exception if it is called in an environment where the transaction is running
NESTED
If an active transaction exists, it is run in a nested transaction, and if there is no active transaction, follow the required
implementation of the service mode. The transaction can be committed or rolled back independently if the rollback does not affect the perimeter transaction

=========================================

Spring transactions appear on the service layer

1. Establishing the JDBC transaction manager

<id= "TransactionManager"  class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager ">        <  name = "DataSource" ref = "DataSource" />    </ Bean >    

2. XML configuration-based transactions need to be imported schema

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"<!--This sentence-->xmlns:tx= "Http://www.springframework.org/schema/tx" xsi:schemalocation= "http://www.springframework.org/sc Hema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/ AOP Http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/context http:/ /www.springframework.org/schema/context/spring-context.xsd<!--this sentence -http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">

Transaction notification

<!--Transaction Notification transaction-manager= "TransactionManager" is the same as the configuration ID of the transaction manager above -        <Tx:adviceID= "Advice"Transaction-manager= "TransactionManager">        <tx:attributes>            <!--method refers to which methods in the service class need to add transactions, and which transaction isolation isolation level propagation propagate property find* means as long as the find is open The head of all add transactions, when queried read-only= "true" means read-only, high performance -            <Tx:methodname= "save*"Isolation= "DEFAULT"Propagation= "REQUIRED"/>            <Tx:methodname= "find*"read-only= "true"/>            <Tx:methodname= "eidt*"/>            <Tx:methodname= "del*"/>        </tx:attributes>    </Tx:advice>    
<!--What classes are added to AOP notifications -    <Aop:config>        <Aop:pointcutexpression= "Execution (* com.kaishengit.service). *.*(..))"ID= "PT"/>        <!--The notification reference above ID advice-ref= "advice" pointcut-ref= "PT" above the ID -        <Aop:advisorAdvice-ref= "Advice"Pointcut-ref= "PT"/>    </Aop:config>

Example

@Named Public classStudentservice {@InjectPrivateStudentdao Studentdao;  Public voidSave (Student Stu) {studentdao.save (STU); if(1==1){            Throw Newruntimeexception ();    } studentdao.save (Stu); }             PublicStudent FindByID (intID) {returnStudentdao.findbyid (ID); }}            

============================================

2. Transactions based on annotation

JDBC Transaction manager

<id= "TransactionManager"  class= " Org.springframework.jdbc.datasource.DataSourceTransactionManager ">        <  name = "DataSource" ref = "DataSource" />    </ Bean >

Annotation-based transactions

<transaction-manager= "TransactionManager"/>

Example

@Named @transactional Public classStudentservice {@InjectPrivateStudentdao Studentdao;  Public voidSave (Student Stu) {studentdao.save (STU); if(1==1){            Throw Newruntimeexception ();    } studentdao.save (Stu); } @Transactional (ReadOnly=true)     PublicStudent FindByID (intID) {returnStudentdao.findbyid (ID); }}    


Transactions are rolled back when an exception occurs, if a method in DAO is called in the service, in DAO
If the direct try catch is dropped, it is not rolled back, and with spring, the SQL executed by default is throwing up the run-time exception. That means
Spring defaults to rollback only if a run-time exception occurs
if (1==1) {
throw new Exception ();
}

But it can be modified.

@Named/*The default is @Transactional (Rollbackfor=runtimeexception.class)*/@Transactional (rollbackfor=exception.class) Public classStudentservice {@InjectPrivateStudentdao Studentdao;  Public voidSave (Student Stu) {studentdao.save (STU); if(1==1){            Throw Newruntimeexception ();    } studentdao.save (Stu); } @Transactional (ReadOnly=true)     PublicStudent FindByID (intID) {returnStudentdao.findbyid (ID); }}        

Spring Transaction Note 3.1

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.