Switch: Spring declarative transaction

Source: Internet
Author: User

1. Declarative transaction Configuration
* Configure sessionfactory
* Configure the Transaction Manager
* Propagation Features of transactions
* The methods of those classes use transactions.

2. Compile the business logic method
* Inherits the hibernatedaosupport class and uses hibernatetemplate for persistence. The hibernatetemplate is
Lightweight encapsulation of hibernate sessions
* By default, runtime exceptions will be rolled back (including inheriting the runtimeexception subclass). Normal exceptions will not be rolled back.
* When writing the business logic method, it is best to throw the exception up and handle it in the presentation layer (struts ).
* The transaction boundary settings are usually set to the business layer and not to the Dao.

3. Understand the propagation characteristics of transactions
1. propagation_required: if a transaction exists, the current transaction is supported. Enable if no transaction exists
2. propagation_supports: if a transaction exists, the current transaction is supported. If no transaction exists, the transaction is not executed.
3. propagation_mandatory: if a transaction already exists, the current transaction is supported. If no active transaction exists, an exception is thrown.
4. propagation_requires_new: always starts a new transaction. If a transaction already exists, the transaction will be suspended.
5. propagation_not_supported: Always executes non-transactions and suspends any existing transactions.
6. propagation_never: Always executes non-transactional operations. If an active transaction exists, an exception is thrown.
7. propagation_nested: if an active transaction exists, it runs in a nested transaction. If no active transaction exists,
Execute the statement according to the transactiondefinition. propagation_required attribute.

4. isolation level of spring transactions
1. isolation_default: This is a default isolation level of platfromtransactionmanager, which uses the default transaction isolation level of the database.
The other four correspond to the JDBC isolation level.
2. isolation_read_uncommitted: This is the lowest isolation level of the transaction. It allows an external transaction to see the uncommitted data of this transaction.
This isolation level will generate dirty reads, which cannot be repeated and Phantom reads.
3. isolation_read_committed: ensure that the data modified by one transaction can be read by another transaction only after it is committed. Another transaction cannot read the uncommitted data of the transaction.
4. isolation_repeatable_read: This transaction isolation level prevents dirty reads and prevents repeated reads. However, phantom reading may occur.
In addition to ensuring that one transaction cannot read the uncommitted data of another transaction, it also ensures that the following situations are avoided (non-repeated reads ).
5. isolation_serializable this is the most costly but reliable transaction isolation level. Transactions are processed in sequence.
In addition to preventing dirty reads and not repeated reads, Phantom reads are also avoided.

The spring configuration file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>

<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
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-2.0.xsd
Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd ">
<! -- Configure sessionfactory -->
<Bean id = "sessionfactory" class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "configlocation">
<Value> classpath: hibernate. cfg. xml </value>
</Property>
</Bean>

<! -- Configure the Transaction Manager -->
<Bean id = "transactionmanager" class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>

<! -- Configure the propagation feature of transactions -->
<TX: Advice id = "txadvice" transaction-Manager = "transactionmanager">
<TX: Attributes>
<TX: method name = "add *" propagation = "required"/>
<TX: method name = "del *" propagation = "required"/>
<TX: method name = "Modify *" propagation = "required"/>
<TX: method name = "*" Read-Only = "true"/>
</TX: Attributes>
</TX: Advice>

<! -- Which methods of those classes are involved in the transaction -->
<AOP: config>
<AOP: pointcut id = "allmanagermethod" expression = "execution (* COM. bjsxt. usermgr. Manager. *. * (...)"/>
<AOP: Advisor pointcut-ref = "allmanagermethod" advice-ref = "txadvice"/>
</AOP: config>
</Beans>

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.