Spring Practical Note 5---transaction management

Source: Internet
Author: User

Transaction management (transaction management rules, before committing, if you modify the database will be a real change, but will leave a cache, in the event of a transaction error, according to the configuration of the transaction to do the corresponding rollback operation, in the rollback operation also from the data and database data synchronization, The operation to modify the database does not occur at the time of submission, and is estimated for efficiency reasons.


In the field of software development, an all-or-nothing operation is called a transaction


A transaction is represented by four words: atomicity consistency Isolation persistence (transaction completion, results will persist, mostly involving database operations)



Understand spring's support for transaction management


Spring separates the actual transaction implementation from transactional code through a callback mechanism, and spring supports both coded and declarative transactions, and encoded transactions can surprise control transactions, while declarative transactions only support adding transactions on methods


The main introduction of declarative transactions, encoded transactions like JDBC Data template, you need to actually execute the code contained in the transaction template.


Spring's transaction manager


The purpose of declaring spring's transaction manager Bean is to tell Spring that it will start the transaction management model in that way, which only describes the underlying hibernate transaction manager


If the application's persistent words are implemented through hibernate, then you need to use

Hibernatetransactionmanager

I am using Hibernate4 so I need to declare the following bean in the application context of SPIRNG


<bean id="TransactionManager"

class ="Org.springframework.orm.hibernate4.HibernateTransactionManager">

<propertyname="Sessionfactory"ref=" Sessionfactory "></property>

</bean>


This requires a sessionfactory as the entry for the transaction manager.


Declarative transactions


Transaction Properties: Propagation behavior (propagation) Isolation level (isolation) whether read-only (read-only) transaction timeout (timeout) transaction rollback (Rollback-for no-rollback-for)


Propagation behavior:

1. Propagation_mandatory indicates that the method must run in a transaction and throws an exception if the current transaction does not exist.

2. Propagation_nested indicates that the method will run in a nested transaction if a transaction already exists. Nested transactions can be individually committed or rolled back independently of the current transaction.

3. Propagation_never indicates that the current method should not run in the transaction context and throws an exception if a transaction is currently running.

4. Propagation_not_supported indicates that the method should not run in a transaction, and if there is a current transaction, the current transaction will be suspended while the method is running.

5. Propagation_required indicates that the current method must be running in a transaction, and if the current transaction exists, the method will run in that transaction, or a new transaction will be started.

6. Propagation_required_new indicates that the current method must be running in his own transaction, and a new transaction will be started. If there is a current transaction, the current transaction is suspended during the execution of the method.

7. Propagation_supports indicates that the current method does not require a transaction context, but if there is a current transaction, the method will run in this transaction


Isolation level:

1. Isolation_default using the default isolation level of the back-end Database

2. isolation_read_uncommitted allows reading of data changes that have not yet been committed. May cause dirty read or non-repeatable reads

3. isolation_read_committed allows reading of data that has been committed by concurrent transactions and can prevent dirty reads, but phantom or non-repeatable reads can still occur

4. Isolation_repeatable_read is consistent with multiple read results for the same field, unless the data is modified by the transaction itself

5. isolation_serializable complete compliance with acid isolation levels, ensuring that dirty reads are blocked, non-repeatable reads and Phantom reads


<beans xmlns="Http://www.springframework.org/schema/beans"

Xmlns:xsi ="Http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http// Www.springframework.org/schema/context "

XMLNS:AOP ="Http://www.springframework.org/schema/aop"xmlns:tx="http// Www.springframework.org/schema/tx "

xsi:schemalocation ="Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-3.0.xsd

Http://www.springframework.org/schema/aop

Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

Http://www.springframework.org/schema/tx

Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

...

</beans>


<tx:advice id="Txadvice">

<tx:attributes>

<tx:methodName="add*"propagation="REQUIRED" />

<tx:methodName="update*"propagation="REQUIRED" />

<tx:methodName="delete*"propagation="REQUIRED" />

<tx:methodName="*" propagation="SUPPORTS" read-only="true"/>

</tx:attributes>

</tx:advice>

It is also possible to set isolation timeout rollback-for no-rollback-for These properties but the rollback property does not yet know the need to set the genetic style, the first feeling should be the full name of the exception class


Define the tangency point of the transaction, and note that the ASPECTJ in the tangent expression "*" will add a space after the


<aop:config>

<aop:pointcut id="User"

expression ="Execution (* com.newblog.dataresourcedao.userdao.* (..))" />

<aop:pointcut id="blog"

expression ="Execution (* com.newblog.dataresourcedao.blogdao.* (..))" />

<aop:pointcut id="comment"

expression ="Execution (* com.newblog.dataresourcedao.picturedao.* (..))" />

<aop:pointcut id="Picture"

expression ="Execution (* com.newblog.dataresourcedao.blogdao.* (..))" />

<aop:advisor pointcut-ref="user"advice-ref="Txadvice" />

<aop:advisor pointcut-ref="blog"advice-ref=" Txadvice "/>

<aop:advisor pointcut-ref="comment"advice-ref=" Txadvice "/>

<aop:advisor pointcut-ref="picture"advice-ref="Txadvice" />

</aop:config>

Spring Practical Note 5---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.