简体   繁体   English

JpaRepository上的@Transactional

[英]@Transactional on a JpaRepository

I'm using a read only database to get some data in to my project. 我正在使用只读数据库来获取我的项目中的一些数据。 We use Spring v3 with jpa and hibernate 我们使用Spring v3和jpa以及hibernate

Will the following annotation have the effect of making all calls to my repository a read only transaction? 以下注释是否会使对我的存储库的所有调用都成为只读事务? Or do I need the annotation on the service layer calling the repository 或者我是否需要调用存储库的服务层上的注释

package com.blah.jpa;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;

@Transactional(readOnly = true)
public interface ServiceToServerRepository extends JpaRepository<ServiceToServerJPA, String> {

}

So we get all the findOne, findAll for free. 所以我们得到了所有的findOne,findAll是免费的。 But any updates should fail as it is a read only transaction 但任何更新都应该失败,因为它是一个只读事务

That should basically do it. 基本上应该这样做。 But I usually opt for marking service-layer artifacts with Transactional annotation. 但我通常选择使用Transactional注释标记服务层工件。 Anyway, please also check this post for more information of using transactionality together with Spring Data: How to use @Transactional with Spring Data? 无论如何,请查看这篇文章,了解有关使用事务性和Spring Data的更多信息: 如何将@Transactional与Spring Data一起使用?

The readOnly flag only hints that it is sufficient with a read only transaction while calling the method. readOnly标志仅提示在调用方法时只读事务就足够了。

If you have a nested transaction from your service layer which is not read only, then it would use that one, and you will effectively disregard this annotation. 如果您的服务层中的嵌套事务不是只读的,那么它将使用该事务,您将有效地忽略此注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM