GVKun编程网logo

Spring @Transactional Annotation属性的优先级/继承(spring优先级order)

17

在这篇文章中,我们将为您详细介绍Spring@TransactionalAnnotation属性的优先级/继承的内容,并且讨论关于spring优先级order的相关问题。此外,我们还会涉及一些关于@T

在这篇文章中,我们将为您详细介绍Spring @Transactional Annotation属性的优先级/继承的内容,并且讨论关于spring优先级order的相关问题。此外,我们还会涉及一些关于@Transactional Annotation +用于在循环中插入数据、java – Spring @Transactional Annotation属性优先级/继承、java – 如何扩展Spring Annotation @Transactional、Java-API-Package:org.springframwork.transaction.annotation的知识,以帮助您更全面地了解这个主题。

本文目录一览:

Spring @Transactional Annotation属性的优先级/继承(spring优先级order)

Spring @Transactional Annotation属性的优先级/继承(spring优先级order)

REQUIRED传播时,如果调用方方法本身是事务型的,那么当前方法是否会覆盖包含的事务属性(例如rollbackFor)(如果它们不同)?

插图:

Class A {    @Transactional(propagation = Propagation.REQUIRED,        rollbackFor = { SomeException.class})    void foo() {        try {           b.bar();        } catch (OtherException e) {           // is the transaction marked as rollback-only at this point ?        }    }}Class B {    @Transactional(propagation = Propagation.REQUIRED,        rollbackFor = { OtherException.class})    void bar() {        [...]    }}

编辑

好吧,我想避免一些琐碎的问题,所以让我们清楚一点,我知道弹簧传播处理。

如果不是这样,则下面是文档的相关部分,我只想澄清与上面的示例有关的第一部分:

PROPAGATION_REQUIRED

当传播设置为PROPAGATION_REQUIRED时,将为应用该设置的每种方法创建一个逻辑事务作用域。每个这样的逻辑事务作用域可以单独确定仅回滚状态,而外部事务作用域在逻辑上独立于内部事务作用域。当然,在标准PROPAGATION_REQUIRED行为的情况下,所有这些范围都将映射到同一物理事务。因此,内部事务范围中设置的仅回滚标记确实会影响外部事务实际提交的机会(正如您期望的那样)。

但是,在内部事务范围设置仅回滚标记的情况下,外部事务尚未决定回滚本身,因此回滚(由内部事务范围默默触发)是意外的。此时将引发相应的UnexpectedRollbackException。这是预期的行为,因此事务调用者永远不会被误认为是在确实未执行提交的情况下进行的。因此,如果内部事务(外部调用者不知道)内部将事务无提示地标记为仅回滚,则外部调用者仍会调用commit。外部调用者需要接收UnexpectedRollbackException,以清楚地指示已执行回滚。

我的问题可以这样改写:

逻辑事务作用域是否包含事务属性?

答案1

小编典典

因此,我建立了一个测试用例,简单的答案是肯定的。

事务逻辑作用域包含事务属性,其边界确实是带注释的方法边界。

因此,即使两种方法的基础物理事务都相同,逻辑属性也适合于每种方法,而内部方法可以强制回退外部方法事务。但是,如果最后一次触发提交,则会导致UnexpectedRollbackException。

cf. Spring TransactionInterceptor(评论是我的)

try {        retVal = invocation.proceed();}catch (Throwable ex) {        completeTransactionAfterThrowing(txInfo, ex);        throw ex;}

completeTransactionAfterThrowing():

// txinfo is proper to the invocation target methodif (txInfo.transactionAttribute.rollbackOn(ex)) {            try {                txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());            }

AbstractPlatformTransactionManager.processRollback():

else if (status.isNewTransaction()) { //requiresnew    doRollback(status);}else if (status.hasTransaction()) { //requiered        [...]        doSetRollbackOnly(status);    }}

@Transactional Annotation +用于在循环中插入数据

@Transactional Annotation +用于在循环中插入数据

我使用的 春天3,JPA + Hibernate的
一个CMS应用。在该应用程序中,我有一个服务类方法,该方法用@Transactional具有rollBack属性的Annotation进行注释。在该方法内部,我正在使用循环将数据(即实体类)插入表中。对于每个iteration循环实体类,必须将其保存到数据库。但这没有发生。仅当循环执行完成并从方法退出时,才会执行提交。然后,它提交并立即保存所有内容。但是在这种情况下,在提交数据之前,我需要先读取数据。我尝试使用ISOLATIONLEVEL读取未提交,但由于我使用的是默认值,因此不支持JPADialect。还试图添加 hibernate
实现jpaDialect但仍然没有用。请提供解决此问题的方法。还有一件事,有什么方法可以使用传播所需的方法。

答案1

小编典典

删除带有循环的方法上的事务注释。

在循环中调用一个单独的方法执行保存,使该方法具有事务性

java – Spring @Transactional Annotation属性优先级/继承

java – Spring @Transactional Annotation属性优先级/继承

在调用方法本身是transactionnal的情况下,当required传播时,如果它们不同,当前方法是否会覆盖封闭的事务属性(例如rollbackFor)?

插图:

Class A {
    @Transactional(propagation = Propagation.required,rollbackFor = { SomeException.class})
    void foo() {
        try {
           b.bar();
        } catch (OtherException e) {
           // is the transaction marked as rollback-only at this point ?
        }
    }
}

Class B {
    @Transactional(propagation = Propagation.required,rollbackFor = { OtherException.class})
    void bar() {
        [...]
    }
}

编辑:

好吧,我想避免琐碎的超出范围的答案,所以让我们清楚,我知道弹簧传播处理.

如果您不是,下面是文档的相关部分,我只想澄清有关上面示例的第一部分:

PROPAGATION_required

When the propagation setting is PROPAGATION_required,a logical
transaction scope is created for each method upon which the setting is
applied. Each such logical transaction scope can determine
rollback-only status individually,with an outer transaction scope
being logically independent from the inner transaction scope. Of
course,in case of standard PROPAGATION_required behavior,all these
scopes will be mapped to the same physical transaction. So a
rollback-only marker set in the inner transaction scope does affect
the outer transaction’s chance to actually commit (as you would expect
it to).

However,in the case where an inner transaction scope sets the
rollback-only marker,the outer transaction has not decided on the
rollback itself,and so the rollback (silently triggered by the inner
transaction scope) is unexpected. A corresponding
UnexpectedRollbackException is thrown at that point. This is expected
behavior so that the caller of a transaction can never be misled to
assume that a commit was performed when it really was not. So if an
inner transaction (of which the outer caller is not aware) silently
marks a transaction as rollback-only,the outer caller still calls
commit. The outer caller needs to receive an
UnexpectedRollbackException to indicate clearly that a rollback was
performed instead.

我的问题可以改写为:

逻辑事务范围是否包含事务属性?

解决方法

所以,我设置了一个测试用例,简短的回答是肯定的.

事务逻辑范围包含事务属性,其边界确实是带注释的方法属性.

因此,即使两个方法的底层物理事务都相同,逻辑属性也适用于每个方法,而内部方法可以强制回滚外部方法事务.
如果最后一次触发提交,则会导致UnexpectedRollbackException.

比照Spring TransactionInterceptor(评论是我的)

try {
        retVal = invocation.proceed();
}
catch (Throwable ex) {
        completeTransactionAfterThrowing(txInfo,ex);
        throw ex;
}

completeTransactionAfterThrowing():

// txinfo is proper to the invocation target method
if (txInfo.transactionAttribute.rollbackOn(ex)) {
            try {
                txInfo.getTransactionManager().rollback(txInfo.getTransactionStatus());
            }

AbstractPlatformTransactionManager.processRollback():

else if (status.isNewTransaction()) { //requiresnew
    doRollback(status);
}
else if (status.hasTransaction()) { //requiered
        [...]
        doSetRollbackOnly(status);
    }
}

java – 如何扩展Spring Annotation @Transactional

java – 如何扩展Spring Annotation @Transactional

我必须在我的webapp中使用3个不同的事务管理器.所以我根据Spring reference(第10.5.6.3节自定义快捷方式注释)编写了自己的注释.

一个注释(用于使用一个特定的事务管理器)如下所示:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.transaction.annotation.Transactional;

@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("customer")
public @interface CustomerTX{


}

使用自定义的@CustomerTX注释注释我的服务层时,一切正常.但是我必须为我的注释提供更多选项,比如readonly = true,rollbackFor =等等.因为你不能“扩展”一个注释(我真的只需要从Spring扩展@Transactional注释),这是什么正确的实现?

最佳答案
您将不得不创建几个自定义注释,我担心,每个用例都有一个注释,并使用您需要的@Transactional注释来注释每个注释.

或者你必须在AspectJ中编写自己的方面(从spring-aspects.jar扩展org.springframework.transaction.aspectj.AbstractTransactionAspect)来创建自己的事务逻辑.

Java-API-Package:org.springframwork.transaction.annotation

Java-API-Package:org.springframwork.transaction.annotation

ylbtech-Java-API-Package:org.springframwork.transaction.annotation

 

1.返回顶部
1、

@NonNullApi @NonNullFields

Package org.springframework.transaction.annotation

Spring''s support for annotation-based transaction demarcation.

See: Description

  • Interface Summary
    Interface Description
    TransactionAnnotationParser
    Strategy interface for parsing known transaction annotation types.
    TransactionManagementConfigurer
    Interface to be implemented by @ Configuration classes annotated with @ EnableTransactionManagement that wish to or need to explicitly specify the default  PlatformTransactionManager bean to be used for annotation-driven transaction management, as opposed to the default approach of a by-type lookup.
  • Class Summary
    Class Description
    AbstractTransactionManagementConfiguration
    Abstract base  @Configuration class providing common structure for enabling Spring''s annotation-driven transaction management capability.
    AnnotationTransactionAttributeSource
    Implementation of the  TransactionAttributeSource interface for working with transaction metadata in JDK 1.5+ annotation format.
    Ejb3TransactionAnnotationParser
    Strategy implementation for parsing EJB3''s  TransactionAttribute annotation.
    JtaTransactionAnnotationParser
    Strategy implementation for parsing JTA 1.2''s  Transactional annotation.
    ProxyTransactionManagementConfiguration
    @Configuration class that registers the Spring infrastructure beans necessary to enable proxy-based annotation-driven transaction management.
    SpringTransactionAnnotationParser
    Strategy implementation for parsing Spring''s  Transactional annotation.
    TransactionManagementConfigurationSelector
    Selects which implementation of  AbstractTransactionManagementConfiguration should be used based on the value of  EnableTransactionManagement.mode() on the importing  @Configuration class.
  • Enum Summary
    Enum Description
    Isolation
    Enumeration that represents transaction isolation levels for use with the  Transactional annotation, corresponding to the  TransactionDefinitioninterface.
    Propagation
    Enumeration that represents transaction propagation behaviors for use with the  Transactional annotation, corresponding to the  TransactionDefinitioninterface.
  • Annotation Types Summary
    Annotation Type Description
    EnableTransactionManagement
    Enables Spring''s annotation-driven transaction management capability, similar to the support found in Spring''s  <tx:*> XML namespace.
    Transactional
    Describes a transaction attribute on an individual method or on a class.

Package org.springframework.transaction.annotation Description

Spring''s support for annotation-based transaction demarcation. Hooked into Spring''s transaction interception infrastructure via a special TransactionAttributeSource implementation.
2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
0、
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/annotation/package-summary.html
1、
 
6.返回顶部
 
warn 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

今天的关于Spring @Transactional Annotation属性的优先级/继承spring优先级order的分享已经结束,谢谢您的关注,如果想了解更多关于@Transactional Annotation +用于在循环中插入数据、java – Spring @Transactional Annotation属性优先级/继承、java – 如何扩展Spring Annotation @Transactional、Java-API-Package:org.springframwork.transaction.annotation的相关知识,请在本站进行查询。

本文标签: