java Spring Data CrudRepository 和事务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29621846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:36:46  来源:igfitidea点击:

Spring Data CrudRepository and Transactions

javaspringtransactionsspring-boot

提问by Jonathan Crégut

I'm trying to implement transactions on a CrudRepository Interface. I'm a beginner with this and my current problem is that when receiving a lot of requests from different clients, I'm sometimes getting a duplicate. To avoid that I wanted to use SQL Transactions and their implementation with Spring but I'm unable to get it working.

我正在尝试在 CrudRepository 接口上实现事务。我是这方面的初学者,我目前的问题是,当收到来自不同客户的大量请求时,有时会收到重复的请求。为了避免这种情况,我想在 Spring 中使用 SQL 事务及其实现,但我无法让它工作。

Here is how I've tried to do it :

这是我尝试这样做的方法:

@Repository
@EnableTransactionManagement
@Transactional
public interface ApplicationPackageDao extends CrudRepository<ApplicationPackage, Long> {

/**
 * Find if a record exists for this package name ,
 * @param packageName
 * @return
 */
@Transactional
ApplicationPackage findByPackageName(String packageName);

}

However it doesn't seem to work. I tried to add the @Transactionnal annotations earlier in the Java methods I'm calling but I can't get it working either.

但是,它似乎不起作用。我之前尝试在我调用的 Java 方法中添加 @Transactionnal 注释,但我也无法使其正常工作。

How am I supposed to work with transactions on CrudRepository ? Or am I using completely the wrong thing?

我应该如何处理 CrudRepository 上的事务?还是我使用完全错误的东西?

采纳答案by crm86

What I suggest:

我的建议:

Check your context and configuration classes with @Configuration annotation. From the documentation:

使用 @Configuration 注释检查您的上下文和配置类。从文档

The @EnableTransactionManagement annotation provides equivalent support if you are using Java based configuration. Simply add the annotation to a @Configuration class

@EnableTransactionManagement and only looks for @Transactional on beans in the same application context they are defined in

如果您使用基于 Java 的配置,@EnableTransactionManagement 注释提供等效支持。只需将注释添加到 @Configuration 类

@EnableTransactionManagement 并且只在 bean 定义的相同应用程序上下文中查找 @Transactional

Then you could use @Transactional in your service even in a method

然后你可以在你的服务中使用 @Transactional 甚至在一个方法中

Hope it helps

希望能帮助到你

回答by Heri

In addition to crm86's answer some more notes to the @Transactional annotation:

除了 crm86 的回答之外,还有一些对@Transactional 注释的注释:

  • It seems to be best practice to annotate the entry points into your application (e.g. your web controller methods or the main method of a scheduled batch). By using the annotation attribute TxType you can ensure constraints/conditions in methods which are located deeper in your application (e.g. TxType.MANDATORY would throw if no trx-context is running, etc.).

  • The @Transactional annotation has only an effect if the class is loaded as spring bean (e.g. @Component annotation at class level).

  • Remember that only RuntimeException's lead to a rollback. If you want a checked Exception leading to a rollback you have to enumerate each such Exception by using the attribute rollbackOn.

  • The annotation at class level is valid for all public methods of this class. Method level annotations override those at the class level. The repeated annotation in your example above (first at class level, then at method level) has no effect.

  • 注释应用程序的入口点似乎是最佳实践(例如,您的 Web 控制器方法或预定批处理的主要方法)。通过使用注释属性 TxType,您可以确保位于应用程序更深处的方法中的约束/条件(例如,如果没有 trx-context 正在运行,TxType.MANDATORY 将抛出,等等)。

  • @Transactional 注解只有在类作为 spring bean 加载时才有效(例如类级别的 @Component 注解)。

  • 请记住,只有 RuntimeException 会导致回滚。如果您想要导致回滚的已检查异常,您必须使用属性 rollbackOn 枚举每个此类异常。

  • 类级别的注解对该类的所有公共方法都有效。方法级别的注释覆盖了类级别的注释。上面示例中的重复注释(首先在类级别,然后在方法级别)不起作用。