1

I have a Spring application in GoogleApp engine which uses JDO to persists objects. So I'm using it in this way in my service layer to keep transactions (with @Transactional annotation).

@Transactional
public void save(Object object) {
    dao1.save(object); // dao1 saves using JDO
    Object object2 = generate1Object(object)
    dao2.save(object2); // dao2 saves using low level API
    Object object3 = generate2Object(object)
    dao3.save(object3); // dao3 saves using JDO
}

The problem I have is that I need to use the low level API to persist the object2 (because it's an advanced save which cannot be done with JDO). The problem I have is that I don't know how to keep they all in the same transaction. If the dao3 fails when saving the sving done in the love level is not rolledback.

So in the dao2 I persist in this way:

//save using low-level API
public void save(Object object){
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Entity entity = generateEntity(object);
    datastore.put(entity);
}

and in the dao1 and dao3 (the ones which use JDO) I have something like this:

//save using JDO
public void save(Object object) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    pm.makePersistent(object);
}

I've seen that the datastore object has a method called:

datastore.getCurrentTransaction()

but it is null for me. How can I make the low level to be aware of the transaction of JDO and keep the three daos in the same transaction.

Thanks.

Javi
  • 19,387
  • 30
  • 102
  • 135
  • An 'advanced save'? What does that mean? – Nick Johnson Mar 25 '11 at 02:15
  • @Nick Johnson it is just that the object to save has different property names based on UUID names. So when I save I must set each value in the column with that UUID. But I can't know in advance the UUID names, so I persist them using the low-level API. – Javi Mar 25 '11 at 07:58
  • It sounds like you should be using something like Objectify, rather than trying to hack a solution together with a combination of JDO and the low-level API. – Nick Johnson Mar 27 '11 at 05:36
  • @Nick Johnson I had already been thinking about it, though it's hard to change JDO to Objectify right now in the project. Thanks. – Javi Mar 28 '11 at 11:58

1 Answers1

1

just use jdo as normal and get hold of the bigtable connection

tx.begin();
...

// Get hold of native connection
JDOConnection jdoConn = pm.getDataStoreConnection();
... cast "jdoConn.getNativeConnection()" to the right type
... (do something with the connection)
jdoConn.close(); // Hands it back to JDO

tx.commit();

at least that is the theory, assuming GAE/J implements it

DataNucleus
  • 15,497
  • 3
  • 32
  • 37
  • I don't know the type I have to cast the object so I've tried to see in debug mode what getNativeConnection() method returns. When I do jdoConn.getNativeConnection() I get a null object (it's null before and after calling dao1.save(object) (I tried both) ). So I can't do anything with this object. – Javi Mar 24 '11 at 11:16
  • so raise a bug on http://code.google.com/p/datanucleus-appengine/issues/list since they claim to implement JDO but are missing that feature – DataNucleus Mar 24 '11 at 11:21