RE: Enable @Transaction on OSGI services

thumbnail
Eric COQUELIN, modified 6 Years ago. Expert Posts: 254 Join Date: 11/3/13 Recent Posts
Dear community,

I have created my own service which is an interface as usual from which my implementation inherits.

It gives me the following snippet

@Transactional(isolation = Isolation.PORTAL, rollbackFor =  {
        PortalException.class, SystemException.class}, enabled= true)
public interface NotificationService {

  public void mymethod();
  ...

}

But when executing any of the methods of the implementation of this interface, there is no transaction.

Same annotation is being used by the service builder and it works perfectly.

I am afraid it has something to do with the service builder being based on Spring...

Can you please help ?
thumbnail
David H Nebinger, modified 6 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Well, the difference really is that the SB classes are not OSGi components, they are Spring Beans. As part of the Spring echosystem, the appropriate transaction wrapping occurs.

We see the SB services as OSGi components because Liferay explicitly exposes the Spring Beans defined in a context as component manually.

The easiest solution is to build in your own services as ServiceBuilder no-column entities or even as Fake Entities. This way you get all the benefit of SB (including the txn management) without the corresponding database table(s).
thumbnail
Eric COQUELIN, modified 6 Years ago. Expert Posts: 254 Join Date: 11/3/13 Recent Posts
Hi David,

If I correctly understood your answer, I cannot setup transactionnal methods outside of the service builder.

Thank you !
thumbnail
Olaf Kock, modified 6 Years ago. Liferay Legend Posts: 6441 Join Date: 9/23/08 Recent Posts
Eric COQUELIN

If I correctly understood your answer, I cannot setup transactionnal methods outside of the service builder.
Well, I guess you can. but it's complicated (TM)
ServiceBuilder has it all implemented already, and it's by far easier (and more futureproof / easier to maintain) to just hook into the same mechanism than to come up with your own.