[appengine-java] Re: What's the right way to use PersistenceManager?

2009-12-07 Thread Larry Cable
I am not sure it/there is a "right" way, but in my application I am
injecting (via Spring)
a (singleton) PMF into my Controller(s) (which are singletons) and
instantiating a PM for each (concurrent) query/request...

On Dec 4, 11:30 pm, Fan Lin  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 addressList;
>
>    public List 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 google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.




[appengine-java] Re: What's the right way to use PersistenceManager?

2009-12-08 Thread David Chandler
I've created an interface PMF:

PMF.java
package com.turbomanage.gwt.server;

import javax.jdo.PersistenceManager;

public interface PMF
{
PersistenceManager getPersistenceManager();
}

and a default implementation that I inject via Guice into my service
classes:

DefaultPMF.java
package com.turbomanage.gwt.server;

import javax.jdo.PersistenceManager;

public interface PMF
{
PersistenceManager getPersistenceManager();
}

Each service then calls pmf.getPersistenceManager(). I hope this is
right :-)

A benefit of the injection approach is that you can easily create a
TestPMF that is injected for unit tests. I've written a little about
this at 
http://turbomanage.wordpress.com/2009/10/27/more-on-unit-testing-with-an-injected-jdo-persistencemanager/

/dmc
David Chandler
http://turbomanage.wordpress.com

On Dec 7, 1:17 pm, Larry Cable  wrote:
> I am not sure it/there is a "right" way, but in my application I am
> injecting (via Spring)
> a (singleton) PMF into my Controller(s) (which are singletons) and
> instantiating a PM for each (concurrent) query/request...
>
> On Dec 4, 11:30 pm, Fan Lin  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 addressList;
>
> >    public List 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 google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.




[appengine-java] Re: What's the right way to use PersistenceManager?

2009-12-28 Thread patr...@kodova.com
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 pmLocal = new
ThreadLocal();

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  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 addressList;
>
>    public List 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 google-appengine-j...@googlegroups.com.
To unsubscribe from this group, send email to 
google-appengine-java+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.