Laxman
Laxman

Reputation: 81

How to implement transactions in liferay?

In liferay i have a requirement like if i am updating the roles of multiple users if one of them updating the role of the user is failed then i want to rollback all the updated roles of users. i have applied as follows.

@Transactional(isolation = Isolation.SERIALIZABLE,
            propagation = Propagation.REQUIRES_NEW)
    public int updateUserRole(long userId,long groupId,long roleId) throws   SystemException{
        try{
            return UserTokenFinderUtil.updateUserRole(userId,groupId,roleId);
        }
        catch(Exception e){
            System.out.println("Exception occured UserTokenServiceImpl");
            e.printStackTrace();
            return -1;
        }

    }

Can anyone help me out with fresh eyes?

Upvotes: 1

Views: 2614

Answers (2)

Gautam
Gautam

Reputation: 3384

This should be handled by default by service builder.

for that you should be using LocalServiceImpl class rather than *Util class

The entry point to transaction in Liferay is the *LocalServiceImpl class. DML operations update, insert and delete on one Entity from another do not use calls to LocalServiceUtil or LocalService as this will result in two transaction boundaries.

You can refer to below Link for more infor.

Transaction Management with liferay service

Upvotes: 1

Alexandre FILLATRE
Alexandre FILLATRE

Reputation: 1327

The best way to do that is doing it in a custom service (i.e. ServiceBuilder) method. Something like MyCustomServiceUtil.addRoles(). Transactions are managed by Liferay in this case, and you'll get the expected result.

Upvotes: 1

Related Questions