Hi Robert,

 

thank you for the hint: using one data context per request solved the problem!

 

Up to now, I didn’t encountered any more this issue.

 

Thanks to all for the help!

Best

cghersi

 

From: Robert Zeigler-6 [via Cayenne] 
[mailto:[email protected]] 
Sent: lunedì 12 marzo 2012 12:18
To: cghersi
Subject: Re: Temporary ID hasn't been replaced on commit

 

Are you using one datacontext for the entire application? 

Generally, you want one datacontext per user, or even one datacontext per 
request, depending on your use-case. 

The exception is a "read-only" data context for data that doesn't typically 
change (lookup tables, for instance). 

Robert 

On Mar 12, 2012, at 3/126:10 AM , cghersi wrote: 


> Update: I think I found the real source of the problem. 
> 
> When I commit the just created object, I use the method 
> context.commitChanges() that saves ALL my persistent objects. If in another 
> thread I’m creating another persistent object in the meanwhile, such object 
> is saved with the partial information just added. 
> 
> e.g. 
> 
> in the first thread: 
> 
> MyPerstObject1 newObj = context.get().newObject(MyPerstObject1.class); 
> 
> newObj.setField1(“valueForField1”); 
> 
> newObj.setField2(“valueForField2”); 
> 
> newObj.setField3(“valueForField3”); 
> 
> newObj.setField4(“valueForField4”); 
> 
> context.commitChanges(); 
> 
> 
> 
> in another thread: 
> 
> MyPerstObject2 newObj = context.get().newObject(MyPerstObject2.class); 
> //there’s a “Id” PK field AUTO_INCREMENT (MySQL) 
> 
> newObj.setField1(“valueForField1”); 
> 
> newObj.setField2(“valueForField2”); 
> 
> newObj.setField3(“valueForField3”); 
> 
> newObj.setField4(“valueForField4”); 
> 
> newObj.setField5(“valueForField5”); 
> 
> newObj.setField6(“valueForField6”); 
> 
> context.commitChanges(); 
> 
> 
> 
> Now, if the commitChanges() in the first thread is invoked e.g. when I 
> performing newObj.setField3(“valueForField3”); in the another thread, the 
> newObj of type MyPerstObject2 should be saved without fields 4,5,6 filled, 
> resulting in ValidationException in the first thread (given that field 4, 5 
> and/or 6 are mandatory). 
> 
> I also tried to make the fields 4,5,6 optional, but in this case another 
> exception occurs: 
> 
> org.apache.cayenne.access.OptimisticLockException: [v.3.0 Apr 26 2010 
> 09:59:17] Optimistic Lock Failure, SQL: [UPDATE MyPerstObject2 SET Field1 = 
> ?, Field2 = ?, Field3 = ?, Field4 = ?, Field5 = ?, Field6 = ? WHERE Id IS 
> NULL], WHERE clause bindings: [Id=NULL] 
> 
> at 
> org.apache.cayenne.access.jdbc.BatchAction.runAsIndividualQueries(BatchAction.java:232)
>  
> 
> at 
> org.apache.cayenne.access.jdbc.BatchAction.performAction(BatchAction.java:91) 
> 
> at 
> org.apache.cayenne.access.DataNodeQueryAction.runQuery(DataNodeQueryAction.java:87)
>  
> 
> at org.apache.cayenne.access.DataNode.performQueries(DataNode.java:269) 
> 
> at 
> org.apache.cayenne.access.DataDomainFlushAction.runQueries(DataDomainFlushAction.java:226)
>  
> 
> at 
> org.apache.cayenne.access.DataDomainFlushAction.flush(DataDomainFlushAction.java:144)
>  
> 
> at org.apache.cayenne.access.DataDomain.onSyncFlush(DataDomain.java:824) 
> 
> at org.apache.cayenne.access.DataDomain$2.transform(DataDomain.java:791) 
> 
> at org.apache.cayenne.access.DataDomain.runInTransaction(DataDomain.java:850) 
> 
> at org.apache.cayenne.access.DataDomain.onSync(DataDomain.java:788) 
> 
> at org.apache.cayenne.access.DataContext.flushToParent(DataContext.java:1106) 
> 
> at org.apache.cayenne.access.DataContext.commitChanges(DataContext.java:1045) 
> 
> 
> 
> 
> 
> This issue is really driving me crazy!! 
> 
> Please, let me know what I’m doing wrong… 
> 
> 
> 
> Thank you very much 
> 
> Best 
> 
> cghersi 
> 
> 
> 
> 
> 
> From: Cristiano Ghersi [mailto:[hidden email]] 
> Sent: lunedì 12 marzo 2012 10:01 
> To: 'Andrus Adamchik [via Cayenne]' 
> Subject: RE: Temporary ID hasn't been replaced on commit 
> 
> 
> 
> OK, but this is exactly the method that causes the exception L 
> 
> 
> 
> Now I’m trying to resolve the problem at the very top, adding a 
> synchronization block to all my context.commitChanges(): this solution, 
> anyway, seems to be too drastic for my application: is there a way to commit 
> changes related only to a single table? 
> 
> 
> 
> Thank you very much. 
> 
> Best 
> 
> cghersi 
> 
> 
> 
> From: Andrus Adamchik [via Cayenne] [mailto:[hidden email]] 
> Sent: venerdì 9 marzo 2012 17:41 
> To: cghersi 
> Subject: Re: Temporary ID hasn't been replaced on commit 
> 
> 
> 
> Sorry this is Cayenne 3.1 API, and there is a typo :) 
> 
> 3.1: Cayenne.intPkForObject 
> 3.0: DataObjectUtils.intPkForObject 
> 
> On Mar 9, 2012, at 11:33 AM, cghersi wrote: 
> 
> 
>> Hi Andrus, 
>> 
>> I cannot find documentation for your proposal (Cayenne.inPkForObject): may 
>> you give me an example of use? 
>> 
>> 
>> 
>> Thank you very much. 
>> 
>> Best 
>> 
>> cghersi 
>> 
>> 
>> 
>> From: Andrus Adamchik [via Cayenne] [mailto:[hidden email]] 
>> Sent: venerdì 9 marzo 2012 15:40 
>> To: cghersi 
>> Subject: Re: Temporary ID hasn't been replaced on commit 
>> 
>> 
>> 
>>> Is there any effective way to directly retrieve the ID of the just added 
>>> object after the context.commitChanges() invocation? 
>> 
>> After commit the ID should be available via normal Cayenne APIs (e.g. 
>> Cayenne.inPkForObject). So there's something else at play too. 
>> 
>> Andrus 
>> 
>> On Mar 9, 2012, at 9:10 AM, cghersi wrote: 
>> 
>> 
>>> Hi all, perhaps I’ve understood something about the issue: 
>>> 
>>> Seems that the problem arises when I add a new object to the DB: just after 
>>> the insert, I need the autogenerated PK, and this seems to put in troubles 
>>> the method DataObjectUtils.intPKForObject(Persistent); 
>>> 
>>> Which sometimes throws the Exception in the subject. 
>>> 
>>> 
>>> 
>>> Is there any effective way to directly retrieve the ID of the just added 
>>> object after the context.commitChanges() invocation? 
>>> 
>>> 
>>> 
>>> Thank you very much for your help. 
>>> 
>>> 
>>> 
>>> Best 
>>> 
>>> cghersi 
>>> 
>>> 
>>> 
>>> From: cghersi [via Cayenne] [mailto:[hidden email]] 
>>> Sent: mercoledì 7 marzo 2012 09:43 
>>> To: cghersi 
>>> Subject: Re: Temporary ID hasn't been replaced on commit 
>>> 
>>> 
>>> 
>>> Hi Andrus, 
>>> 
>>> here there's the code that I use to save my object: 
>>> 
>>>      private AtomicReference<DataContext> context = new 
>>> AtomicReference<DataContext>(config.getDomain("PacketDomain").createDataCont
>>>  
>>> ext()); 
>>> 
>>>      public Network GetNetworkByPK(Integer pkID) { 
>>>              ObjectId id = new ObjectId(Network.class.getSimpleName(), 
>>> Network.ID_PK_COLUMN, pkID); 
>>> 
>>>              // this constructor implicitly uses "CACHE_REFRESH" policy, 
>>> so a fresh object will be returned 
>>>              ObjectIdQuery query = new ObjectIdQuery(id); 
>>>              return 
>>> (Network)DataObjectUtils.objectForQuery(context.get(), query); 
>>>      } 
>>> 
>>>      public PhysicalNode GetPhysicalNodeByMac(byte[] macAddr){ 
>>>              Expression qualifier = 
>>> ExpressionFactory.matchExp(PhysicalNode.MAC_ADDRESS_PROPERTY, macAddr); 
>>>              List<PhysicalNode> list = query(qualifier, 
>>> PhysicalNode.class); 
>>> 
>>>              //there can be only a single result from this query: 
>>>              return list.get(0); 
>>>      } 
>>> 
>>>      public synchronized PhysicalNode AddPhysicalNode(byte[] macAddr, 
>>> byte[] nsap, int netId, short type, 
>>>                      boolean isOnline, String defaultLabel){ 
>>> 
>>>              PhysicalNode pn=null; 
>>>              Network n = GetNetworkByPK(netId); 
>>>              if(n!=null){ 
>>>                      pn = GetPhysicalNodeByMac(macAddr); 
>>>                      if(pn==null){ 
>>>                              pn = createNewObj(PhysicalNode.class); 
>>>                              pn.setMacAddress(macAddr); 
>>>                              pn.setLabel(defaultLabel); 
>>>                              pn.setCoordX((double)-1); 
>>>                              pn.setCoordY((double)-1); 
>>>                              pn.setCoordZ((double)-1); 
>>>                              pn.setCreation((new Date()).getTime()); 
>>>                      } 
>>>                      pn.setNetwork(n); 
>>>                      pn.setNsap(nsap); 
>>>                      pn.setType(type); 
>>>                      pn.setIsOnline(isOnline); 
>>>                      CommitObjects(PhysicalNode.class); 
>>>              } 
>>>              else{ 
>>>                      throw new ValidationException("Network not in DB"); 
>>>              } 
>>>              return pn; 
>>>      } 
>>> 
>>> The problem is not systematic, but may occur with randomness if I force the 
>>> DB to add a great number of PhysicalNode objects in a little time window. 
>>> 
>>> Please, let me know if you have any idea! 
>>> 
>>> Thank you very much 
>>> Best 
>>> cghersi 
>>> 
>>> _____   
>> 
>> 
>> 
>> 
>> 
>> _____   
>> 
>> If you reply to this email, your message will be added to the discussion 
>> below: 
>> 
>> http://cayenne.195.n3.nabble.com/Temporary-ID-hasn-t-been-replaced-on-commit-tp3801043p3812833.html
>>   
>> 
>> To unsubscribe from Temporary ID hasn't been replaced on commit, click here 
>> < . 
>> <http://cayenne.195.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer
>>  
>> <http://cayenne.195.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer
>>  
>> <http://cayenne.195.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>  
>> &id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>  
>> &id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>  NAML 
>> 
>> 
>> 
>> -- 
>> View this message in context: 
>> http://cayenne.195.n3.nabble.com/Temporary-ID-hasn-t-been-replaced-on-commit-tp3801043p3813158.html
>> Sent from the Cayenne - User mailing list archive at Nabble.com. 
> 
> 
> 
>  _____   
> 
> If you reply to this email, your message will be added to the discussion 
> below: 
> 
> http://cayenne.195.n3.nabble.com/Temporary-ID-hasn-t-been-replaced-on-commit-tp3801043p3813176.html
>   
> 
> To unsubscribe from Temporary ID hasn't been replaced on commit, click here < 
> . 
> <http://cayenne.195.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer 
> <http://cayenne.195.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>  
> &id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>  NAML 
> 
> 
> 
> -- 
> View this message in context: 
> http://cayenne.195.n3.nabble.com/Temporary-ID-hasn-t-been-replaced-on-commit-tp3801043p3818939.html
> Sent from the Cayenne - User mailing list archive at Nabble.com. 





  _____  

If you reply to this email, your message will be added to the discussion below:

http://cayenne.195.n3.nabble.com/Temporary-ID-hasn-t-been-replaced-on-commit-tp3801043p3818950.html
 

To unsubscribe from Temporary ID hasn't been replaced on commit, click here 
<http://cayenne.195.n3.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3801043&code=Y3Jpc3RpYW5vLmdoZXJzaUBhYm9kYXRhLmNvbXwzODAxMDQzfDUzMzUzMjY3>
 .
 
<http://cayenne.195.n3.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
 NAML 



--
View this message in context: 
http://cayenne.195.n3.nabble.com/Temporary-ID-hasn-t-been-replaced-on-commit-tp3801043p3831292.html
Sent from the Cayenne - User mailing list archive at Nabble.com.

Reply via email to