Springboot manual control of affairs

  First, the importance of affairs, I believe in the actual development process, has a deep understanding. But there was a problem when we often under normal circumstances are developed with annotations way to control affairs, put it plainly to coordination between transaction-based control spring 7 kinds of affairs.

  Two, spring propagation behavior Affairs 7

Propagation.REQUIRED Current methods of support on behalf of the current transaction, and the caller is in the context of the same transaction, rollback unified rollback (if the current method is invoked when the other methods, and the caller itself has affairs), if there is no transaction, then himself New transaction,
Propagation.SUPPORTS Current methods of support on behalf of the current transaction, and the caller is in the context of the same transaction, rollback unified rollback (if the current method is invoked when the other methods, and the caller itself has affairs), if there is no transaction, the the method of execution in the context of non-transactional
Propagation.MANDATORY Current methods of support on behalf of the current transaction, and the caller is in the context of the same transaction, rollback unified rollback (if the current method is invoked when the other methods, and the caller itself has affairs), if there is no transaction, then toss abnormal
Propagation.REQUIRES_NEW Create a new transaction context, if the caller of the current method has been transaction, the pending transaction caller, the two transactions are not in the same context, if the respective exception occurs, each rollback
Propagation.NOT_SUPPORTED The method is performed in a non-transactional state of the caller if the call to this method has a transaction is pending before the caller's transaction
Propagation.NEVER The method is non-transactional state of execution, if the caller transaction exists, an exception is thrown
Propagation.NESTED If there is a transaction in the current context, places nested transaction for performing the method, is to say, this method is part of the external part of the method, the caller rollback, the rollback method, but this method yourself if abnormal, then himself rollback does not affect external affairs, if the transaction does not exist, and as PROPAGATION_REQUIRED

  Three, four features and database MySQL transaction isolation level

  1) Four characteristics  

  a, atomicity (Atomicity)
  Atomicity means that all the operations included in the transaction either all succeed or all fail rolled back.

  b, consistency (Consistency)
  Consistency means that the transaction must transform the database from one consistent state to another consistent state, that is to say before and after a transaction execution and implementation must be in a consistent state.

  C, isolation (Isolation)
  isolation is when a plurality of users concurrent access to the database, such as operating with a table, a database for each user transaction open, operation not being disturbed by other transactions between a plurality of concurrent transactions to be isolated from each other.

  d, persistent (Durability)
  Persistence means that once a transaction is committed, then changes to the data in the database is permanent, commit the transaction will not be lost even in the case of a database system experienced a failure of the operation .

  2) isolation level

  

  a, a dirty read means reads the transaction processing data in uncommitted transaction in another. (Uncommitted read)

  B, refers to a non-repeatable read database for a data transaction within a range of multiple queries but returns different data values, which is due to poll interval is modified and submitted to another transaction. (Read While Write)

  c, phantom read refers to two concurrent transactions, two transactions modify data, read data is not data to start their own modifications. Phantom reads and non-repeatable read are read another transaction has been submitted (this is different to dirty read), the difference is non-repeatable read query data items are the same, but the magic of reading for a group data whole. (While writing, and reading)

  3) database transaction level, using default Repeatable read level, the level of the list from the bottom up, the lower level. View Level

select @@tx_isolation;

  Fourth, the topic, write above spring Affairs and database transaction isolation levels, the main purpose is to understand the relationship that exists between the transfer transaction, so that when controlled, spring will be the relationship between transactions and affairs to achieve rollback or the effect of submission.

  Fifth, if the time is no way to use annotations (such as multi-threading, etc.), then use the manual way to do transaction management, and this is the programmatic transaction management .

  1) First, add annotations, this is the way jdbc transaction management framework provided in the spring

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private TransactionDefinition transactionDefinition;

   2) look at the source code (DataSourceTransactionManagerAutoConfiguration.class, TransactionTemplate.class)

        @Bean
        @ConditionalOnMissingBean({PlatformTransactionManager.class})
        public DataSourceTransactionManager transactionManager(DataSourceProperties properties) {
            DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.dataSource);
            if (this.transactionManagerCustomizers != null) {
                this.transactionManagerCustomizers.customize(transactionManager);
            }

            return transactionManager;
        }

  Note: There are interested can find out DataSourceTransactionManager wording and principles.

        @Bean
        @ConditionalOnMissingBean
        public TransactionTemplate transactionTemplate() {
            return new TransactionTemplate(this.transactionManager);
        }

  注意,这里的所有事务传播方式包括处理,都需要自己手动去处理。

  3)编写方式

TransactionStatus transactionStatus = platformTransactionManager.getTransaction(transactionDefinition);
platformTransactionManager.commit(transactionStatus);
platformTransactionManager.rollback(transactionStatus);

  说明:这里开发事务过后,返回一个事务状态,这个状态记录了东西,用来控制事务的管理,当然,多个事务之间的控制需要人为控制。

  

 

   4)编程式的事务控制经量少用,因为控制程度上面来说spring的方式还是来的更加不错,编程式的方式,更多用于在需要事务的时候,没有办法加入事务,才采取手动控制事务的方式。

 

Guess you like

Origin www.cnblogs.com/ll409546297/p/11076258.html