This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

In my last article, I talked about declarative transactions. In this article, I’ll talk about programmatic transactions and how to implement them.

Note: the Spring 6. X

Core principles

Open Spring transaction is essentially added a Advisor, but we use @ EnableTransactionManagement annotations to open the Spring transaction is that the annotation agent’s function is to add the two beans in the Spring container:

  1. AutoProxyRegistrar
  2. ProxyTransactionManagementConfiguration

AutoProxyRegistrar primary role is to the Spring container is registered in a InfrastructureAdvisorAutoProxyCreator Bean. And InfrastructureAdvisorAutoProxyCreator inherited AbstractAdvisorAutoProxyCreator, so this class is the main function of automatic open proxy, which is a BeanPostProcessor, The post-initialization step looks for beans of type Advisor and determines whether a Bean currently has a matching Advisor and whether a proxy object needs to be generated using dynamic proxies.

ProxyTransactionManagementConfiguration is a configuration class, it defines the three other bean:

  1. BeanFactoryTransactionAttributeSourceAdvisor: an Advisor
  2. AnnotationTransactionAttributeSource: the equivalent of the Pointcut BeanFactoryTransactionAttributeSourceAdvisor
  3. TransactionInterceptor: equivalent to BeanFactoryTransactionAttributeSourceAdvisor’s Advice

AnnotationTransactionAttributeSource is used to determine whether there @ Transactional annotation on a class, or to determine whether there @ Transactional annotation on a method. **TransactionInterceptor ** is proxy logic. When a class has the @Transactional annotation in it, a proxy object is generated as a Bean. You end up in the TransactionInterceptor invoke() method.

Transaction Execution Principle

A Bean to create Bean in the implementation of the life cycle, after InfrastructureAdvisorAutoProxyCreator initialization method, Will determine whether the current Bean object and BeanFactoryTransactionAttributeSourceAdvisor horse matchs, matching logic to determine whether there @ Transactional annotation on the Bean’s class, Or whether there is a @Transactional annotation ona method in the class, which means that the Bean needs to be dynamically proxyed to produce a proxy object as the Bean object.

The proxy objects when performing a method, to determine whether the currently executing method and BeanFactoryTransactionAttributeSourceAdvisor match, If matched, the Invoke () method of the TransactionInterceptor in the Advisor is executed. The basic flow is as follows:

  1. Use the configured PlatformTransactionManager transaction manager to create a new database connection
  2. Change the database connection autocommit to false
  3. The MethodInvocation.proceed() method is simply the business method, where SQL is executed
  4. If no exception is thrown, commit
  5. If an exception is thrown, it is rolled back

Transaction isolation level

Programming the transaction

Here is another programming transaction, example code:

public void testCommit2(a) {
    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.execute((TransactionCallback<Object>) status -> {
        String uuid = UUID.randomUUID().toString().replace("-"."");
        jdbcTemplate.update("insert into summer_main(`name`) value ('" + uuid + "')");

        List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from summer_main where `name` = '" + uuid + "'");
        Integer id = null;
        if (maps.size() > 0) {
            Map<String, Object> stringObjectMap = maps.get(0);
            id = Integer.parseInt(String.valueOf(stringObjectMap.get("id")));
            if (id % 2= =0) {
                status.setRollbackOnly();
                return 0;
            }
        }
        jdbcTemplate.update("update summer_main set remarks = 'WaKen Notes' where id = " + id);
        return 1;
    });
}

Copy the code

The data in the table is as followsYou can see that if the data id is divisible by 2, it successfully falls back, and then you have an odd number of id cores

The resources

  • Docs. Spring. IO/spring – fram…
  • www.cnblogs.com/mseddl/p/11…