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.
|