You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Cil-Gamir <ha...@rmb.co.za> on 2009/11/12 12:33:09 UTC

[SOLVED] Re: Locking Exception after Persisting new entity

Answering my own question

It turns out... when I save an existing object I must flush the entity
manager before persisting. On creating I should not flush. 

    public final void create(Object o) {
        getEntityManager().getTransaction().begin();
        getEntityManager().persist(o);
        getEntityManager().getTransaction().commit();
    }
    
    public final void update(Object o) {
        getEntityManager().getTransaction().begin();
        getEntityManager().persist(o);
        getEntityManager().flush();
        getEntityManager().getTransaction().commit();
    }

In the past I was using the create method (called save at the time) for
updating and saving.

Thanks
-- 
View this message in context: http://n2.nabble.com/Locking-Exception-after-Persisting-new-entity-tp3992010p3992165.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
Output of test class before it throws the Exception

2009-11-12 18:21:52,113 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] creating entitymanager
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] Found variable:
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] ID: 1081
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] Description: T1
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] Value: 27.0
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] New Value: 28.0
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] Updating Variable with
ID 1081
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] end dating old entity
2009-11-12 18:21:53,674 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# UPDATE
START
2009-11-12 18:21:53,763 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# UPDATE
DONE
2009-11-12 18:21:53,766 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# CREATE
START
2009-11-12 18:21:53,828 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# CREATE
DONE
2009-11-12 18:21:53,828 INFO  [simpleTest.SimpleTest]
############################################
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] Found variable:
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] ID: 1078
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] Description: T3
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] Value: 11.3
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] New Value: 12.3
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] Updating Variable with
ID 1078
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] end dating old entity
2009-11-12 18:21:53,882 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# UPDATE
START
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993983.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
This is a test class that shows the error

package simpleTest;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import za.co.rmb.rac.riskRatingEngineWeb.entities.Variable;
import za.co.rmb.rac.riskRatingEngineWeb.service.ThresholdService;
import za.co.rmb.rac.riskRatingEngineWeb.service.VariableService;
import za.co.rmb.rac.riskRatingEngineWeb.service.RegulatorService;;

public class SimpleTest {

    private Logger logger = Logger.getLogger(this.getClass().getName());
    private VariableService variableService = new VariableService();

    public static void main(String[] args) {
        SimpleTest st = new SimpleTest();
        st.doVariables();
    }
    
    private void doVariables(){
        Variable v = getVariableOne();
        logger.info("Found variable:");
        logger.info("ID: " + v.getId());
        logger.info("Description: " + v.getDescription());
        logger.info("Value: " + v.getValue());

        // update variable
        v.setValue(v.getValue() + 1);
        logger.info("New Value: " + v.getValue());
        updateVariable(v);

        logger.info("############################################");

        // get the new variable
        v = getVariableTwo();
        logger.info("Found variable:");
        logger.info("ID: " + v.getId());
        logger.info("Description: " + v.getDescription());
        logger.info("Value: " + v.getValue());
        // update variable again
        v.setValue(v.getValue() + 1);
        logger.info("New Value: " + v.getValue());
        updateVariable(v);        
    }


    private Variable getVariableOne() {
        List<Variable> variables =
variableService.getVariableByType("TestType", "T1", false);
        if (variables.size() > 1) {
            logger.error("More than one variable fetched");
            System.exit(1);
        }
        return variables.get(0);
    }

    private Variable getVariableTwo() {
        List<Variable> variables =
variableService.getVariableByType("TestType", "T3", false);
        if (variables.size() > 1) {
            logger.error("More than one variable fetched");
            System.exit(1);
        }
        return variables.get(0);
    }

    
    public String updateVariable(Variable variable) {
        logger.info("Updating Variable with ID " + variable.getId());

        logger.info("end dating old entity");
        // set the user and end date for the currently active variable
        variable.setEndDate(new Date());
        variable.setUserName("jhv");
        variableService.updateVariable(variable);

          //logger.info("creating new entity");
        Variable v2 = new Variable();
        v2.setDescription(variable.getDescription());
        v2.setStartDate(new Date());
        v2.setName(variable.getName());
        v2.setType(variable.getType());
       v2.setValue(variable.getValue());

        variableService.createVariable(v2);

        return "";
    }

  

}

-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993974.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
This is a test class that shows how the error happens

package simpleTest;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import za.co.rmb.rac.riskRatingEngineWeb.entities.Variable;
import za.co.rmb.rac.riskRatingEngineWeb.service.ThresholdService;
import za.co.rmb.rac.riskRatingEngineWeb.service.VariableService;
import za.co.rmb.rac.riskRatingEngineWeb.service.RegulatorService;;

public class SimpleTest {

    private Logger logger = Logger.getLogger(this.getClass().getName());
    private VariableService variableService = new VariableService();
    private ThresholdService thresholdService = new ThresholdService();
    private RegulatorService regulatorService = new RegulatorService();

    public static void main(String[] args) {
        SimpleTest st = new SimpleTest();
        st.doVariables();
    }
    
    private void doVariables(){
        Variable v = getVariableOne();
        logger.info("Found variable:");
        logger.info("ID: " + v.getId());
        logger.info("Description: " + v.getDescription());
        logger.info("Value: " + v.getValue());

        // update variable
        v.setValue(v.getValue() + 1);
        logger.info("New Value: " + v.getValue());
        updateVariable(v);

        logger.info("############################################");

        // get the new variable
        v = getVariableTwo();
        logger.info("Found variable:");
        logger.info("ID: " + v.getId());
        logger.info("Description: " + v.getDescription());
        logger.info("Value: " + v.getValue());
        // update variable again
        v.setValue(v.getValue() + 1);
        logger.info("New Value: " + v.getValue());
        updateVariable(v);        
    }


    private Variable getVariableOne() {
        List<Variable> variables =
variableService.getVariableByType("TestType", "T1", false);
        if (variables.size() > 1) {
            logger.error("More than one variable fetched");
            System.exit(1);
        }
        return variables.get(0);
    }

    private Variable getVariableTwo() {
        List<Variable> variables =
variableService.getVariableByType("TestType", "T3", false);
        if (variables.size() > 1) {
            logger.error("More than one variable fetched");
            System.exit(1);
        }
        return variables.get(0);
    }

    
    public String updateVariable(Variable variable) {
        logger.info("Updating Variable with ID " + variable.getId());

        logger.info("end dating old entity");
        // set the user and end date for the currently active variable
        variable.setEndDate(new Date());
        variable.setUserName("jhv");
        variableService.updateVariable(variable);

          //logger.info("creating new entity");
        Variable v2 = new Variable();
        v2.setDescription(variable.getDescription());
        v2.setStartDate(new Date());
        v2.setName(variable.getName());
        v2.setType(variable.getType());
       v2.setValue(variable.getValue());
    //    v2.setId(null);

        variableService.createVariable(v2);

        return "";
    }

  

}

WITH ITS OUTPUT

2009-11-12 18:21:52,113 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] creating entitymanager
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] Found variable:
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] ID: 1081
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] Description: T1
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] Value: 27.0
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] New Value: 28.0
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] Updating Variable with
ID 1081
2009-11-12 18:21:53,670 INFO  [simpleTest.SimpleTest] end dating old entity
2009-11-12 18:21:53,674 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# UPDATE
START
2009-11-12 18:21:53,763 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# UPDATE
DONE
2009-11-12 18:21:53,766 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# CREATE
START
2009-11-12 18:21:53,828 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# CREATE
DONE
2009-11-12 18:21:53,828 INFO  [simpleTest.SimpleTest]
############################################
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] Found variable:
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] ID: 1078
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] Description: T3
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] Value: 11.3
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] New Value: 12.3
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] Updating Variable with
ID 1078
2009-11-12 18:21:53,881 INFO  [simpleTest.SimpleTest] end dating old entity
2009-11-12 18:21:53,882 INFO 
[za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO] ############# UPDATE
START
Exception in thread "main" <openjpa-1.2.1-r752877:753278 fatal store error>
org.apache.openjpa.persistence.RollbackException: Optimistic locking errors
were detected when flushing to the data store.  The following objects may
have been concurrently modified in another transaction:
[org.apache.openjpa.enhance.za$co$rmb$rac$riskRatingEngineWeb$entities$Variable$pcsubclass-1081,
za.co.rmb.rac.riskRatingEngineWeb.entities.Variable-0]
	at
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:523)
	at za.co.rmb.rac.basicjpadao.DAO.update(DAO.java:41)
	at
za.co.rmb.rac.riskRatingEngineWeb.service.VariableService.updateVariable(VariableService.java:24)
	at simpleTest.SimpleTest.updateVariable(SimpleTest.java:168)
	at simpleTest.SimpleTest.doVariables(SimpleTest.java:50)
	at simpleTest.SimpleTest.main(SimpleTest.java:24)
Caused by: <openjpa-1.2.1-r752877:753278 nonfatal store error>
org.apache.openjpa.persistence.OptimisticLockException: Optimistic locking
errors were detected when flushing to the data store.  The following objects
may have been concurrently modified in another transaction:
[org.apache.openjpa.enhance.za$co$rmb$rac$riskRatingEngineWeb$entities$Variable$pcsubclass-1081,
za.co.rmb.rac.riskRatingEngineWeb.entities.Variable-0]
	at
org.apache.openjpa.kernel.BrokerImpl.newFlushException(BrokerImpl.java:2160)
	at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2010)
	at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1908)
	at
org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1826)
	at
org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:81)
	at org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1350)
	at
org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:877)
	at
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:512)
	... 5 more
Caused by: <openjpa-1.2.1-r752877:753278 nonfatal store error>
org.apache.openjpa.persistence.OptimisticLockException: An optimistic lock
violation was detected when flushing object instance
"org.apache.openjpa.enhance.za$co$rmb$rac$riskRatingEngineWeb$entities$Variable$pcsubclass-1081"
to the data store.  This indicates that the object was concurrently modified
in another transaction.
FailedObject:
org.apache.openjpa.enhance.za$co$rmb$rac$riskRatingEngineWeb$entities$Variable$pcsubclass@162522b
	at
org.apache.openjpa.jdbc.kernel.BatchingPreparedStatementManagerImpl.checkUpdateCount(BatchingPreparedStatementManagerImpl.java:269)
	at
org.apache.openjpa.jdbc.kernel.BatchingPreparedStatementManagerImpl.flushBatch(BatchingPreparedStatementManagerImpl.java:189)
	at
org.apache.openjpa.jdbc.kernel.BatchingConstraintUpdateManager.flush(BatchingConstraintUpdateManager.java:63)
	at
org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:89)
	at
org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:72)
	at
org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:717)
	at
org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:130)
	... 12 more

-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993965.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
This is a service class between the front end and the DAO

package za.co.rmb.rac.riskRatingEngineWeb.service;

import java.util.List;
import za.co.rmb.rac.riskRatingEngineWeb.dao.RiskRatingDAO;
import za.co.rmb.rac.riskRatingEngineWeb.entities.Variable;

/**
 * 
 * @author JHV
 */
public class VariableService {

    private RiskRatingDAO dao;

    public VariableService() {
        dao = RiskRatingDAO.getInstance();
    }

    public void createVariable(Variable variable) {
        dao.create(variable);
    }

    public void updateVariable(Variable variable) {
        dao.update(variable);
    }
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993926.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

RE: Locking Exception after Persisting new entity

Posted by Michael Vorburger <mv...@odyssey-group.com>.
> I think build time enhancement solved it.

FYI: The run-time enhancement via subclassing has lot's of other
problems (the agent seems fine though; it does essentially the same
thing as the build time thing)... we specify
openjpa.RuntimeUnenhancedClasses = unsupported (@see
http://openjpa.apache.org/builds/1.2.1/apache-openjpa-1.2.1/docs/manual/
ref_guide_conf_openjpa.html#openjpa.RuntimeUnenhancedClasses) in our
OpenJPA bootstrap stuff to prevent it in all situations (final runtime &
unit/integration tests).

PS, also see https://issues.apache.org/jira/browse/OPENJPA-651.


-----Original Message-----
From: Cil-Gamir [mailto:hannes.visagie@rmb.co.za] 
Sent: Friday, November 13, 2009 8:56 AM
To: users@openjpa.apache.org
Subject: Re: Locking Exception after Persisting new entity


Hi

I think build time enhancement solved it.

I'm getting a different error now

org.apache.openjpa.persistence.EntityExistsException: An object of type
"za.co.rmb.rac.riskRatingEngineWeb.entities.Variable" with oid "0"
already exists in this context; another cannot be persisted.

But at least the optimistic locking thing has taken a hike

Unfortunately I have a teambuilding thing on today... so I can't fix
this thing any further.

Thanks a lot for the help Daryl

Regards

Hannes
--
View this message in context:
http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new
-entity-tp3992651p3997874.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

____________________________________________________________

� This email and any files transmitted with it are CONFIDENTIAL and intended
  solely for the use of the individual or entity to which they are addressed.
� Any unauthorized copying, disclosure, or distribution of the material within
  this email is strictly forbidden.
� Any views or opinions presented within this e-mail are solely those of the
  author and do not necessarily represent those of Odyssey Financial
Technologies SA unless otherwise specifically stated.
� An electronic message is not binding on its sender. Any message referring to
  a binding engagement must be confirmed in writing and duly signed.
� If you have received this email in error, please notify the sender immediately
  and delete the original.

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
Hi

I think build time enhancement solved it.

I'm getting a different error now

org.apache.openjpa.persistence.EntityExistsException: An object of type
"za.co.rmb.rac.riskRatingEngineWeb.entities.Variable" with oid "0" already
exists in this context; another cannot be persisted.

But at least the optimistic locking thing has taken a hike

Unfortunately I have a teambuilding thing on today... so I can't fix this
thing any further.

Thanks a lot for the help Daryl

Regards

Hannes
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3997874.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
Thanks Daryl

I'll try closing the EM, not caching it in the DAO and using built time
enhancement.

Will tell U how it goes

H
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3994710.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Nov 12, 2009 at 12:44 PM, Cil-Gamir <ha...@rmb.co.za>wrote:

>
> Hi Daryl
>
> I'm not closing the entity manager, where should I do this ?
>

Hard to say. What kind of app are you making? I have a webapp, so I have a
lazy creation mechanism for the em, at the completion of the HTTP request
thread, the EM is closed. So I have one EM per thread that is created and
closed in the lifetime of a single HTTP Request.


>
> I'm also not familiar with enhancing, my entity classes were declared as
> final, and JPA complained saying that it cannot do the enhancement for me
> if
> my classes are final.
>
> If I want to do the build time enhancement, where is a good place to start
> to figure out how.
>

Here's an Ant task that won't work with 1.2.1 if you reference enums:

<target name="EnhanceJPA">
<openjpac>
<classpath path="${classes}" />
<config propertiesFile="${src}/META-INF/persistence.xml"
connectionUserName="user"
connectionPassword="pw"
connectionURL="jdbc:postgresql://localhost/mydb"
connectionDriverName="org.postgresql.Driver" />
<fileset dir=".">
<include name="**/model/*.java" />
</fileset>
</openjpac>
</target>

Here's one that will:

<target name="EnhanceJPA">
<java classname="org.apache.openjpa.enhance.PCEnhancer">
<classpath>
<pathelement path="${classes}"/>
      <pathelement path="${classpath}"/>
      <fileset dir="/path/to/openjpa">
        <include name="**/*.jar"/>
      </fileset>
</classpath>
</java>
</target>


If I only save without persisting a new entity I can do that the whole day.
> but once I create a new entity the subsequent operation on any other entity
> or the newly saved one throws an optimistic locking exception
>

It's possible build time enhancement may take care of this, I don't know,
but you should be doing build-time anyway and depending on your app, closing
the em's.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
I see I can do build time enhancement with Maven2, I'll try that tomorrow and
report back

Not at work now

Thanks

H
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3994570.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Michael Dick <mi...@gmail.com>.
Hi,

Comments inline

On Thu, Nov 12, 2009 at 11:44 AM, Cil-Gamir <ha...@rmb.co.za>wrote:

>
> Hi Daryl
>
> I'm not closing the entity manager, where should I do this ?
>

Well, when you're done using it.. From a quick look at your code you cache
the EntityManager in your DAO, but never call the setEntityManager method.
So you get a new instance of the EM each time you call getEntityManager().

I'd suggest updating your main test to cache the em. Something like this :
. . .
EntityManager em = getEntityManager();
em.getTransaction().begin();
em.persist(o);
log.info("############# CREATE START");
em.commit();
em.close();
 . . .
em = getEntityManager()
 . . .

I'd avoid caching it in the DAO for the time being..

> I'm also not familiar with enhancing, my entity classes were declared as
> final, and JPA complained saying that it cannot do the enhancement for me
> if
> my classes are final.
>

<brief aside>
That's a limitation of the subclassing approach to enhancement. If you run
the stand-alone PCEnhancer tool you don't have to worry about final classes
(AFAIK).
</brief aside>


> If I want to do the build time enhancement, where is a good place to start
> to figure out how.
>
>
Here's a good place to start :
http://webspherepersistence.blogspot.com/2009/02/openjpa-enhancement.html


> The basic scenario is as follows.
>
> 1. I fetch an entity
> 2. I make a change to that entity and I save it (saving here involves
> creating a new entity as i'm trying to version rows in the database).
> 3. I fetch another different entity
> 4. upon trying to save that entity it fails.
>
> If I only save without persisting a new entity I can do that the whole day.
> but once I create a new entity the subsequent operation on any other entity
> or the newly saved one throws an optimistic locking exception
> --
> View this message in context:
> http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3994474.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
Hi Daryl

I'm not closing the entity manager, where should I do this ?

I'm also not familiar with enhancing, my entity classes were declared as
final, and JPA complained saying that it cannot do the enhancement for me if
my classes are final. 

If I want to do the build time enhancement, where is a good place to start
to figure out how.

The basic scenario is as follows.

1. I fetch an entity
2. I make a change to that entity and I save it (saving here involves
creating a new entity as i'm trying to version rows in the database). 
3. I fetch another different entity
4. upon trying to save that entity it fails.

If I only save without persisting a new entity I can do that the whole day.
but once I create a new entity the subsequent operation on any other entity
or the newly saved one throws an optimistic locking exception
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3994474.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Nov 12, 2009 at 11:26 AM, Cil-Gamir <ha...@rmb.co.za>wrote:

>    @Override
>    public EntityManager getEntityManager() {
>        if (entityManager == null) {
>            log.info("creating entitymanager");
>            entityManager =
>
> Persistence.createEntityManagerFactory("risk-rating-data-engine").createEntityManager();
>        }
>        return entityManager;
>    }
>
>
I see where you are creating your entity manager. Where do you close it?

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
The DAO Above gets extended by this class

package za.co.rmb.rac.riskRatingEngineWeb.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.OptimisticLockException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.apache.log4j.Logger;

import za.co.rmb.rac.basicjpadao.DAO;
import za.co.rmb.rac.riskRatingEngineWeb.entities.Threshold;
import za.co.rmb.rac.riskRatingEngineWeb.entities.Variable;
import za.co.rmb.rac.riskRatingEngineWeb.entities.Regulator;

public class RiskRatingDAO extends DAO {

    private static RiskRatingDAO instance;
    private static Logger log = Logger.getLogger(RiskRatingDAO.class);
    private EntityManager entityManager;
    private Logger logger = Logger.getLogger(this.getClass().getName());

    private RiskRatingDAO() {
    }

    public static RiskRatingDAO getInstance() {
        if (instance == null) {
            instance = new RiskRatingDAO();
        }
        return instance;
    }

    @Override
    public EntityManager getEntityManager() {
        if (entityManager == null) {
            log.info("creating entitymanager");
            entityManager =
Persistence.createEntityManagerFactory("risk-rating-data-engine").createEntityManager();
        }
        return entityManager;
    }

    @Override
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }   
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993922.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
This is my DAO

package za.co.rmb.rac.basicjpadao;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.Transient;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author nico
 */
public abstract class DAO {

    public abstract EntityManager getEntityManager();

    public abstract void setEntityManager(EntityManager entityManager);
    private Logger log = Logger.getLogger(this.getClass());

    public final void create(Object o) {
        getEntityManager().getTransaction().begin();
        getEntityManager().persist(o);
        log.info("############# CREATE START");
        getEntityManager().getTransaction().commit();
        log.info("############# CREATE DONE");        
    }
    
    public final void update(Object o) {
        getEntityManager().getTransaction().begin();
        getEntityManager().merge(o);
        //getEntityManager().flush();
        log.info("############# UPDATE START");
        getEntityManager().getTransaction().commit();
        log.info("############# UPDATE DONE");
    }
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993916.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
This is my Entity class

package za.co.rmb.rac.riskRatingEngineWeb.entities;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
 * 
 * @author jhv
 */
@Entity
@Table(name = "KYC_P2_VARIABLES")
public class Variable implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "NAME")
    private String name;
    @Basic(optional = false)
    @Column(name = "TYPE")
    private String type;
    @Basic(optional = false)
    @Column(name = "VALUE")
    private Double value;
    @Basic(optional = false)
    @Column(name = "START_DATE")
    //@Temporal(TemporalType.TIMESTAMP)
    private Date startDate;
    @Basic(optional = true)
    @Column(name = "END_DATE")
    //@Temporal(TemporalType.TIMESTAMP)
    private Date endDate;
    @Basic(optional = true)
    @Column(name = "USER_NAME")
    private String userName;
    @Basic(optional = false)
    @Column(name = "DESCRIPTION")
    private String description;

.... getters and setters follow, auto generated with netbeans 6.7
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993911.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Nov 12, 2009 at 11:23 AM, Cil-Gamir <ha...@rmb.co.za>wrote:

>
> This is the Exception stack
>
> Exception in thread "main" <openjpa-1.2.1-r752877:753278 fatal store error>
> org.apache.openjpa.persistence.RollbackException: Optimistic locking errors
> were detected when flushing to the data store.  The following objects may
> have been concurrently modified in another transaction:
> [org.apache.openjpa.enhance.za
> $co$rmb$rac$riskRatingEngineWeb$entities$Variable$pcsubclass-1081,
> za.co.rmb.rac.riskRatingEngineWeb.entities.Variable-0]
>

The first question "the experts" usually ask is "Are you using build-time or
run-time enhancement?" If you are using run-time as appears to be the case
above, at least to me, try build-time.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
This is the Exception stack

Exception in thread "main" <openjpa-1.2.1-r752877:753278 fatal store error>
org.apache.openjpa.persistence.RollbackException: Optimistic locking errors
were detected when flushing to the data store.  The following objects may
have been concurrently modified in another transaction:
[org.apache.openjpa.enhance.za$co$rmb$rac$riskRatingEngineWeb$entities$Variable$pcsubclass-1081,
za.co.rmb.rac.riskRatingEngineWeb.entities.Variable-0]
	at
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:523)
	at za.co.rmb.rac.basicjpadao.DAO.update(DAO.java:41)
	at
za.co.rmb.rac.riskRatingEngineWeb.service.VariableService.updateVariable(VariableService.java:24)
	at simpleTest.SimpleTest.updateVariable(SimpleTest.java:168)
	at simpleTest.SimpleTest.doVariables(SimpleTest.java:50)
	at simpleTest.SimpleTest.main(SimpleTest.java:24)
Caused by: <openjpa-1.2.1-r752877:753278 nonfatal store error>
org.apache.openjpa.persistence.OptimisticLockException: Optimistic locking
errors were detected when flushing to the data store.  The following objects
may have been concurrently modified in another transaction:
[org.apache.openjpa.enhance.za$co$rmb$rac$riskRatingEngineWeb$entities$Variable$pcsubclass-1081,
za.co.rmb.rac.riskRatingEngineWeb.entities.Variable-0]
	at
org.apache.openjpa.kernel.BrokerImpl.newFlushException(BrokerImpl.java:2160)
	at org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:2010)
	at org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1908)
	at
org.apache.openjpa.kernel.BrokerImpl.beforeCompletion(BrokerImpl.java:1826)
	at
org.apache.openjpa.kernel.LocalManagedRuntime.commit(LocalManagedRuntime.java:81)
	at org.apache.openjpa.kernel.BrokerImpl.commit(BrokerImpl.java:1350)
	at
org.apache.openjpa.kernel.DelegatingBroker.commit(DelegatingBroker.java:877)
	at
org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:512)
	... 5 more
Caused by: <openjpa-1.2.1-r752877:753278 nonfatal store error>
org.apache.openjpa.persistence.OptimisticLockException: An optimistic lock
violation was detected when flushing object instance
"org.apache.openjpa.enhance.za$co$rmb$rac$riskRatingEngineWeb$entities$Variable$pcsubclass-1081"
to the data store.  This indicates that the object was concurrently modified
in another transaction.
FailedObject:
org.apache.openjpa.enhance.za$co$rmb$rac$riskRatingEngineWeb$entities$Variable$pcsubclass@162522b
	at
org.apache.openjpa.jdbc.kernel.BatchingPreparedStatementManagerImpl.checkUpdateCount(BatchingPreparedStatementManagerImpl.java:269)
	at
org.apache.openjpa.jdbc.kernel.BatchingPreparedStatementManagerImpl.flushBatch(BatchingPreparedStatementManagerImpl.java:189)
	at
org.apache.openjpa.jdbc.kernel.BatchingConstraintUpdateManager.flush(BatchingConstraintUpdateManager.java:63)
	at
org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:89)
	at
org.apache.openjpa.jdbc.kernel.AbstractUpdateManager.flush(AbstractUpdateManager.java:72)
	at
org.apache.openjpa.jdbc.kernel.JDBCStoreManager.flush(JDBCStoreManager.java:717)
	at
org.apache.openjpa.kernel.DelegatingStoreManager.flush(DelegatingStoreManager.java:130)
	... 12 more
-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993884.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Nov 12, 2009 at 11:10 AM, Cil-Gamir <ha...@rmb.co.za>wrote:

>
> I've now added a third managed entity, so I am managing three tables now.
>
> All three tables use the same DAO methods to persist objects, the only
> difference between them are the Entity classes.
>
> One of them works and the other two doesn't.
>

You could start by posting the exception stack. Then your classes with
annotations (or xml) and maybe your DAO code. Also describe how you are
getting entity manager instances and how you are managing the life cycle of
the entity managers.

I doubt I can help you but I think you need to provide more information.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
I've now added a third managed entity, so I am managing three tables now.

All three tables use the same DAO methods to persist objects, the only
difference between them are the Entity classes.

One of them works and the other two doesn't.

I've been looking at this for two days now, I have no explanation.

-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993780.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: Locking Exception after Persisting new entity

Posted by Cil-Gamir <ha...@rmb.co.za>.
I thought I had it working, but I was mistaken

It doesn't matter if I merge on update and persist on create. After a
persist I cannot do another merge or persist to the database without ending
in an optimistic locking exception.

The solution is REALLY simple, I've got one table that doesn't have any
relationships to any other tables.

Does anyone have any advice.



-- 
View this message in context: http://n2.nabble.com/Re-SOLVED-Re-Locking-Exception-after-Persisting-new-entity-tp3992651p3993427.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: [SOLVED] Re: Locking Exception after Persisting new entity

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, Nov 12, 2009 at 6:33 AM, Cil-Gamir <ha...@rmb.co.za> wrote:

> In the past I was using the create method (called save at the time) for
> updating and saving.
>
> I use a single "save" method for create and update. If the object's ID is
null I do a persist. If not null I do a merge. Works fine.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com