Spring transaction management [Focus]

Spring transaction management

Commit and rollback transactions to spring

 

1, Brief introduction

In Java EE development, transactions are in the Service layer,

Judged by the program: in case of abnormal transaction rollback, no abnormal transaction commit occurs.

Spring uses AOP technology for transaction control, and helps developers timely submit or roll back transactions

 

Spring: the service layer has no exception, and the transaction is submitted automatically

An exception occurred in the Service layer, and the transaction was automatically rolled back

 

 

2, Operation of Spring transaction

See the integration of spring and MyBatis in the next chapter for details of environment construction

 

Test class:

/**
 * Query all user information
 */
@Test
public void run1(){
    List<User> ulist = userService.selectAll();
    for (User user : ulist) {
        System.err.println(user);
    }
}

 

Service class:

/**
 * Query all user information
 * @return
 */
@Override
public List<User> selectAll() {
    return userDao.selectAll();
}

 

 

  • Add user:

Test class:

/**
 * Add a user information
 */
@Test
public void run2(){
    int row = userService.saveUser(new User("009","Xiao Zhang","123"));
    System.err.println("Added successfully:"+row);
}

 

Service class:

@Override
public int saveUser(User user) {
    return userDao.insert(user);
}

 

  • Modify user:

Test:

    @Test
    public void run3(){
//Create a user object
        user user = new user("222", "aaa", "1111");
//Call service
      int row=service.updateUser(user);
        System.err.println(row);

    }

 

Service:

@Override
public int updateUser(user user) {
    int i = dao.updateByPrimaryKey(user);
    return i;
}

 

Delete user:

Test:

@Test
public void run4(){
   int row= service.deleteUser("222");
    System.err.println(row);

}

 

Service:

@Override
public int deleteUser(String s) {
    int i = dao.deleteByPrimaryKey(s);
    return  i;
}

 

 

Summary:

Through @ Transactional annotation, the transaction of Service layer has been submitted to spring management.

Management mode:

There is no exception in the Service method, and the transaction is submitted automatically

There is an exception in the Service method, and the transaction is automatically rolled back

 

In development, in order to improve the running efficiency of the program, it is suggested to use different types of transactions for DQL query statements and DML add / delete / modify statements.

annotation

describe

@Transactional(readOnly=true)

Read only transaction, improve operation efficiency, DQL use

@Transactional(readOnly=false)

Default value, non read-only transaction, can be added, deleted or modified. DML use

Modified service class:

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    /**
     * Query all user information
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<User> selectAll() {
        return userDao.selectAll();
    }

    @Override
    public int saveUser(User user) {
        return userDao.insert(user);
    }
}

 

The default value is - 1, no timeout limit. If so, set in seconds. (generally not set)

annotation

describe

@Transactional(timeout=60)

Set the timeout to 60 seconds. If the operation is not finished, an exception will be thrown.

 

in general:

Spring transaction management: (AOP)

Spring configuration class: enable transaction management

Service layer: @ Transactional: marks that the transaction of the current class is managed by spring

 

How Spring manages transactions:

  1. If there is no exception in the service method, spring will automatically commit the transaction
  2. If there is an exception in the service method, spring will automatically roll back the transaction

 

 

 

After watching it, congratulations and a little progress!!!

The more you know, the more you don't know

~Thank you for reading. Your support is the biggest driving force for my study! Come on, strangers work together and encourage together!!

Keywords: Programming Spring Java Mybatis

Added by fredroines on Tue, 05 May 2020 21:50:16 +0300