Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Joerg Schaefer
calling liferay-services within one transaction
January 30, 2009 1:05 AM
Answer

Joerg Schaefer

Rank: Junior Member

Posts: 60

Join Date: May 5, 2008

Recent Posts

Hi,
i want to implement a service-method in my portlet who calls more than one liferay-services.
For example:

1
2// start transaction
3JournalStructureLocalServiceUtil.copyStructure...
4
5JournalStructureLocalServiceUtil.updateJournalStructure...
6
7JournalTemplateLocalServiceUtil.copyTemplate...
8// 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
Daniele Segato
RE: calling liferay-services within one transaction
May 5, 2010 12:45 AM
Answer

Daniele Segato

Rank: New Member

Posts: 12

Join Date: October 5, 2009

Recent Posts

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:
 1
 2/* this is inside your LocalServiceImpl */
 3public void yourMethod() throws SystemException {
 4  try {
 5    BatchSessionUtil.setEnabled(true);
 6
 7    /* your stuff here */
 8    JournalStructureLocalServiceUtil.copyStructure...
 9
10    JournalStructureLocalServiceUtil.updateJournalStructure...
11
12    JournalTemplateLocalServiceUtil.copyTemplate...
13
14    /* .... */
15
16    /* any exception at any point cause the rollback of the entire work */
17  }
18  finally {
19    BatchSessionUtil.setEnabled(false);
20  }
21}


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.
Ankit Srivastava
RE: calling liferay-services within one transaction
October 16, 2012 9:34 PM
Answer

Ankit Srivastava

Rank: Regular Member

Posts: 197

Join Date: April 15, 2011

Recent Posts

Hi Dainele,

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

Thanks:
Federico Miralles
RE: calling liferay-services within one transaction
March 12, 2013 10:36 AM
Answer

Federico Miralles

Rank: New Member

Posts: 7

Join Date: September 20, 2012

Recent Posts

Hi Tonu,

You could check:

Liferay Transactional Custom Services Made Easy

Federico Miralles @Rotterdam CS BV