Transaction management in spring (4)

Transaction management in spring (4)


Timeout and read-only properties

Because transactions can acquire locks on rows and tables, long transactions consume resources and have an impact on overall performance.

If a transaction only reads data but does not modify it, the database engine can optimize the transaction.

Timeout transaction property: How long a transaction can stay on before forcing a rollback. This prevents long-running transactions from consuming resources.

Read-only transaction attribute: Indicates that this transaction only reads data but does not update data, which can help the database engine optimize the transaction.

 

Timeout and read-only properties can be defined in the @Transactional annotation. The timeout property is calculated in seconds.


In transaction notifications, timeout and read-only attributes can be specified in the <tx:method> element.

 

Property settings in transactions (@Transactional)

propagation attribute: used to set the propagation behavior of the transaction

            - Propagation.REQUIRED : default, use the caller's transaction

            - Propagation.REQUIRES_NEW : start a new transaction

     

     isolation property: used to set the isolation level of the transaction

            - Isolation.REPEATABLE_READ : Repeatable read, Mysql default isolation level

            - Isolation.READ_COMMITTED : read has been committed, Oracle 's default isolation level is also a common isolation level

     

     rollbackFor attribute: used to set the rollback when an exception occurs, the value is an array, and the type of exception is placed in it

     rollbackForClassName property: used to set the rollback when an exception occurs, the value is an array, and the name of the exception is placed in it

     noRollbackFor attribute: used to set what exception does not roll back, the value is an array, which is the type of exception

     norollbackForClassName property: used to set no rollback when an exception occurs, the value is an array, and the name of the exception is placed in it

     

     timeout attribute: used to set the timeout time in seconds

     

     readOnly attribute: used to set the current operation is a read-only operation, usually set this attribute to true when querying the database

 

@Transactional(propagation=Propagation.REQUIRES_NEW , isolation=Isolation.READ_COMMITTED,

            noRollbackFor={ArithmeticException.class},timeout=3,readOnly=false)

 

 

 

 

pointcut expression

1. The format of the pointcut expression: execution([visibility] return type [declaration type]. method name (parameter) [exception])

 

2. Pointcut expression wildcard:

      *: matches all characters

      ..: Generally used to match multiple packages, multiple parameters

      +: Represents a class and its subclasses

 

3. The pointcut expression supports logical operators: &&, ||, !

 

4. Pointcut expression keywords:

      1) execution: used to match subexpressions.

            //匹配com.cjm.model包及其子包中所有类中的所有方法,返回类型任意,方法参数任意

            @Pointcut("execution(* com.cjm.model..*.*(..))")

            public void before(){}

 

      2)within:用于匹配连接点所在的Java类或者包。

            //匹配Person类中的所有方法

            @Pointcut("within(com.cjm.model.Person)")

            public void before(){}

 

            //匹配com.cjm包及其子包中所有类中的所有方法

            @Pointcut("within(com.cjm..*)")

            public void before(){}

 

     3) this:用于向通知方法中传入代理对象的引用。

            @Before("before() && this(proxy)")

            public void beforeAdvide(JoinPoint point, Object proxy){

                  //处理逻辑

            }

 

      4)target:用于向通知方法中传入目标对象的引用。

            @Before("before() && target(target)

            public void beforeAdvide(JoinPoint point, Object proxy){

                  //处理逻辑

            }

 

      5)args:用于将参数传入到通知方法中。

            @Before("before() && args(age,username)")

            public void beforeAdvide(JoinPoint point, int age, String username){

                  //处理逻辑

            }

 

      6)@within :用于匹配在类一级使用了参数确定的注解的类,其所有方法都将被匹配。

            @Pointcut("@within(com.cjm.annotation.AdviceAnnotation)") - 所有被@AdviceAnnotation标注的类都将匹配

            public void before(){}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325939654&siteId=291194637