掲示板

Transaction roolback/commit with entity

12年前 に AA CC によって更新されました。

Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/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
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/31 最新の投稿
Help me please
thumbnail
12年前 に Thiago Leão Moreira によって更新されました。

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.
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/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.
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/31 最新の投稿
Please help me!!!
thumbnail
12年前 に Amos Fong によって更新されました。

RE: Transaction roolback/commit with entity

Liferay Legend 投稿: 2047 参加年月日: 08/10/07 最新の投稿
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
12年前 に David García González によって更新されました。

RE: Transaction roolback/commit with entity

Regular Member 投稿: 127 参加年月日: 09/07/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
12年前 に Thiago Leão Moreira によって更新されました。

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>.
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/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
12年前 に Thiago Leão Moreira によって更新されました。

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...
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/31 最新の投稿
i'm not usin LocalServiceUtil but locaService, because i get the service from LocalServiceUtil like this ArgContentPurchaseLocalServiceUtil.getService().
It's right ?
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/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
12年前 に Mika Koivisto によって更新されました。

RE: Transaction roolback/commit with entity

Liferay Legend 投稿: 1519 参加年月日: 06/08/07 最新の投稿
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.
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/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
12年前 に Mika Koivisto によって更新されました。

RE: Transaction roolback/commit with entity

Liferay Legend 投稿: 1519 参加年月日: 06/08/07 最新の投稿
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.
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/31 最新の投稿
The versione is 6.0.6
thumbnail
12年前 に Mika Koivisto によって更新されました。

RE: Transaction roolback/commit with entity

Liferay Legend 投稿: 1519 参加年月日: 06/08/07 最新の投稿
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
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/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
12年前 に Mika Koivisto によって更新されました。

RE: Transaction roolback/commit with entity

Liferay Legend 投稿: 1519 参加年月日: 06/08/07 最新の投稿
August is still the target date but I wouldn't expect it early August but rather end of August.
thumbnail
12年前 に David García González によって更新されました。

RE: Transaction roolback/commit with entity

Regular Member 投稿: 127 参加年月日: 09/07/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
12年前 に David García González によって更新されました。

RE: Transaction roolback/commit with entity

Regular Member 投稿: 127 参加年月日: 09/07/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?
12年前 に AA CC によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 38 参加年月日: 11/05/31 最新の投稿
Nobody help me?
thumbnail
12年前 に Thiago Leão Moreira によって更新されました。

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
12年前 に David García González によって更新されました。

RE: Transaction roolback/commit with entity

Regular Member 投稿: 127 参加年月日: 09/07/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
12年前 に Thiago Leão Moreira によって更新されました。

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
12年前 に Mika Koivisto によって更新されました。

RE: Transaction roolback/commit with entity

Liferay Legend 投稿: 1519 参加年月日: 06/08/07 最新の投稿
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;
9年前 に Michal R によって更新されました。

RE: Transaction roolback/commit with entity

Junior Member 投稿: 25 参加年月日: 12/05/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.