Issue Details (XML | Word | Printable)

Key: NUCCORE-667
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: Fiaz Hossain
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
DataNucleus Core

Calling detach on a persistent object leads to subsequent NullPointerException on commit

Created: 03/Mar/11 04:23 AM   Updated: 03/Oct/11 07:37 AM   Resolved: 03/Aug/11 03:10 PM
Component/s: Lifecycles
Affects Version/s: 2.2.3
Fix Version/s: 3.0.1

File Attachments: 1. Zip Archive detach_npe.zip (2 kB)

Environment: Ubuntu Linux

Severity: Development


 Description  « Hide
The following code hits an NPE
            A a = new A();
            em.persist(a);
            em.detach(a);
            
            /**
             * ERROR is here - calling commit() causes the detached entity to be flushed causing NPE.
             */
            tx.commit();

Mar 2, 2011 7:08:06 PM org.datanucleus.test.Main main
INFO: >> Exception thrown retrieving objects
java.lang.NullPointerException
at org.datanucleus.jdo.state.JDOStateManagerImpl.flush(JDOStateManagerImpl.java:4772)
at org.datanucleus.ObjectManagerImpl.flushInternal(ObjectManagerImpl.java:3179)
at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:3119)
at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:3260)
at org.datanucleus.ObjectManagerImpl$2.transactionPreCommit(ObjectManagerImpl.java:324)
at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:394)
at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:279)
at org.datanucleus.jpa.EntityTransactionImpl.commit(EntityTransactionImpl.java:106)
at org.datanucleus.test.Main.main(Main.java:27)

I am not sure why someone would detach a persisted entity before calling commit() but it seems like nothing prevents someone from doing so as well. I am attaching a test case to reproduce the error.

I added the following change to ObjectManagerImpl.java that seems to fix it. Not sure clearing dirty objects on detach is the functionally correct.

+++ src/java/org/datanucleus/ObjectManagerImpl.java 2011-03-02 18:35:45.761863043 -0800
@@ -1981,6 +1981,7 @@
             throw new NucleusUserException(LOCALISER.msg("010007", getApiAdapter().getIdForObject(obj)));
         }
         sm.detach(state);
+ clearDirty(sm);
     }
 
     /**

Fiaz Hossain added a comment - 03/Mar/11 04:24 AM
Running the test will produce NPE

Fiaz Hossain made changes - 03/Mar/11 04:24 AM
Field Original Value New Value
Attachment detach_npe.zip [ 11365 ]
Andy Jefferson added a comment - 03/Aug/11 03:10 PM
SVN trunk runs fine on that case and logs a message about performing stupid operations; a detached object is as much use as a chocolate teapot without a persistent equivalent

Andy Jefferson made changes - 03/Aug/11 03:10 PM
Status Open [ 1 ] Resolved [ 5 ]
Fix Version/s 3.0.1 [ 11300 ]
Resolution Fixed [ 1 ]
Andy Jefferson made changes - 03/Oct/11 07:37 AM
Status Resolved [ 5 ] Closed [ 6 ]