简体   繁体   English

如何在 Liferay 7.1 中使方法具有事务性以操作实体

[英]How to make method transactional to manipulate entities in Liferay 7.1

I need to do multiple writes to DB under single transaction using Liferay 7.1.我需要使用 Liferay 7.1 在单个事务下对 DB 进行多次写入。 Basically, my question is would this work?基本上,我的问题是这行得通吗?

@Component(service = MyService.class)
public class MyService {

    private OrganizationLocalService localService;

    @Reference(unbind = "-")
    protected void setOrganizationLocalService(OrganizationLocalService localService) {
        this.localService = localService;
    }

    @Transactional(rollbackFor = IllegalArgumentException.class)
    public void doInTransaction() {
        try {
            localService.createOrganization(...);
            localService.updateOrganization(...);
            // more
        catch (IllegalArgumentException e) {
            // rollback logic
        }
    }
}

There are also Liferay event listeners built to be part of the service calls used to manipulate Liferay entities.还有一些 Liferay 事件监听器被构建为用于操作 Liferay 实体的服务调用的一部分。 Those event listeners will do additional work like sending messages to Kafka topics, etc. And I am not sure if introducing transactions would not disrupt the work of these listeners.这些事件监听器将做额外的工作,比如向 Kafka 主题发送消息等。我不确定引入事务是否会干扰这些监听器的工作。

By default in Liferay, every method at LocalService level is transactional.在 Liferay 中,默认情况下,LocalService 级别的每个方法都是事务性的。

Then, you have to collect all tasks in a single localservice method to ensure a single transactional enviroment.然后,您必须在单个本地服务方法中收集所有任务以确保单个事务环境。

@Transactional annotation is not effective as you have tryed to do. @Transactional 注释不像您尝试的那样有效。 Here is not a Spring enviroment.这里不是 Spring 环境。

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

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