留言板

How to implement transaction on MVCPortlet

thumbnail
Yoshikazu Kobayashi,修改在7 年前。

How to implement transaction on MVCPortlet

New Member 帖子: 20 加入日期: 13-8-26 最近的帖子
When updating multiple models , I realized transaction processing as follows.

① Create updating process for each model with service builder
Model1LocalServiceImpl#update (model1) {
 ...
 }
Model2LocalServiceImpl#update (model2) {
 ...
}


② Create update method for multiple models in service builder
Model1LocalServiceImpl#updateAll (model1, model2) {
  Model1LocalService.update (model1);
  Model2LocalService.update (model2);
}


② Call the above method from the class inheriting Base MVCActionCommand.
MyBaseMVCActionCommand#processAction {
  Model1LocalService.updateAll(model1, model2);
}


■On the other hand, I found BaseTransactionalMVCActionCommand in the source code of Liferay DXP SP1.
Transaction processing seems to be possible if transaction processing is implemented as follows.

MyBaseTransactionalMVCActionCommand # doTransactionalCommand {
  Model1LocalService.update (model1);
  Model2LocalService.update (model2);
}


As you can see, there are two ways to inherit BaseTransactionalMVCActionCommand and create update method for multiple service builder models, which one should be used?
thumbnail
David H Nebinger,修改在7 年前。

RE: How to implement transaction on MVCPortlet (答复)

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
The first one, transactions should apply around the database access, not the MVC action layer.

The reason is that you cannot guarantee that future developers will use the BaseTransactionalMVCActionCommand to provide a transactional wrapper around the calls. They might use it in, say, a scheduler or in a serve resource action or a normal MVC action command or ...

As soon as they do something like that, the txn logic is lost.

If you wrap the calls in your normal SB tier, the transaction handling will be automagic and won't matter where they call it from.

Remember that transactions are a database detail, not an MVC detail.