Getting Started with transaction management in spring: Programmatic transaction Management (TRANSACTIONTEMPLATE) __ Programming

Source: Internet
Author: User
Tags rollback

Reprint: http://blog.csdn.net/zq9017197/article/details/6321391

Spring can support programmatic and declarative transactions.

Spring uses the transaction manager, which implements the interface for each of the different platform's transaction managers: Platformtransactionmanager

This interface is the core of transaction management and provides three functions that need to be implemented:

[Java] View plain copy commit (Transactionstatus status);      Gettransaction (transactiondefinition definition); Rollback (transactionstatus status);

If we are using JDBC to handle transactions, then this transaction manager is Datasourcetransactionmanager.

Find this class through the spring document and find that it needs to datasource this class. That is, a class that implements the Javax.sql.DataSource interface can be passed into the Datasourcetransactionmanager as a parameter.

Then, find the Transactiontemplate in the package org.springframework.transaction.support.

One important method of discovering transactiontemplate is:

[Java] View plain copy execute (transactioncallback action);

It is using this method that we can add transactions to this method.

This method needs to pass in the parameter transactioncallback.

Transactioncallback, as the name suggests, is the transaction callback and then find Transactioncallback.

Found that this is an interface (this must also be an interface, because the task is defined by itself)

There is only one way:

[Java] View plain copy dointransaction (transactionstatus status);

Obviously, what is needed to be done in a business involves this approach.

And this dointransaction passed a parameter, this is Transactionstatus, continue as the name implies, that is, the state of affairs.

Query down, this transactionstatus is still an interface. See what services (methods) are defined by this interface:

[Java] View plain copy hassavepoint ();     IsCompleted ();   Isnewtransaction (); Setrollbackonly ();

When you need to roll back, you need to call setroolbackonly (); It's OK.

Well, it's been so long, now it's a simple summary of programmatic transaction management.

First of all: because we are using a specific platform, we need to create a suitable platform for our transaction management Plateformtransactionmanager. If you are using JDBC, use Datasourcetransactionmanager. Note that you need to pass in a datasource so that the platform knows how to deal with the database.

Second: To make the platform transaction manager transparent to us, we need to use transactiontemplate. Using Transactiontemplat requires a plateformtransactionmanager to enter, so we get a transactiontemplate, without caring what platform we're using.

Third: The important method of Transactiontemplate is the Execute method, which is to call transactioncallback for processing.

In other words, all the things we need to deal with are encoded in transactioncallback.

IV: The Transactioncallback interface, we can define a class and implement this interface, and then as a transactiontemplate.execute parameter. Put what needs to be done into the dointransaction and pass in a transactionstatus parameter. This parameter is to invoke the rollback.

In other words, Plateformtransactionmanager and transactiontemplate only need to be defined once in the program, and Transactioncallback and Transactionstatus Must be defined more than once for different tasks.

This is the programmatic transaction management of spring. The following example code is posted:

Templateutils

[Java] View plain copy import Javax.naming.Context;   Import Javax.naming.InitialContext;   Import javax.naming.NamingException;      Import Javax.sql.DataSource;   Import Org.apache.log4j.Logger;   Import Org.springframework.jdbc.core.JdbcTemplate;   Import Org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;   Import Org.springframework.jdbc.core.simple.SimpleJdbcTemplate;   Import Org.springframework.jdbc.datasource.DataSourceTransactionManager;   Import Org.springframework.transaction.PlatformTransactionManager; Import Org.springframework.transaction.support.TransactionTemplate;

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.