-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: StaleObjectStateException ERROR - please help
PostPosted: Fri Jan 09, 2009 6:49 am 
Beginner
Beginner

Joined: Thu Aug 09, 2007 3:48 pm
Posts: 38
Hi,

Anyone can point me to the error please?

Note: this is a simplified test case extracted from my real app.
Thus the weird usage of 3 entity managers and
em1.getTransaction().begin();
em1.clear();
em1.close();
at the end of each section.
In real app it happens in different times.
HibernateUtil is basically copied from the tutorial.

Code:
            HibernateUtil.open();

            EntityManager em1 = HibernateUtil.reserveEntityManager();
            em1.getTransaction().begin();
            StringType st1 = new StringType();
            st1.setName("a");
            em1.persist(st1);
            em1.getTransaction().commit();
            em1.getTransaction().begin();
            em1.clear();
            em1.close();

            EntityManager em2 = HibernateUtil.reserveEntityManager();
            em2.getTransaction().begin();
            StringType st2 = new StringType();
            st2.setName("a");
            st2.setId(st1.getId());
            em2.merge(st2);
            em2.getTransaction().commit();
            em2.getTransaction().begin();
            em2.clear();
            em2.close();

            EntityManager em3 = HibernateUtil.reserveEntityManager();
            em3.getTransaction().begin();
            StringType st3 = new StringType();
            st3.setName("a");
            st3.setId(st1.getId());
            [b]em3.merge(st3);[/b]
            em3.getTransaction().commit();
            em3.getTransaction().begin();
            em3.clear();
            em3.close();


Code:
public static EntityManager reserveEntityManager()
    {
        return emf.createEntityManager();
    }
public static void open()
    {
        try
        {           
            emf = Persistence.createEntityManagerFactory("manager1");
        }
        catch (Throwable e)
        {
            throw new ExceptionInInitializerError(e);
        }
    }


Code:
javax.persistence.OptimisticLockException: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [WebOrganizer.classes.types.StringType#174]
   at org.hibernate.ejb.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:646)
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:600)
   at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:237)
   at WebOrganizer.web.servlets.TypeServlet.test2(TypeServlet.java:356)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:580)
   at org.testng.internal.Invoker.invokeMethod(Invoker.java:517)
   at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:669)
   at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:956)
   at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
   at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:110)
   at org.testng.TestRunner.runWorkers(TestRunner.java:720)
   at org.testng.TestRunner.privateRun(TestRunner.java:590)
   at org.testng.TestRunner.run(TestRunner.java:484)
   at org.testng.SuiteRunner.runTest(SuiteRunner.java:332)
   at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:327)
   at org.testng.SuiteRunner.privateRun(SuiteRunner.java:299)
   at org.testng.SuiteRunner.run(SuiteRunner.java:204)
   at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:864)
   at org.testng.TestNG.runSuitesLocally(TestNG.java:830)
   at org.testng.TestNG.run(TestNG.java:748)
   at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
   at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:124)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [WebOrganizer.classes.types.StringType#174]
   at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:261)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:120)
   at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:53)
   at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:677)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:661)
   at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:665)
   at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:228)
   ... 28 more


Last edited by Archmisha on Fri Jan 09, 2009 7:09 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 6:57 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
It would help if you post your mapping and source of your HibernateUtil. Why do you create three entityManagers?
(And why
Code:
            emX.getTransaction().begin();
            emX.clear();
            emX.close();

at the end of each transaction??)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 7:05 am 
Beginner
Beginner

Joined: Thu Aug 09, 2007 3:48 pm
Posts: 38
I made a simplified test case extracted from my app.
In the real case, it happens in different times....

HibernateUtil copied from tutorial.

open function: emf = Persistence.createEntityManagerFactory("manager1");
reserveEntityManager function: return emf.createEntityManager();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 09, 2009 6:51 pm 
Beginner
Beginner

Joined: Thu Aug 09, 2007 3:48 pm
Posts: 38
bump please :(


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.