Skip Links

Blog

Posts tagged with "tips".

Detaching objects in Google App Engine

Sid

Sid

05 Dec 2009 11:25

I was caught out the other day whilst writing a data service when I got a JDOException telling me I was accessing a field (in this case it was a serialized Collection) that hadn’t been detached.

This happens when you try to access an object, or collection within that object, which is still under the control of the PersistenceManager.

The PersistenceManager manages the persistence lifecycle of datastore objects, making sure they are synchronised with the underlying store. This is great in the data service layers, but most web applications will want to manipulate and display objects outside of this control before putting them back in to persistence-land. This is what detaching allows you to do.

The JDO spec is well worth a read as it describes the lifecycle in detail (a lot of detail :-)

There are several ways (including doing it manually) how you can handle the persisted object’s lifecycle and detaching in particular, but as we use transactions in our app the easiest was to use the JDO feature detachAllOnCommit which detaches an object and its collections. The code snippet below shows how.

Incidentally, although you can set detachAllOnCommit in JDO properties I prefer the explicit approach as it doesn’t hide potentially complex behaviour in configuration.

pm = jdoPersistenceHelper.getPersistenceManager();

pm.setDetachAllOnCommit(true);

tx = pm.currentTransaction();				

tx.begin();					

p = pm.getObjectById(Participant.class,participantId);				

tx.commit();					

pm.close();

...
// manipulate the paricipant here, for example
MailManager.invite(p.getEmail());

One final word about using Transactions in the Google App Engine. You can only enlist objects in a transaction that belong to the same EntityGroup. This means an object and its children (even objects of the same type are in different EntityGroups). We designed our application so that this would be easy for us, but it required careful thought and an app engine specific design. There’s more about transactions in the Google App Engine here which also addresses other ways to detach.

Tagged in: gaej, jdo, java, detached objects, tips

Expression language support with Google App Engine

Mark

Mark

07 Dec 2009 19:26

It took me quite a bit of searching and reading to find why although Google App Engine was using JSP 2.1 that my expression language expressions weren’t being evaluated.

You need to add


<%@ page isELIgnored="false" %>

into your JSP and then you’re away

Tagged in: gaej, JSP, tips