classification

Programmatic transaction

Programmatic transaction: refers to the manual management of transaction submission, rollback and other operations in the code, the code is relatively invasive

try {
    //TODO something
     transactionManager.commit(status);
} catch (Exception e) {
    transactionManager.rollback(status);
    throw new InvoiceApplyException("Abnormal failure");
}
Copy the code

Declarative transaction

Declarative transaction: AOP based on the aspect, it will be specific business and transaction processing part decoupled, code invasion is very low, so in the actual development of declarative transaction used more. Declarative transactions can also be implemented using XML configuration files based on TX and AOP, or using the @Transactional annotation.

@Transactional
public class test {
    public static void main(String[] args) {}}Copy the code

@Transactional

The source code

// Apply to methods and classes. Note that if the class and method are annotated with transactions, the method is the nearest rule
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Transactional {

	@AliasFor("transactionManager")
	String value(a) default "";

	@AliasFor("value")
	String transactionManager(a) default "";

	Propagation propagation(a) default Propagation.REQUIRED;

	Isolation isolation(a) default Isolation.DEFAULT;

	boolean readOnly(a) default false;

	Class<? extends Throwable>[] rollbackFor() default {};
        
	String[] rollbackForClassName() default {};

	String[] noRollbackForClassName() default {};

}
Copy the code

Failure scenario

See the following failure scenarios in the reference link article

Apply to methods that are not public

Error in attribute Propagation setting

The rollbackFor attribute is incorrectly set

A method call in the same class causes @Transactional to fail

The exception being “eaten” by your catch causes @Transactional to fail

The database engine does not support transactions

TransactionDefinition

attribute

public interface TransactionDefinition {
	int PROPAGATION_REQUIRED = 0;

	int PROPAGATION_SUPPORTS = 1;

	int PROPAGATION_MANDATORY = 2;

	int PROPAGATION_REQUIRES_NEW = 3;

	int PROPAGATION_NOT_SUPPORTED = 4;

	int PROPAGATION_NEVER = 5;

	int PROPAGATION_NESTED = 6;

	int ISOLATION_DEFAULT = -1;

	int ISOLATION_READ_UNCOMMITTED = Connection.TRANSACTION_READ_UNCOMMITTED;

	int ISOLATION_READ_COMMITTED = Connection.TRANSACTION_READ_COMMITTED;

	int ISOLATION_REPEATABLE_READ = Connection.TRANSACTION_REPEATABLE_READ;

	int ISOLATION_SERIALIZABLE = Connection.TRANSACTION_SERIALIZABLE;

	int TIMEOUT_DEFAULT = -1;
Copy the code

Transaction propagation type

The default transaction type is REQUIRED

  • Propagation.REQUIRED: Join the transaction if it exists, or create a new transaction if it does not exist. (That is, if methods A and B are annotated, the default propagation mode will merge the two methods’ transactions into one if method A calls method B internally.)
  • Propagation.SUPPORTS: Joins a transaction if it currently exists. If no transaction currently exists, it continues in a non-transactional manner.
  • Propagation.MANDATORY: If a transaction exists, it is joined. If no transaction currently exists, an exception is thrown.
  • Propagation.REQUIRES_NEW: Re-create a new transaction, and suspend the current transaction if it exists. Propagation.REQUIRED; REQUIRES_NEW; Propagation.REQUIRES_NEW; [[email protected]_NEW] [[email protected]_NEW]
  • Propagation.NOT_SUPPORTED: To run in a non-transaction manner. If a transaction exists, it is suspended.
  • Propagation.NEVER: To run in non-transaction mode. If the transaction exists, an exception is thrown.
  • Propagation.NESTED: The result was the same as Propagation.

Isolation level of data

Isolation: Isolation level of a transaction. The DEFAULT is Isolation.default.

  • Isolation.DEFAULT: Use the DEFAULT Isolation level of the underlying database.
  • Isolation.READ_UNCOMMITTED
  • Isolation.READ_COMMITTED
  • Isolation.REPEATABLE_READ
  • Isolation.SERIALIZABLE

Expiration time

int TIMEOUT_DEFAULT = -1;
Copy the code

Reference article:

  • @tranctional Failure scenario