留言板

Transaction roolback/commit with entity

AA CC,修改在12 年前。

Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
Hi,

i have the following problem :

i have two entity MyContent and MyUser.
In my operation, using the class of service builder, i insert first a MyUserand after MyContent.

My problem is that, if the insert of the user is successful but not the insert of content, I should make a roolback, conversely a commit. It 'can ?

thank you
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
Help me please
thumbnail
Thiago Leão Moreira,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1449 加入日期: 07-10-10 最近的帖子
This should be handled by default by service builder. How are you adding both entities? Show us the code.
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
public String getContentUrl(String tokenUser,String contentId,String channel)
throws SystemException, PortalException,NoCreditUserException,NoSuchArgUserLoggedException{


ArgContentPurchase argContentPurchase = ArgContentPurchaseLocalServiceUtil
.createArgContentPurchase((int)CounterLocalServiceUtil.increment(ArgContentPurchase.class.getName()));

argContentPurchase.setArguserId(argUserLogged.getArguserId());
argContentPurchase.setArgcontentId(Integer.parseInt(contentId));
argContentPurchase.setCreationdatetime(new Date());
argContentPurchase.setLastmodifieddatetime(new Date());
argContentPurchase.setViewnum((short)0);
argContentPurchase.setExpired(false);
argContentPurchase.setToken(PortalUUIDUtil.generate());

ArgContentPurchaseLocalServiceUtil.addArgContentPurchase(argContentPurchase);
ArgContentPurchaseLocalServiceUtil.addArgContentPurchase(null);


return getContentLinkFromArgContentPurchase(argContentPurchase);
}

I add my content in blue line, afert in red line; i force an error, but the content added in blue line doesn't rollback.
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
Please help me!!!
thumbnail
Amos Fong,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 2047 加入日期: 08-10-7 最近的帖子
Is this code inside your localServiceImpl? If no, then then it won't those methods won't be in the same transaction.

If yes, then you shouldn't be using the LocalServiceUtil. methods, that will take you out of the transaction I think, use LocalService or Persistence methods instead.

For example:

ArgContentPurchasePersistence.update(argContentPurchase, false);
ArgContentPurchasePersistence.update(null, false);
thumbnail
David García González,修改在12 年前。

RE: Transaction roolback/commit with entity

Regular Member 帖子: 127 加入日期: 09-7-14 最近的帖子
Thanks Amos Fong, I didnt realize that calling LocalServiceUtil I was starting a new transaction.

I have seen in another examples the following trick:

try {
			BatchSessionUtil.setEnabled(true);
			//Call persistance methods
		} catch (SystemException e) {
			logger.error("Ha ocurrido un error al crear un aviso", e);
			throw e;
		} finally {
			BatchSessionUtil.setEnabled(false);
		} 


Is this recommended?
thumbnail
Thiago Leão Moreira,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1449 加入日期: 07-10-10 最近的帖子
Hey David,

I would say that this is not recommended. The right way to do it is inside a LocalServiceImpl method calling your other services using the references injected by Liferay.

public class JediLocalServiceImpl extends ... {
 
  public void addJedi(...) {
    userLocalService.addUser(...)
    lightsaberLocalService.addLightsaber(..)
  }
}


Don't forget to configure these two services in your service.xml using the tag <reference>.
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
Sorry i don't understand . I write all code from my methods that i want to put in transaction in red critical operation.

	public String getContentUrl(String tokenUser,String contentId,String channel) 
			throws SystemException, PortalException,NoCreditUserException,NoSuchArgUserLoggedException{
	
		
		//Chek if user logged 
		ArgUserLogged argUserLogged =null; 
		argUserLogged= ArgUserLoggedLocalServiceUtil.getArgUserLoggedByValidToken(tokenUser, channel);

		
		//This DynamicQuery get all argContentPurchase from ArguserId
		DynamicQuery argContentPurchaseQuery = DynamicQueryFactoryUtil.forClass(ArgContentPurchase.class,
				"argcontentpurchase",getClass().getClassLoader())
				.add(PropertyFactoryUtil.forName("argcontentpurchase.arguserId").eq(argUserLogged.getArguserId()))
				.add(PropertyFactoryUtil.forName("argcontentpurchase.argcontentId").eq(Integer.parseInt(contentId)))
				.add(PropertyFactoryUtil.forName("argcontentpurchase.expired").ne(true));  
		
		//get related content
		List<argcontentpurchase> argContentPurchases;

		argContentPurchases = ArgContentPurchaseLocalServiceUtil.dynamicQuery(argContentPurchaseQuery);

		ArgContentPurchase argContentPurchase = null;
		if(argContentPurchases.size() &gt;0)
			argContentPurchase = argContentPurchases.get(0);
		
		//if argContentPurchase exist and is valid increate the viewnum
		if(argContentPurchases.size() &gt; 0 &amp;&amp; argContentPurchase.isValid()){
			[color=#ff0202]ArgContentPurchaseLocalServiceUtil.getService().updateArgContentPurchase( 
					argContentPurchase.increaseViewNum());[/color]
		//if argContentPurchase is invalid or doesn't exist, create new entry
		}else{
			//before create new entry set content field expired=true;
			[color=#ff0202]ArgContentPurchaseLocalServiceUtil.getService()
					.updateArgContentPurchase(argContentPurchase.setToExpired());[/color]
			
			//check is user has credit
			[color=#ff0000]purchaseArgContent(contentId, argUserLogged);[/color]
		}

		return [color=#1900ff]argContentPurchase.getArgoContent().getContentpath()[/color];

	}
  
  	private void purchaseArgContent(String contentId, ArgUserLogged argUserLogged)
			throws NumberFormatException, PortalException, SystemException,NoCreditUserException {
		
		ArgContent argContent;
    
		argContent = ArgContentLocalServiceUtil.getArgContent(Integer.parseInt(contentId));


		if(!argUserLogged.getArgUser().isCreditEnough(argContent.getArgpriceId())){
			throw new NoCreditUserException();

		ArgContentPurchase argContentPurchase;

		argContentPurchase = ArgContentPurchaseLocalServiceUtil.getService()
			.createArgContentPurchase((int)CounterLocalServiceUtil.increment(ArgContentPurchase.class.getName()));

		argContentPurchase.setArguserId(argUserLogged.getArguserId());
		argContentPurchase.setArgcontentId(Integer.parseInt(contentId));
		argContentPurchase.setCreationdatetime(new Date());
		argContentPurchase.setLastmodifieddatetime(new Date());
		argContentPurchase.setViewnum((short)1);
		argContentPurchase.setExpired(false); 
		argContentPurchase.setToken(PortalUUIDUtil.generate());
		
		[color=#ff0000]ArgContentPurchaseLocalServiceUtil.getService().addArgContentPurchase(argContentPurchase);

	
		ArgUserLocalServiceUtil.getService().decreaseCredit(argUserLogged.getArguserId(), 
					ArgContentLocalServiceUtil.getArgContent(argContentPurchase.getArgcontentId()).getArgpriceId());[/color]

	}
</argcontentpurchase>

ArgContentPurchase and ArgContentPurchase are in the same ServiceBuilder, then ArgUser in other.
For example if the blue line thows an exception i want to rollback the red line.
I don't unserstand what i had to do.
Sorry but i'm a newbie.
Help me
thank you
thumbnail
Thiago Leão Moreira,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1449 加入日期: 07-10-10 最近的帖子
Hey AA CC,

You shouldn't use the LocalServiceUtil classes. See my previous example that I didn't use those...
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
i'm not usin LocalServiceUtil but locaService, because i get the service from LocalServiceUtil like this ArgContentPurchaseLocalServiceUtil.getService().
It's right ?
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
Sorry but it's dosn't works .

I'm in ArgoContentLocalServiceImpl, in the function addArgContent i try to put only the insert of ArgContent entity but it doesn't work this is the code.
Before the return of the function I expect that no row on database are added . It will be add only ofter the return of the function if no error appends right ?


	//Add ArgContet and related AssetEntry
	public ArgContent addArgConteAsset(String channel,long vocabularyId,String tokenUser,int argproviderId,String contentpath,
			String description,int duration,Boolean preview,String previewpath,String thumbpath,
			String title,Boolean top,String[] categoriesName,String[] tagNames)throws SystemException, PortalException{
		
		//Create argContent
		ArgContent argContent = null;
		try {
			argContent = argContentLocalService.createArgContent(
					(int)CounterLocalServiceUtil.increment(ArgContent.class.getName()));
//			argContent = ArgContentLocalServiceUtil.createArgContent(
//						(int)CounterLocalServiceUtil.increment(ArgContent.class.getName()));
		} catch (SystemException exc) {
			exc.printStackTrace();
			throw exc;
		} 
		
		argContent.setArgproviderId(argproviderId);
		argContent.setContentpath(contentpath);
		argContent.setDescription(description);
		argContent.setDuration(duration);
		argContent.setLruserId(10169);
		argContent.setLruserUuid("4a24490e-fd49-4519-8eb2-b7b18ba75320");
		argContent.setPreview(preview);
		argContent.setPreviewpath(previewpath);
		argContent.setThumbpath(thumbpath);
		argContent.setTitle(title);
		argContent.setTop(top);

		//Add ArgContent
		try {
			argContentLocalService.addArgContent(argContent);
			//ArgContentLocalServiceUtil.addArgContent(argContent);
		} catch (SystemException exc) {
			log.error("Error adding ArgContentL",exc);
			throw exc;
		}
		
		return argContent;
	}


I don't know what to do

Thank you
thumbnail
Mika Koivisto,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1519 加入日期: 06-8-7 最近的帖子
The data is added to the database but if an exception (SystemException or PortalException) occurs it is rolledback from the database. For this to work you need a database engine that supports transactions such as MySQL with InnoDB.
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
yes it's support transaction infact for example for the AssetEntity it's work's, but form my entity it's does't work.
thumbnail
Mika Koivisto,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1519 加入日期: 06-8-7 最近的帖子
Which Liferay version are you using? I suspect it doesn't have the improvement we did to allow plugins participate in the same transaction as the portal.
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
The versione is 6.0.6
thumbnail
Mika Koivisto,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1519 加入日期: 06-8-7 最近的帖子
Yeah just like I thought. It's fixed in upcoming 6.1 and latest 5.2 and 6.0 EE versions. The issue I'm talking about is this: http://issues.liferay.com/browse/LPS-15904
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
thank you so much .

Small Off Topic when is then realize date of 6.1 , because if is in august, i don't spend time to resolve this problem.

thank you so much .
thumbnail
Mika Koivisto,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1519 加入日期: 06-8-7 最近的帖子
August is still the target date but I wouldn't expect it early August but rather end of August.
thumbnail
David García González,修改在12 年前。

RE: Transaction roolback/commit with entity

Regular Member 帖子: 127 加入日期: 09-7-14 最近的帖子
I am experiencing the same problem. I am running LR 6.0.5 CE. I also encountered the same problem in delete operations.

It will be easy to upgrade LR 6.0.5 to 6.1?

Thanks.
thumbnail
David García González,修改在12 年前。

RE: Transaction roolback/commit with entity

Regular Member 帖子: 127 加入日期: 09-7-14 最近的帖子
I am trying to obtain the services of another entity that is located in a different plugin in a different war.

When I try to build the service.xml it is throwing me the following exception:

<reference entity="Evento" package-path="com.aaa.bbb.portlet.eventos"></reference>


     [java] java.io.IOException: Unable to open resource in class loader com/aaa/bbb/portlet/eventos/service.xml
     [java] 	at com.liferay.portal.kernel.util.StringUtil.read(StringUtil.java:604)
     [java] 	at com.liferay.portal.kernel.util.StringUtil.read(StringUtil.java:567)
     [java] 	at com.liferay.portal.tools.servicebuilder.ServiceBuilder.getEntity(ServiceBuilder.java:1248)
     [java] 	at com.liferay.portal.tools.servicebuilder.ServiceBuilder.<init>(ServiceBuilder.java:960)
     [java] 	at com.liferay.portal.tools.servicebuilder.ServiceBuilder.<init>(ServiceBuilder.java:404)
     [java] 	at com.liferay.portal.tools.servicebuilder.ServiceBuilder.main(ServiceBuilder.java:176)</init></init>


The entity tag is also supported for entities that are located inside the same war?
AA CC,修改在12 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 38 加入日期: 11-5-31 最近的帖子
Nobody help me?
thumbnail
Thiago Leão Moreira,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1449 加入日期: 07-10-10 最近的帖子
You won't be able to reference an Entity from another WAR project, this is not supported by Liferay. To workaround it you must escalate the service-plugin.jar file to the global classloader and then reference it from your WAR client application.
thumbnail
David García González,修改在12 年前。

RE: Transaction roolback/commit with entity

Regular Member 帖子: 127 加入日期: 09-7-14 最近的帖子
Thiago Leão Moreira:
You won't be able to reference an Entity from another WAR project, this is not supported by Liferay. To workaround it you must escalate the service-plugin.jar file to the global classloader and then reference it from your WAR client application.


Thanks Thiago, The jars belonging to other services of other entities (*-service.jar) are in the global classloader, I don't find the service-plugin.jar.
thumbnail
Thiago Leão Moreira,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1449 加入日期: 07-10-10 最近的帖子
Hey David,

I'm sorry, I meant *-portlet-service.jar you did right.
thumbnail
Mika Koivisto,修改在12 年前。

RE: Transaction roolback/commit with entity

Liferay Legend 帖子: 1519 加入日期: 06-8-7 最近的帖子
The transaction gets automatically rolledback for PortalException and SystemException.

If you want to inject more services to your LocalServiceImpl you'll add something like this:
@BeanReferece(type=com.aaa.bbb.portlet.eventos.service.EventosLocalService.class)
private static EventosLocalService eventosLocalService;
Michal R,修改在9 年前。

RE: Transaction roolback/commit with entity

Junior Member 帖子: 25 加入日期: 12-5-28 最近的帖子
Mika Koivisto:
The transaction gets automatically rolledback for PortalException and SystemException.


Transaction seems to be rolled back also for any RuntimeException. But not for checked exceptions other than PortalException and SystemException (tried it with FileNotFoundException). Liferay 6.1.1.