Mariselvam
Mariselvam

Reputation: 1113

JDO Exception in google app engine transaction

I am getting the following exception while trying to use transation in app engine datastore.

javax.jdo.JDOUserException: Transaction is still active.
You should always close your transactions correctly using commit() or rollback().

FailedObject:org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManager@12bbe6b
at org.datanucleus.jdo.JDOPersistenceManager.close(JDOPersistenceManager.java:277)

The following is the code snippet I used :

List<String> friendIds = getFriends(userId);
Date currentDate = new Date();
PersistenceManager manager = pmfInstance.getPersistenceManager();
try {
   Transaction trans = manager.currentTransaction();
   trans.begin();
   for(String friendId : friendIds) {
      User user = manager.getObjectById(User.class, friendId);
      if(user != null) {
         user.setRecoCount(user.getRecoCount() + 1);
         user.setUpdatedDate(currentDate);
         manager.makePersistent(user);
      }
   }
   trans.commit();
} finally {
   manager.close();
}

Upvotes: 1

Views: 1306

Answers (3)

bsautner
bsautner

Reputation: 4802

I was able to reproduce this - if you declare your transaction inside the try block and close the pm in the finally. You won't get this message if you move your

Transaction trans = manager.currentTransaction(); trans.begin();

to above the try { } section like this:

PersistenceManager pm = PMF.get().getPersistenceManager(); Transaction tx = pm.currentTransaction(); tx.begin(); try { //do my thing tx.commit(); } } catch (Exception e) { tx.rollback(); } finally { pm.close(); }

javax.jdo.JDOUserException: Transaction is still active. You should always close your transactions correctly using commit() or rollback().

Upvotes: 1

Mostafa Amer
Mostafa Amer

Reputation: 1

I think the different 'User' object are not belonging to the same Entity Group. All datastore operations in a transaction must operate on entities in the same entity group.

You may begin the transaction inside the loop , so you will be operating on one entity at a time, or make sure all your objects are in the same group.

Upvotes: 0

DataNucleus
DataNucleus

Reputation: 15577

and if the commit or makePersistent fails where is the call to "rollback" ?

Upvotes: 3

Related Questions