jimmayhem
jimmayhem

Reputation: 405

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. 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. 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.

Upvotes: 0

Views: 163

Answers (1)

Daniele Baggio
Daniele Baggio

Reputation: 2257

By default in Liferay, every method at LocalService level is transactional.

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. Here is not a Spring enviroment.

Upvotes: 1

Related Questions