Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Weird problem of persist in another transaction
icon9.gif  Weird problem of persist in another transaction [message #989534] Thu, 06 December 2012 16:28 Go to next message
khalil haddad is currently offline khalil haddadFriend
Messages: 12
Registered: November 2012
Junior Member
I'm facing a weird problem. The same code does not do the same thing when I launch my application (Spring) or a JUNIT test.

Here is the exception :

Quote:
org.springframework.dao.InvalidDataAccessApiUsageException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: net.diamis.mph.common.entities.process.Workflow@6.


The "WorkFlow" object exists in database and I want to link it to a "Process" object.
I'm trying to persist the "Process" object in a parallel transaction :

	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
	public T immediateSave(T e) {
		em.persist(e);
		if(!AppliUtils.isTest())
			update_cache_timestamp();
		return (T) e;
	}


The "Process" entity is defined that way :


	@Entity
	@Table(name = "PROCESS")
	public class Process {
		
		@ManyToOne
		@JoinColumn(name = "WRKFLW_ID")
		private Workflow workflow = null;

		...
	}


and the ManyToOne relation is NOT bidirectional.

The code which throws the exception is the following :

process = new Process();
process.setWorkflow(workflowDAO.findById(workflowId));
process.setInstance(instance);
process.setStatus(ProcessStatus.pending.toString());
process.setStartTimestamp(startTime);
process = processDAO.immediateSave(process);


When I execute this in my Spring application, the Process object is created and linked to the Workflow object.
But when I execute the JUNIT test, I get the exception mentioned at the top of my post.
I don't want to set the relation between Process and WorkFlow to CascadeType.PERSIST since I don't want to persist the WorkFlow object.

Did I do something wrong?
Re: Weird problem of persist in another transaction [message #989616 is a reply to message #989534] Thu, 06 December 2012 23:32 Go to previous messageGo to next message
J LM is currently offline J LMFriend
Messages: 11
Registered: December 2012
Junior Member
Hi,

Could you check the EntityManagers objetcs that you are using in the
workflowDAO.findById
and the
processDAO.immediateSave

What's your complete JUnit test case ?
Do you use L2 cache ?
Re: Weird problem of persist in another transaction [message #989684 is a reply to message #989616] Fri, 07 December 2012 10:07 Go to previous messageGo to next message
khalil haddad is currently offline khalil haddadFriend
Messages: 12
Registered: November 2012
Junior Member
I'm testing a business component which is intercepted by a Spring AOP method, and the problem occurs in this method :

	@Around("execution(* net.diamis.mph.server.component.impl..*.process(..))")
	public Object processService(ProceedingJoinPoint pjp) throws Throwable {
		
		// --------------------------------------------------------------------------
		// GESTION DES TRACES SERVICE ==> PROCESS ==> INSTANCE: DEBUT
		// --------------------------------------------------------------------------
		AbstractEvent event = (AbstractEvent) pjp.getArgs()[0];

		Instance instance = StartRouter.getInstance();
		
		Process process;

		Date startTime = new Date();
		
		// Pour les tables d'Audit
		LogUtils.username.set("SERV-" + instance.getInstanceCode());

		// Recuperation du workflow auquel appartient le process
		Long workflowId = ((AbstractComponentImpl) pjp.getTarget()).getWorkflowId();

		// Création de l'objet process
		process = new Process();
		process.setWorkflow(workflowDAO.findById(workflowId));
		process.setInstance(instance);
		process.setStatus(ProcessStatus.pending.toString());
		process.setStartTimestamp(startTime);
		process = processDAO.immediateSave(process);

		...
	}




In my DAOs, entity managers are injected like this :
	@PersistenceContext(unitName="MPH")
	protected EntityManager em;


And I didn't set any cache. Should I?

[Updated on: Fri, 07 December 2012 10:07]

Report message to a moderator

Re: Weird problem of persist in another transaction [message #989690 is a reply to message #989684] Fri, 07 December 2012 10:27 Go to previous messageGo to next message
khalil haddad is currently offline khalil haddadFriend
Messages: 12
Registered: November 2012
Junior Member
I tried to disable the cache by adding this line in my persistence.xml :

<shared-cache-mode>NONE</shared-cache-mode>


But I still have the same error...
Re: Weird problem of persist in another transaction [message #990297 is a reply to message #989690] Tue, 11 December 2012 18:14 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Your problem is,

process.setWorkflow(workflowDAO.findById(workflowId));


The workflowDAO is using a different EntityManager, and thus persistence context than the one you are using to persist.
They need to use the same one, or you need to use merge().


James : Wiki : Book : Blog : Twitter
Re: Weird problem of persist in another transaction [message #991483 is a reply to message #989534] Wed, 19 December 2012 12:45 Go to previous message
khalil haddad is currently offline khalil haddadFriend
Messages: 12
Registered: November 2012
Junior Member
Ok I see. But why the problem only occurs in JUnit context?? Rolling Eyes
Previous Topic:EclipseLink zipped p2 repositories?
Next Topic:EclipseLink-7251: is mapped to a primary key column in the database. Updates are not allowed.
Goto Forum:
  


Current Time: Fri Apr 19 18:34:17 GMT 2024

Powered by FUDForum. Page generated in 0.03764 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top