Spring annotated Transaction Management instance

Source: Internet
Author: User

I. Spring configuration file

The spring namespace is used here, 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: context = "http://www.springframework.org/schema/context"
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.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
Default-autowire = "byname" default-Lazy-init = "true">

<Context: Property-placeholder location = "classpath: JDBC. properties"/>

<Bean id = "datasource" class = "org. Apache. commons. DBCP. basicdatasource"
Destroy-method = "close">
<Property name = "driverclassname" value = "$ {JDBC. driverclassname}"/>
<Property name = "url" value = "$ {JDBC. url}"/>
<Property name = "username" value = "$ {JDBC. Username}"/>
<Property name = "password" value = "$ {JDBC. Password}"/>
<Property name = "initialsize" value = "$ {JDBC. initialsize}"/>
<Property name = "maxactive" value = "$ {JDBC. maxactive}"/>
<Property name = "maxidle" value = "$ {JDBC. maxidle}"/>
<Property name = "minidle" value = "$ {JDBC. minidle}"/>
<Property name = "maxwait" value = "$ {JDBC. maxwait}"/>
</Bean>

<! -- Set transactionmanager -->
<Bean id = "transactionmanager"
Class = "org. springframework. JDBC. datasource. datasourcetransactionmanager">
<Property name = "datasource" ref = "datasource"/>
</Bean>

<! -- Start the spring annotation function -->

<TX: annotation-driven transaction-Manager = "transactionmanager"/>

 
</Beans>

Note:

1. If the Transaction Manager ID is transactionmanager, you cannot configure transaction-manager here, that is, <TX: annotation-driven/>.

2. This configuration tells spring to check the application at the class (Interface) or method level.ProgramAll beans with the standard @ transactional in the context, spring will automatically notify the transaction notification content to it.

3. The transaction parameter of this notification will be defined by the @ transactional annotation parameter.

4. If an interface is annotated, all the implementation classes of the interface will be transcoded.

Ii. Use @ transactional to mark Bean

Package com. netqin. bbs. inituserdata;

Import org. springframework. transaction. annotation. propagation;
Import org. springframework. transaction. annotation. Transactional;

Import com. netqin. bbs. inituserdata. Service. inituserdataservice;
Import com. netqin. bbs. utils. constant;
Import com. netqin. bbs. utils. readfileutil;

@ Transactional (propagation = propagation. supports, readonly = true) // sets the default transaction management policy, that is, the transaction processing method without @ transactional annotation, meaning that the method is not required to run in a transaction
Public class inituserdata {
/**
* Description: The transaction management policy declared on the method. It indicates that a transaction needs to be run. <br>
* <P>
                                                                                                                                                                                                                                                                                                
*/
@ Transactional (propagation = propagation. required, readonly = false)
Public void Method1 (){

........................

........................

}

/**
* Description: Use the Default policy. <br>
* <P>
                                                                                                                                                                                                                                                                                                
*/

Public void method2 (){

........................

........................

}

}

Remember to add this bean to the spring context.

In general, the above two transaction policies can meet the requirements, but note that the use of annotation functions need to add cglib-nodep-x.x_x.jar in the project

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.