DataNucleus JIRA is now in read-only mode. Raise any new issues in GitHub against the plugin that it applies to. DataNucleus JIRA will remain for the foreseeable future but will eventually be discontinued
Issue Details (XML | Word | Printable)

Key: NUCMONGODB-125
Type: Bug Bug
Status: Closed Closed
Resolution: Cannot Reproduce
Priority: Major Major
Assignee: Unassigned
Reporter: Bjorn Roche
Votes: 0
Watchers: 0
Operations

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

Update and delete operations of entities with @Version annotation require refresh

Created: 03/Jul/13 07:15 PM   Updated: 22/Oct/13 11:44 AM   Resolved: 14/Oct/13 10:19 AM
Component/s: None
Affects Version/s: 3.1.1
Fix Version/s: None

File Attachments: 1. Zip Archive dn-test.zip (10 kB)

Environment: J2 SE, JPA

Forum Thread URL: http://stackoverflow.com/questions/17432410/optimistic-locking-in-datanucleus-mongodb
Datastore: MongoDB
Severity: Production


 Description  « Hide
When working with entities that have @Version annotations, I have encountered trouble updating and deleting. Specifically, unless I refresh the object, updates and deletes fail.

I will attach unit tests.

Bjorn Roche added a comment - 03/Jul/13 07:19 PM
Attached is the complete test project. Below is the test case.


/**
 *
 */
package com.xonami.dnTest;

import static org.junit.Assert.*;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.LockModeType;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.xonami.dnTest.entities.A;

/**
 * @author bjorn
 *
 */
public class CrudTest {
private EntityManager em;

private void clean() {
em.getTransaction().begin();
em.createQuery("DELETE FROM A a").executeUpdate();
em.getTransaction().commit();
}

/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
em = EMFactory.createMainEntityManager();
clean();
}

/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
// clean(); //final clean is commented out so we can see the results if we want to
em.close();
}

@Test
public void removeViaQueryTest() {
// create
A a = new A("some text");
em.persist(a);
em.refresh(a);
String key = a.getKey();
assertNotNull(key);

em.clear();

a = em.find(A.class, key);
assertNotNull(a);

em.clear();

//em.getTransaction().begin();
em.createQuery("DELETE FROM A a").executeUpdate(); //this works only if you put it in a transaction.
//em.getTransaction().commit();

em.clear();

a = em.find(A.class, key);
assertNull(a);
}

@Test
public void deletionTest1() {
EntityTransaction txn = em.getTransaction();
// create
A a = new A("some text");
txn.begin();
em.persist(a);
em.refresh(a);
txn.commit();
String key = a.getKey();
assertNotNull(key);
// delete
em.clear();
txn.begin();
a = em.find(A.class, key);
//em.refresh(a, LockModeType.OPTIMISTIC); //<< -- this solves the problem
em.remove(a);
txn.commit(); //optimistic verification problems! a = em.find(A.class, key, LockModeType.OPTIMISTIC); makes no difference
em.clear();
txn.begin();
a = em.find(A.class, key);
assertNull(a);
txn.commit();
}

@Test
public void basicCreate() { //this test succeeds
EntityTransaction txn = em.getTransaction();
// create
A a = new A("some text");
txn.begin();
em.persist(a);
em.refresh(a);
txn.commit();
String key = a.getKey();
assertNotNull(key);
// retrieve
em.clear();
txn.begin();
a = em.find(A.class, key);
assertNotNull(a);
assertEquals(a.getText(), "some text");
txn.commit();
}

@Test
public void basicUpdate() {
EntityTransaction txn = em.getTransaction();
// create
A a = new A("some text");
txn.begin();
em.persist(a);
em.refresh(a);
txn.commit();
String key = a.getKey();
assertNotNull(key);
// update:
em.clear();
txn.begin();
a = em.find(A.class, key);
//em.refresh(a, LockModeType.OPTIMISTIC); <<-- this fixes issue
a.setText("new text");
em.persist(a);
txn.commit(); //fails here due to optimistic verification problems
// // check update
em.clear();
txn.begin();
em = EMFactory.createMainEntityManager();
a = em.find(A.class, key);
assertNotNull(a);
assertEquals(a.getText(), "new text");
txn.commit();
}
@Test
public void lockedUpdate() {
EntityTransaction txn = em.getTransaction();
// create
A a = new A("some text");
txn.begin();
em.persist(a);
em.refresh(a);
txn.commit();
String key = a.getKey();
assertNotNull(key);
// update:
em.clear();
txn.begin();
a = em.find(A.class, key);
em.lock(a, LockModeType.OPTIMISTIC); // <<-- this fails telling me A is not versioned
a.setText("new text");
em.persist(a);
txn.commit();
// // check update
em.clear();
txn.begin();
em = EMFactory.createMainEntityManager();
a = em.find(A.class, key);
assertNotNull(a);
assertEquals(a.getText(), "new text");
txn.commit();
}
}

Bjorn Roche made changes - 03/Jul/13 07:19 PM
Field Original Value New Value
Attachment dn-test.zip [ 11980 ]
Andy Jefferson added a comment - 14/Oct/13 10:19 AM
Tried with current SVN and can't see problem

Andy Jefferson made changes - 14/Oct/13 10:19 AM
Status Open [ 1 ] Resolved [ 5 ]
Resolution Cannot Reproduce [ 5 ]
Andy Jefferson made changes - 22/Oct/13 11:44 AM
Status Resolved [ 5 ] Closed [ 6 ]