I playing around with GWT RPC & App Engine and was wondering the same
thing today and I ended up using a ThreadLocal.Also I am going to do
some refactoring it to make it work with dependency injection so I can
reuse it in the future.
Pros:
* I have one PersistenceManager per RPC request.
* Less code on every method to handle exceptions and closing
connections
* Entity objets being passed around between methods are not detached.
Cons:
* Increases runtime exceptions if you forget to initiate the
PersistenceManager
* Currently not very friendly for unit testing of your RPC calls but
can be fixed.
PMF.java
public final class PMF {
private static final PersistenceManagerFactory pmfInstance =
JDOHelper
.getPersistenceManagerFactory("transactions-optional");
private static final ThreadLocal<PersistenceManager> pmLocal = new
ThreadLocal<PersistenceManager>();
private PMF() {
}
public static PersistenceManager initPersistenceManager() {
pmLocal.set(getPersistenceManagerFactory().getPersistenceManager());
return pmLocal.get();
}
public static PersistenceManager getPersistenceManager() {
PersistenceManager pm = pmLocal.get();
if (null == pm) {
throw new IllegalStateException(
"You must call initPersitenceManager
first.");
} else if (pm.isClosed()) {
throw new IllegalStateException(
"The persistence manager is closed. Was
close called on it before
you expected.");
}
return pm;
}
private static PersistenceManagerFactory getPersistenceManagerFactory
() {
return pmfInstance;
}
}
RPC Service Implementation Method
///////////////////////////
PMF.initPersistenceManager();
try{
// Call some methods that call this -> PersistenceManager pm =
PMF.getPersistenceManager();
}catch (Exception e) {
// Handle Exception in a graceful way
}finally{
PMF.getPersistenceManager().close();
}
On Dec 5, 12:30 am, Fan Lin <[email protected]> wrote:
> Hi, I'm new in JDO, so I think this problem may sounds stupid...
> I'm working on my application on App-engine, and feel really confused
> about how to use the PersistenceManager in my app...
>
> 1) At the very beginning I tried to maintain only one static pm
> instance in my app, but I found that when I try to update the entities
> in datastore, it does not write back, since I do not close the pm.
>
> 2) Then, I tried to get a new pm in every request, and after the
> request I close it. But I still store the pm as a static field in a
> DaoBase class. the code looks like:
>
> PersistenceManager pm = PFM.get().getPersistenceManager();
> DaoBase.setPm(pm);
>
> ....do the request...
>
> DaoBase.getPm().close();
> DaoBase.setPm(null);
>
> But when multiple requests come concurrently, things becomes messy and
> sometimes the DaoBase.getPm() will returns null during a request.
>
> 3) Use new pm each time when reading the datastore, then detach the
> object from pm, close the pm. Then during update time, use a new pm to
> update it. But the problem here is when there is some owned
> relationship between datastore entities, a detatched object will not
> fetch the data automatically, like:
>
> /////////////////////////////
> class Address {
> ....
>
> }
>
> class Employee {
> private List<Address> addressList;
>
> public List<Address> getAddressList() {
> return addressList;
> }}
>
> ///////////////////////////////
> the getAddressList() will always return null.
>
> So...What is the right way to use PersistenceManager?
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.