Spring Transaction Pass Tutorial _ there are instances

Source: Internet
Author: User

Through this article, you will learn about the delivery of transactions in the spring framework

1. Introduction

When dealing with spring-managed transactions, the developer can define the behavior of the transaction in a propagated manner. In other words, developers can determine how business methods are encapsulated in logical and physical transactions. Different methods from different spring beans can be executed in the same transaction scope or split into multiple nested transactions. This can lead to details like how the results of internal transactions affect external transactions. We'll see different behaviors of different propagation mechanisms in spring in the next section.

This tutorial will focus on the propagation mechanism behavior of the transaction only. Other aspects of the spring transaction, you can refer to other tutorials or spring official documentation.

The complete code used in this tutorial can be downloaded below the tutorial. We will show only the relevant parts of spring that help you understand the transaction propagation mechanism.

The full source code uses hibernate to implement the persistence layer (Spring uses hibernate for the transaction example).

2. Required Behavior

Spring's required behavior means that the transaction is used if an open transaction already exists in the context of the current Bean method execution, and if there is no existing transaction, the spring container creates a new transaction and uses it. If multiple methods are configured as required and called nested calls, they are divided into multiple logical transactions, but the same physical transaction is used at the bottom. In simple terms, this means that if the inner layer method causes a transaction rollback, the external method invocation transaction will also fail to roll back at the same time. Let's take a look at an example:

External Bean

[Email protected]

02private Testdao Testdao;

03

[Email protected]

05private Innerbean Innerbean;

06

[Email protected]

[Email protected] (propagation=propagation.required)

09public void testrequired (user user) {

Ten testdao.insertuser (user);

try{

Innerbean.testrequired ();

(RuntimeException e) {

//Handle exception

15}

16}

Internal Bean

[Email protected]

[Email protected] (propagation=propagation.required)

3public void testrequired () {

4 throw new RuntimeException ("Rollback this transaction!");

5}

Note that the method of the internal bean is annotated with required and throws a runtimeexception. This means that it will use the same transaction with the external bean, so the external transaction will fail at commit and the same rollback operation.

Note: By default, only non-inspected exceptions (unchecked exception), like RuntimeException, set the state of the transaction to rollback. If you want the exception to be inspected (checked exception) to also set the transaction state to rollback, you must set them, but this is not included in this tutorial.

Note 2: When using declarative transactions (that is, by using annotations), if you call the method directly in the same bean (calling your own method yourself), @Transactional annotations are ignored by the container. If you want to enable transaction management for your own method calls, you must use ASPECTJ to configure the transaction, but that content is not included in this tutorial.

3. Requires_new behavior

Requires_new behavior means that the container will always create new physical transactions. In other words, internal transactions can commit or roll back transactions independently of external transactions, meaning that external transactions will not be affected by the results of internal transactions: they will return different physical transactions.

External Bean

[Email protected]

02private Testdao Testdao;

03

[Email protected]

05private Innerbean Innerbean;

06

[Email protected]

[Email protected] (propagation=propagation.required)

09public void testrequiresnew (user user) {

Ten testdao.insertuser (user);

try{

Innerbean.testrequiresnew ();

(RuntimeException e) {

//Handle exception

15}

16} Internal Bean

[Email protected]

[Email protected] (propagation=propagation.requires_new)

3public void Testrequiresnew () {

4 throw new RuntimeException ("Rollback this transaction!");

The 5} Internal method uses requireds_new for annotations and throws RuntimeException, so its transactions are rolled back but do not affect external transactions. External transactions are suspended when the internal transaction is started and then resumed after the internal transaction has finished processing. They are handled independently so that external transactions are also successfully committed.

4. netsted Behavior

The nested behavior uses the same physical transaction but sets the retention point (savepoint) when nested calls, so that internal transactions can be rolled back independently of the external transaction without affecting the external transaction. This may be similar to the savepoint of JDBC, so this behavior needs to be used in conjunction with spring's JDBC-managed transactions (Spring JDBC Transaction example).

5. Mandatory behavior

The mandatory behavior requires that you have a transaction that already exists and is open when you execute the method. If there are no existing transactions, the container throws an exception.

6. Never behavior

The never behavior requires that the method be executed without a transaction that already exists. If the transaction already exists, the container throws an exception.

7. not_supported Behavior

not_supported behavior is marked when the method is executed, the transaction is ignored. If an open transaction already exists, the transaction is suspended.

8. Supports behavior

Supports behavior is marked when the method is executed, if there is a transaction, the transaction scope is executed, and if it does not exist, it is not transacted.

9. Full source of Notes

A complete code download is available at the end of this page. Here is the MySQL build Table statement:

MySQL Build Table statement

1CREATE TABLE USER (

2 ID INT not NULL auto_increment PRIMARY KEY,

3 USERNAME VARCHAR (+) not NULL,

4 NAME VARCHAR (+) not NULL,

5 UNIQUE (USERNAME)

6);

Download the tutorial source code

Download Link: spring-transaction-propagation-tutorial.zip

Spring Transaction Pass Tutorial _ there are instances

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.