在Play中将@Transactional注释放在何处 [英] Where to place @Transactional annotation in Play

查看:149
本文介绍了在Play中将@Transactional注释放在何处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Play应用程序与数据库连接,我遵循的是controller-> service-> DAO图层样式.

I am trying to connect my Play application with a database, I am following controller->service->DAO layer style.

@Transactional批注应放在何处.在控制器操作方法中还是可以将其放在服务层类级别/方法级别中.

Where should the @Transactional annotation be placed. In the controller action method or can I place it in a service layer class level/method level.

我尝试将@Transactional批注保留在Service层类的方法之一中,但它引发了运行时错误.

I tried keeping the @Transactional annotation in one of the Service layer class's method and it has thrown runtime error.

[RuntimeException:在上下文中找不到EntityManager.尝试 用@ play.db.jpa.Transactional注释您的操作方法

[RuntimeException: No EntityManager found in the context. Try to annotate your action method with @play.db.jpa.Transactional]

那么,我们是否必须仅在控制器操作之上使用@Transactional?我正在使用播放2.4.2版本.如果是,为什么?当我在服务层中调用DAO时,实际的数据库操作将在服务层而不是控制器中开始.在控制器中,我只是在调用服务层.

So, do we have to use @Transactional only on top of the controller actions? I am using play 2.4.2 version. If yes, why? As I am calling my DAO's in service layer the actual database operations will begin in service layer not in controller I guess. In controller, I am just calling the service layer.

推荐答案

我建议不要使用@Transactional,因为它会将整个操作包装在JPA事务中.在靠近数据库调用的地方开始事务会更有效.

I would recommend not to use @Transactional because it wraps entire action in an JPA transaction. It would be more efficient to start the transaction closer to db call.

我更喜欢从服务级别开始交易.同样需要注意的是,数据库调用是一个阻塞过程,因此必须在默认操作上下文之外执行.

I prefer starting the transaction at service level. Also it is important to note that db call is a blocking process so it must be executed outside of default action context.

看看简单的DAO + Service示例 https://gist.github.com/dzagorovsky/b8064c97ba647ed453ab

Take a look at simple DAO+Service example https://gist.github.com/dzagorovsky/b8064c97ba647ed453ab

另请在此处阅读有关播放线程池的信息 https://www.playframework.com/documentation/2.4.x/ThreadPools#Using-other-thread-pools

Also read about play thread pools here https://www.playframework.com/documentation/2.4.x/ThreadPools#Using-other-thread-pools

此处描述的阻止代码(数据库调用)处理: https://www.playframework. com/documentation/2.3.x/JavaAsync

Blocking code (db calls) handling described here: https://www.playframework.com/documentation/2.3.x/JavaAsync

这篇关于在Play中将@Transactional注释放在何处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆