Spring2.x transaction Configuration Policy

Source: Internet
Author: User

Spring1.xDeclarative transactions use the transactionproxyfactorybean Configuration Policy to be simple and easy to understand, but the configuration is extremely cumbersome: each target bean requires an additional transactionproxyfactorybean proxy, which will lead to a sharp increase in the configuration file.

Spring 2.xThe XML Schema method of provides a more concise transaction Configuration Policy, and spring2.x providesTX namespaceTo configure transaction management. The TX namespace provides<TX: advice.../>To configure the enhanced transaction processing. Once the enhanced transaction processing is configured with this element, you can directly use<AOP: Advisor.../>
The automatic proxy is enabled for the element.

Newsdao. Java:

public interface NewsDao {public void insert(Integer id,String title,String content);}

Newsdaoimpl. Java:

import javax.sql.DataSource;import org.springframework.jdbc.core.JdbcTemplate;public class NewsDaoImpl implements NewsDao{private DataSource ds;public void setDs(DataSource ds) {this.ds = ds;}@Overridepublic void insert(Integer id, String title, String content) {JdbcTemplate jt=new JdbcTemplate(ds);jt.update("insert into news values(?,?,?)",new Object[]{id,title,content});jt.update("insert into news values(?,?,?)",new Object[]{id,title,content});}}

Bean. xml:

<? 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: 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.5.xsdhttp: // your http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <! -- Define the data source Bean --> <bean id = "datasource" class = "com. mchange. v2.c3p0. combopooleddatasource "> <property name =" driverclass "value =" oracle. JDBC. driver. oracledriver "/> <property name =" jdbcurl "value =" JDBC: oracle: thin :@ localhost: 1521: orcl "/> <property name =" user "value =" Scott "/> <property name =" password "value =" tiger "/> <property name =" maxpoolsize "Value = "40"/> <property name = "minpoolsize" value = "1"/> <pro Perty name = "initialpoolsize" value = "1"/> <property name = "maxidletime" value = "20"/> </bean> <! -- Configure a business logic Bean --> <bean id = "newsdao" class = "com. bean. newsdaoimpl "> <property name =" ds "ref =" datasource "/> </bean> <! -- Configure the local Transaction Manager of the JDBC data source --> <bean id = "transactionmanager" class = "org. springframework. JDBC. datasource. datasourcetransactionmanager "> <property name =" datasource "ref =" datasource "/> </bean> <! -- Configure the transaction enhancement processing bean and specify the Transaction Manager --> <TX: Advice id = "txadvice" transaction-Manager = "transactionmanager"> <TX: Attributes> <! -- All Methods Starting with 'get' are read-only --> <TX: method name = "get *" Read-Only = "true"/> <! -- Use the default Transaction Processing Method for other methods --> <TX: method name = "*"/> </TX: Attributes> </TX: Advice> <! -- Element of AOP configuration --> <AOP: config> <AOP: pointcut id = "mypointcut" expression = "execution (* COM. bean. *. *(..)) "/> <AOP: Advisor advice-ref =" txadvice "pointcut-ref =" mypointcut "/> </AOP: config> </beans>

Test. Java:

Public class test {public static void main (string [] ARGs) {applicationcontext CTX = new classpathxmlapplicationcontext ("bean. XML "); newsdao Dao = (newsdao) CTX. getbean ("newsdao"); Dao. insert (1, "win", "Green Shirts win ");}}

Run the program and the console outputs:

The transaction is started automatically. Two records are a transaction, and the insertion of the second record fails, causing the first record to be rolled back.

Configuration<TX: advice.../>You only need to specify a transaction-manager attribute. The default value of this attribute is "transactionmanager ". In addition to the transaction-manager attribute, you also need to configure an attributes sub-element, which can contain multiple method sub-elements, each<Method.../>The sub-element specifies the transaction semantics required for a batch of methods, including the transaction propagation attribute, transaction isolation attribute, transaction timeout attribute, read-only transaction, and rollback of the specified exception, does not roll back the specified exception.

You can specify the following attributes When configuring the method sub-element:

Attribute Description
Name Required attribute. method name associated with the transaction semantics. This attribute supports wildcard characters, such as get * and handle.
Propagation Specifies the transaction Propagation Behavior. The attribute value can be any enumeration value of the propagation enumeration class. The default value is propagation_required.
Isolation Specifies the transaction isolation level. This attribute value can be any enumeration value of the isolation enumeration class. The default value is isolation_default.
Timeout Specifies the transaction timeout time (in seconds ). -1 indicates no timeout. The default value is-1.
Read-Only Whether the transaction is read-only. The default value is false.
Rollback- Specifies the exception classes that trigger transaction rollback. Multiple exception classes can be specified, separated by commas.
No-rollback- Specifies the exception classes that do not trigger transaction rollback. You can specify multiple exception classes separated by commas.

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.