掲示板

calling liferay-services within one transaction

15年前 に Joerg Schaefer によって更新されました。

calling liferay-services within one transaction

Junior Member 投稿: 60 参加年月日: 08/05/05 最新の投稿
Hi,
i want to implement a service-method in my portlet who calls more than one liferay-services.
For example:


// start transaction
JournalStructureLocalServiceUtil.copyStructure...

JournalStructureLocalServiceUtil.updateJournalStructure...

JournalTemplateLocalServiceUtil.copyTemplate...
// end transation


What i want to do now is calling these service-methods within one transaction.
Is this possible?
What are the steps i have to do?

Thanks Joerg
13年前 に Daniele Segato によって更新されました。

RE: calling liferay-services within one transaction

New Member 投稿: 12 参加年月日: 09/10/05 最新の投稿
Hi Joerg,

yes it's possible but you have to do it inside a LocalService.

If you have a custom entity created with the service builder you should have a YourEntityLocalServiceImpl somewhere, just add a new method doing all you need and calling multiple services inside it.

Then re-execute the service builder on your entity to made it build the LocalService chain (YourEntityLocalService interface, YourEntityLocalServiceUtil).

see http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Service+Builder

Then you may call your method, that's work in a single transaction (atomic transaction).


This is possible because there are Hibernate interceptor for each persistence service you use. If the method has been called inside an already opened transaction it do NOT open a new one.

You may want to use BatchSessionUtil if you plan to do a lot of operation inside your method otherwise just remove the BatchSessionUtil lines from the example.

Here the example:

/* this is inside your LocalServiceImpl */
public void yourMethod() throws SystemException {
  try {
    BatchSessionUtil.setEnabled(true);

    /* your stuff here */
    JournalStructureLocalServiceUtil.copyStructure...

    JournalStructureLocalServiceUtil.updateJournalStructure...

    JournalTemplateLocalServiceUtil.copyTemplate...

    /* .... */

    /* any exception at any point cause the rollback of the entire work */
  }
  finally {
    BatchSessionUtil.setEnabled(false);
  }
}


You may obtain the same result without creating an entity by creating a new bean replicating what a service builder does, but that's a little more complicated.

I created an interface and a method in one of my entity to be able to use it whatever I need without having to create a new method in the entity.

That's up to you.
I hope this help everybody else with this needs.
thumbnail
11年前 に Tonu Sri によって更新されました。

RE: calling liferay-services within one transaction

Regular Member 投稿: 197 参加年月日: 11/04/15 最新の投稿
Hi Dainele,

Is there any recommended way?
Which method Liferay prefer for Atomic Transaction.

Thanks: