Home » Eclipse Projects » EclipseLink » Unexpected entity version number
Unexpected entity version number [message #507187] |
Tue, 12 January 2010 07:26 |
Marcin Kwapisz Messages: 14 Registered: January 2010 |
Junior Member |
|
|
Hi,
I wrote several test for JPA (eclipselink 2.0) scenarios like concurrent update with optimistic locking.
I use DerbyDB and DBUnit to initialize a database before each test.
@Before
public void setUp() throws MalformedURLException, DataSetException, DatabaseUnitException, SQLException {
FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
builder.setColumnSensing(true);
dataset = builder.build(new File(initDataSourceFile));
//Clean and initialize database
DatabaseOperation.CLEAN_INSERT.execute(connection, dataset);
EntityManagerFactory emFactory = Persistence.createEntityManagerFactory(testPUName, properties);
testEm = emFactory.createEntityManager();
logger.log(Level.INFO, "+++ Test begin");
}
There are two test methods that use two entity managers (testEm created in setUp and additional one created in the test method) to simulate concurrent update operations:
@Test
public void concurrentUpdate() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(testPUName, properties);
EntityManager concurrentTestEm = emf.createEntityManager();
Query firstQuery = testEm.createNamedQuery("Student.getByID");
firstQuery.setParameter("id", 2);
Query secondQuery = concurrentTestEm.createNamedQuery("Student.getByID");
secondQuery.setParameter("id", 2);
Student student, student2;
//Start two transaction, each on different entity manager
try {
testEm.getTransaction().begin();
concurrentTestEm.getTransaction().begin();
student = (Student) firstQuery.getSingleResult();
student.setImie("Wiesław");
student2 = (Student) secondQuery.getSingleResult();
student2.setImie("Kacper");
testEm.getTransaction().commit();
//the following operation should fail
concurrentTestEm.getTransaction().commit();
//Error, concurrent update was possible
fail("Concurrent update was possible");
} catch (javax.persistence.RollbackException ex) {
//Oczekujemy javax.persistence.OptimisticLockException opakowany w {@link javax.persistence.RollbackException}
Throwable cause = ex.getCause();
if ((cause != null) && (cause instanceof javax.persistence.OptimisticLockException)) {
logger.log(Level.INFO, "**** OK - Expected optimistic lock exception occured");
} else {
logger.log(Level.WARNING, "Błąd", ex);
fail("Unexpected exception, should be javax.persistence.OptimisticLockException");
}
} finally {
concurrentTestEm.close();
}
//Dump DB
dumpDb=true;
}
@Test
public void concurrentDeleteAndUpdate() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(testPUName, properties);
EntityManager concurrentTestEm = emf.createEntityManager();
Query firstQuery = testEm.createNamedQuery("Student.getByID");
firstQuery.setParameter("id", 2);
Query secondQuery = concurrentTestEm.createNamedQuery("Student.getByID");
secondQuery.setParameter("id", 3);
Student student, student2;
//Start two transaction, each on different entity manager
try {
testEm.getTransaction().begin();
concurrentTestEm.getTransaction().begin();
student = (Student) firstQuery.getSingleResult();
student2 = (Student) secondQuery.getSingleResult();
testEm.remove(student);
//Concurrent update exception is thrown during commit. It was not expected here.
//Student entity version is 2, but should be 1
//When concurrentUpdate() test is commented, this will fail
//Running test in debug mode and setting breakpoint in concurrenUpdate,
//will cause this test to fail
testEm.getTransaction().commit();
student2.setImie("Kacper");
//the following operation should fail
concurrentTestEm.getTransaction().commit();
//Error, concurrent update was possible
fail("Concurrent update was possible");
} catch (javax.persistence.RollbackException ex) {
Throwable cause = ex.getCause();
if ((cause != null) && (cause instanceof javax.persistence.OptimisticLockException)) {
logger.log(Level.INFO, "**** OK - Expected optimistic lock exception occured");
} else {
logger.log(Level.WARNING, "Błąd", ex);
fail("Unexpected exception, should be javax.persistence.OptimisticLockException");
}
} finally {
concurrentTestEm.close();
}
//Dump DB
dumpDb=true;
}
And the problem is with the concurrentDeleteAndUpdate test method. This test method succeedes but should fail. If you look carefully you will find that there is no concurrent update. I changed secondQuery id parameter to 3 intentionally to show this unexpected for me behaviour. firstQuery.getSingleResult returns student entity with version set to 2, but should be 1.
- When I run test suite in debug mode and set breakpoint in concurrentUpdate and then continue the test, the concurrentDeleteAndUpdate fails (student version is 1)
- If I comment the first test method (concurrentUpdate) concurrentDeleteAndUpdate fails (as expected).
What is wrong with my test methods? You can download maven project with these tests from:
http://zskl.zsk.p.lodz.pl/~mkwapisz/jpatest/entitytest.zip
Thanks in advance for your help
Regards
Marcin Kwapisz
|
|
| | |
Re: Unexpected entity version number [message #507593 is a reply to message #507501] |
Thu, 14 January 2010 00:14 |
Marcin Kwapisz Messages: 14 Registered: January 2010 |
Junior Member |
|
|
Hello,
You can download the test project using the link from my first post. This is maven project so there should be no problems with running the test suite. You can check configuration and dependency versions in pom.xml and of course try to reproduce the problem. Uncomment the first line of JPATest.setUpClass to run the test using in-memory database.
There is another very interesting thing. I have run the test suite in debug mode. When a breakpoint is set within the try block of the first test method (concurrentUpdate) and then the test is continued, the second test method (concurrentUpdateAndDelete) works as expected (query reads Student entity with version 1).
2010-01-14 00:43:51 pl.lodz.p.ck.egzaminy.entities.JPATest setUp
INFO: +++ Początek testu
[EL Fine]: 2010-01-14 00:43:51.609--ServerSession(2456372)--Connection(3075861)--T hread(Thread[main,5,main])--SELECT TS_ID, WD_NAZWA, CZ_IMIE, ADR_EMAIL, WERSJA, CZ_NAZWISKO, AL_ALBUM FROM TESTDB.STUDENT WHERE (TS_ID = ?)
bind => [2]
[EL Fine]: 2010-01-14 00:43:51.609--ServerSession(2456372)--Connection(3075861)--T hread(Thread[main,5,main])--SELECT TS_ID, WD_NAZWA, CZ_IMIE, ADR_EMAIL, WERSJA, CZ_NAZWISKO, AL_ALBUM FROM TESTDB.STUDENT WHERE (TS_ID = ?)
bind => [2]
[EL Fine]: 2010-01-14 00:43:51.609--ClientSession(10808772)--Connection(3075861)-- Thread(Thread[main,5,main])--UPDATE TESTDB.STUDENT SET CZ_IMIE = ?, WERSJA = ? WHERE ((TS_ID = ?) AND (WERSJA = ?))
bind => [Wiesław, 2, 2, 1]
[EL Fine]: 2010-01-14 00:43:51.609--ClientSession(23398687)--Connection(3075861)-- Thread(Thread[main,5,main])--UPDATE TESTDB.STUDENT SET CZ_IMIE = ?, WERSJA = ? WHERE ((TS_ID = ?) AND (WERSJA = ?))
bind => [Kacper, 2, 2, 1]
[EL Warning]: 2010-01-14 00:43:51.609--UnitOfWork(9089012)--Thread(Thread[main,5,main ])--Exception [EclipseLink-5006] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.OptimisticLockException
Exception Description: The object [pl.lodz.p.ck.egzaminy.entities.Student[id=2, ver=2]] cannot be updated because it has changed or been deleted since it was last read.
Class> pl.lodz.p.ck.egzaminy.entities.Student Primary Key> [2]
[EL Warning]: 2010-01-14 00:43:51.609--UnitOfWork(9089012)--Thread(Thread[main,5,main ])--javax.persistence.OptimisticLockException: Exception [EclipseLink-5006] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.OptimisticLockException
Exception Description: The object [pl.lodz.p.ck.egzaminy.entities.Student[id=2, ver=2]] cannot be updated because it has changed or been deleted since it was last read.
Class> pl.lodz.p.ck.egzaminy.entities.Student Primary Key> [2]
2010-01-14 00:43:51 pl.lodz.p.ck.egzaminy.entities.StudentTest concurrentUpdate
INFO: **** OK - Expected optimistic lock exception occured
2010-01-14 00:43:51 pl.lodz.p.ck.egzaminy.entities.JPATest tearDown
INFO: +++ Koniec testu
2010-01-14 00:43:51 pl.lodz.p.ck.egzaminy.entities.JPATest setUp
INFO: +++ Początek testu
[EL Fine]: 2010-01-14 00:43:51.625--ServerSession(2456372)--Connection(3075861)--T hread(Thread[main,5,main])--SELECT TS_ID, WD_NAZWA, CZ_IMIE, ADR_EMAIL, WERSJA, CZ_NAZWISKO, AL_ALBUM FROM TESTDB.STUDENT WHERE (TS_ID = ?)
bind => [2]
2010-01-14 00:43:51 pl.lodz.p.ck.egzaminy.entities.StudentTest concurrentUpdateAndDelete
INFO: pl.lodz.p.ck.egzaminy.entities.Student[id=2, ver=2]
[EL Fine]: 2010-01-14 00:43:51.625--ServerSession(2456372)--Connection(3075861)--T hread(Thread[main,5,main])--SELECT TS_ID, WD_NAZWA, CZ_IMIE, ADR_EMAIL, WERSJA, CZ_NAZWISKO, AL_ALBUM FROM TESTDB.STUDENT WHERE (TS_ID = ?)
bind => [3]
2010-01-14 00:43:51 pl.lodz.p.ck.egzaminy.entities.StudentTest concurrentUpdateAndDelete
INFO: pl.lodz.p.ck.egzaminy.entities.Student[id=3, ver=1]
[EL Fine]: 2010-01-14 00:43:51.625--ClientSession(12090673)--Connection(3075861)-- Thread(Thread[main,5,main])--DELETE FROM TESTDB.STUDENT WHERE ((TS_ID = ?) AND (WERSJA = ?))
bind => [2, 2]
[EL Warning]: 2010-01-14 00:43:51.625--UnitOfWork(3023915)--Thread(Thread[main,5,main ])--Exception [EclipseLink-5003] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.OptimisticLockException
Exception Description: The object [pl.lodz.p.ck.egzaminy.entities.Student[id=2, ver=2]] cannot be deleted because it has changed or been deleted since it was last read.
Class> pl.lodz.p.ck.egzaminy.entities.Student Primary Key> [2]
[EL Warning]: 2010-01-14 00:43:51.625--UnitOfWork(3023915)--Thread(Thread[main,5,main ])--javax.persistence.OptimisticLockException: Exception [EclipseLink-5003] (Eclipse Persistence Services - 2.0.0.v20091127-r5931): org.eclipse.persistence.exceptions.OptimisticLockException
Exception Description: The object [pl.lodz.p.ck.egzaminy.entities.Student[id=2, ver=2]] cannot be deleted because it has changed or been deleted since it was last read.
Class> pl.lodz.p.ck.egzaminy.entities.Student Primary Key> [2]
2010-01-14 00:43:51 pl.lodz.p.ck.egzaminy.entities.StudentTest concurrentUpdateAndDelete
INFO: **** OK - Expected optimistic lock exception occured
2010-01-14 00:43:51 pl.lodz.p.ck.egzaminy.entities.JPATest tearDown
|
|
| | | | |
Goto Forum:
Current Time: Thu Apr 25 22:28:25 GMT 2024
Powered by FUDForum. Page generated in 0.02534 seconds
|