[9] springboot learn fast. @Transactional annotation configuration using transaction management

Introduction

springboot use of database transactions is very convenient, just add in the way @Transactional annotation can be. Spring provides a feature-rich support for transaction management. Spring transaction management is divided into two ways programmatic and declarative. Programmatic transaction refers to a transaction implemented by coding; statement transaction based AOP, and the specific business transaction processing logic decoupling. Statement transaction management logic makes service code is not contaminated, and therefore in practical use declarative transaction with more.

Use Transactional annotation

Transactional annotation is very simple to use, just need to identify the Transactional annotation at the appropriate method above.

By following a simple example demonstrates the use of annotations Transaction

@Transactional
@Override
public void addUser() {
 User user1 = new User();
 user1.setUsername("happyjava1");
 user1.setPassword("123456");
 userRepo.save(user1);
 int i = 1 / 0;
 User user2 = new User();
 user2.setUsername("happyjava2");
 user2.setPassword("123456");
 userRepo.save(user2);
}

Here to save two users, then 1/0 of the program by way of exception. A test by test method:

@Test
public void testTx() {
 userService.addUser();
}

If the database is not user1 this record, indicating that things are worked in.

[Fast] learn springboot the @Transactional annotation configuration transaction management

The result is expected to perform the error.

[Fast] learn springboot the @Transactional annotation configuration transaction management

Database nor jappyjava1 this field. Description Transactional annotation working.

Transactional annotation configuration does not roll back abnormal by noRollbackFor

Not rollback Transactional annotation can configure an exception occurs. For example, I add the following property on the Transaction notes, specify ArithmeticException not roll back the transaction.

@Transactional(noRollbackFor = ArithmeticException.class)

Run the test method again.

[Fast] learn springboot the @Transactional annotation configuration transaction management

Still error, but check the database, user1 This record has been inserted into the database.

[Fast] learn springboot the @Transactional annotation configuration transaction management

Configuring transaction attribute propagation through the propagation behavior

The propagation of the transaction, the default value Propagation.REQUIRED. You can manually specify a different propagation behavior of the transaction as follows:

  • Propagation.REQUIRED

If the current transaction exists, it is added to the transaction, if the transaction does not currently exist, create a new business.

  • Propagation.SUPPORTS

If the current transaction exists, it is added to the transaction; if the transaction does not currently exist, the non-transactional mode continues to run.

  • Propagation.MANDATORY

If the current transaction exists, it is added to the transaction; if the transaction does not currently exist, an exception is thrown.

  • Propagation.REQUIRES_NEW

Re-create a new transaction, if the current transaction exists, suspend the current transaction.

  • Propagation.NOT_SUPPORTED

It runs as a non-transaction, if the current transaction exists, suspend the current transaction.

  • Propagation.NEVER

It runs as a non-transaction, if the current transaction exists, an exception is thrown.

  • Propagation.NESTED

If not, create a new transaction; if so, nesting other matters in the current transaction.

By isolation properties configuration transaction isolation level

Transaction isolation level, the default value Isolation.DEFAULT, this has to do with isolation level of the database. You can customize the isolation level, as follows:

  • Isolation.DEFAULT
  • Isolation.READ_UNCOMMITTED
  • Isolation.READ_COMMITTED
  • Isolation.REPEATABLE_READ
  • Isolation.SERIALIZABLE

Transaction propagation behavior and isolation level is more abstract things, for the time being not to discuss this article as a tutorial course springboot, and other follow-up will have time to write articles do explored.

timeout property

Transaction timeout, the default value is -1. If you set the time, exceeded the time limit but the transaction has not been completed, it automatically rolls back the transaction.

to sum up

This article only describes springboot by Transactional annotation, fast configuration transaction management. Database transaction, is a very complex thing, if talk clearly, should not be placed in a springboot explain the relevant article, follow-up will be renewed space, alone talk about things related to the transaction.

Guess you like

Origin www.cnblogs.com/happy4java/p/11205807.html