Transaction Management with Liferay Service

Following are the important points that a Liferay Architect must make note of in order to work with Transactions in Liferay

  1. The entry point to transaction (start of transaction boundary) in Liferay service is the <Entity>LocalServiceImpl class. Hence, while firing DML oriented methods like update, insert and delete on X Entity from Y do not use make calls to XLocalServiceUtil or XLocalService as this will result in two transaction boundaries - one for entity X and the other for entity Y. Relate the two entities through service.xml file (one to many or many to many based on your domain model) or else refer one entity in the other using <reference... in service.xml file. For e.g. if you want to make call on X entity from YLocalServiceImpl then in service.xml file while configuring Y Entity put

    <reference entity="X" package-path="com.company.project.X"></reference>

    On building the service, you will have access to xPersistence in YLocalServiceImpl. You can now make calls to xPersistence for update, insert and delete. <reference... tag is exactly for this reason. You can also include something like <reference entity="User" package-path="com.liferay.portal"></reference> to get the userPersistence object in your Entity. Then the calls to your entity and userPersistence will be the part of same transaction. In other words, updates to your tables and user_ table will be part of same transaction - either everything will commit or evrerything will be rolledback on encounter PortalException or SystemException.
  2. Ensure that your database supports transactions; yes ! this is needless to say but then if you are using MySQL, the database engine other MyISAM does not support transactions. You must use InnoDB as the database engine for your tables.
  3. As you know Liferay Entity code is generated by service builder and you get an interface called XLocalService. It is this interface which is marked with Transactional Annotation. It is set such that if you throw PortalException or SystemException from any of the methods, Hibernate Transaction Manager will rollback the transaction. Hence on violation of any business rule in the custom business-service method of XLocalServiceImpl if you want to rollback the transaction, you must throw either PortalException or SystemException.

FROM: http://www.liferayaddicts.net/blogs/-/blogs/transaction-management-with-liferay-service

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值