您应该将@Transactional放在DAO类和/或它们的方法中,还是更好地注释使用DAO对象调用的服务类?或者对两个“层”都加注释有意义吗?


当前回答

@Transactional注释应该放在所有不可分割的操作周围。 使用@Transactional可以自动处理事务传播。在这种情况下,如果当前方法调用了另一个方法,那么该方法将可以选择加入正在进行的事务。

让我们举个例子:

我们有两个模型,即国家和城市。国家和城市模型的关系映射就像一个国家可以有多个城市,所以映射就像,

@OneToMany(fetch = FetchType.LAZY, mappedBy="country")
private Set<City> cities;

在这里国家映射到多个城市,获取他们懒惰。这里是@Transactinal的作用当我们从数据库中检索Country对象时,我们会得到Country对象的所有数据,但不会得到城市的集合,因为我们是懒洋洋地获取城市。

//Without @Transactional
public Country getCountry(){
   Country country = countryRepository.getCountry();
   //After getting Country Object connection between countryRepository and database is Closed 
}

当我们想要从国家对象访问Set of Cities时,我们将在该Set中获得空值,因为只有该Set创建的对象没有初始化该Set的数据来获取Set的值,我们使用@Transactional,即,

//with @Transactional
@Transactional
public Country getCountry(){
   Country country = countryRepository.getCountry();
   //below when we initialize cities using object country so that directly communicate with database and retrieve all cities from database this happens just because of @Transactinal
   Object object = country.getCities().size();   
}

所以基本上@Transactional is Service可以在单个事务中进行多个调用,而无需关闭与端点的连接。

其他回答

服务层中的@事务使用是通过使用控制器层(@Controller)调用的,服务层调用DAO层(@Repository),即数据库相关的操作。

@Transactional注释应该放在所有不可分割的操作周围。 使用@Transactional可以自动处理事务传播。在这种情况下,如果当前方法调用了另一个方法,那么该方法将可以选择加入正在进行的事务。

让我们举个例子:

我们有两个模型,即国家和城市。国家和城市模型的关系映射就像一个国家可以有多个城市,所以映射就像,

@OneToMany(fetch = FetchType.LAZY, mappedBy="country")
private Set<City> cities;

在这里国家映射到多个城市,获取他们懒惰。这里是@Transactinal的作用当我们从数据库中检索Country对象时,我们会得到Country对象的所有数据,但不会得到城市的集合,因为我们是懒洋洋地获取城市。

//Without @Transactional
public Country getCountry(){
   Country country = countryRepository.getCountry();
   //After getting Country Object connection between countryRepository and database is Closed 
}

当我们想要从国家对象访问Set of Cities时,我们将在该Set中获得空值,因为只有该Set创建的对象没有初始化该Set的数据来获取Set的值,我们使用@Transactional,即,

//with @Transactional
@Transactional
public Country getCountry(){
   Country country = countryRepository.getCountry();
   //below when we initialize cities using object country so that directly communicate with database and retrieve all cities from database this happens just because of @Transactinal
   Object object = country.getCities().size();   
}

所以基本上@Transactional is Service可以在单个事务中进行多个调用,而无需关闭与端点的连接。

@Transactional应该用于服务层,因为它包含业务逻辑。DAO层通常只有数据库CRUD操作。

// the service class that we want to make transactional
@Transactional
public class DefaultFooService implements FooService {

    Foo getFoo(String fooName);

    Foo getFoo(String fooName, String barName);

    void insertFoo(Foo foo);

    void updateFoo(Foo foo);
}

春季文档:https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/transaction.html

用于数据库级别的事务

大多数情况下,我在DAO的方法级别上使用@Transactional,所以配置可以专门用于一个方法/使用默认值(必需的)

DAO获取数据的方法(select ..)-不需要 @事务性这可能会导致一些开销,因为 事务拦截器/和需要执行的AOP代理 好。 DAO的插入/更新方法将获得@Transactional

非常好的博客

应用级别: 我正在使用事务性业务逻辑,我希望能够在发生意外错误的情况下回滚

@Transactional(rollbackFor={MyApplicationException.class})
public void myMethod(){

    try {    
        //service logic here     
    } catch(Throwable e) {

        log.error(e)
        throw new MyApplicationException(..);
    }
}

事务注释应该放在所有不可分割的操作周围。

例如,你的呼叫是“change password”。它由两个操作组成

修改密码。 审计变更。 通过电子邮件通知客户端密码已更改。

那么在上面,如果审计失败,那么修改密码也应该失败吗?如果是这样,那么事务应该在1和2左右(因此在服务层)。如果电子邮件失败了(可能应该有某种故障安全措施,这样它就不会失败),那么它是否应该回滚更改密码和审计?

在决定将@Transactional放在哪里时,您需要询问这些问题。