如何在Google App Engine上的相同RPC请求中始终使用相同的PersistenceManager [英] How to always use the same PersistenceManager within the same RPC request on Google App Engine

查看:78
本文介绍了如何在Google App Engine上的相同RPC请求中始终使用相同的PersistenceManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以确保在同一RPC请求的上下文中执行的不同代码部分中使用相同的PersistenceManager实例?



例如,从函数到函数手动处理持久性管理器实例是一件非常痛苦的事情:例如,



  private void updateItem(ItemModel listItem)
throws UserNotLoggedInException {
PersistenceManager pm = PMF.get( ).getPersistenceManager();
if(isItemIsNew(pm,listItem)){
workOnItem(pm,listItem);
}
workSomeMoreOnItem(pm,listItem);



解决方案

Google Cookbook提供以下答案:


使用过滤器可以使PersistenceManager可用,并确保它在请求结束时关闭。如果您正在使用为您处理渲染的MVC框架(如Spring),那么这对于让管理器保持足够长的时间以使View仍能够获取尚未被访问的持久对象也很有用。



  public final class PersistenceFilter implements Filter {
private static final PersistenceManagerFactory persistenceManagerFactory
= JDOHelper.getPersistenceManagerFactory(transactions-optional);

private static PersistenceManagerFactory factory(){
return persistenceManagerFactory;
}

private static ThreadLocal currentManager = new ThreadLocal();
$ b $ public static PersistenceManager getManager(){
if(currentManager.get()== null){
currentManager.set(factory()。getPersistenceManager());
}
return currentManager.get();
}
@Override
public void doFilter(ServletRequest req,ServletResponse res,
FilterChain链)抛出IOException,ServletException {
PersistenceManager manager = null;
尝试{
manager = getManager();
//可选:允许所有持久化对象实现自定义接口
//,以通知它们何时被保存和加载。
manager.addInstanceLifecycleListener(new PersistHookListener(),PersistHooks.class);
chain.doFilter(req,res);
} finally {
if(manager!= null){
manager.flush();
manager.close();


$ b @Override
public void init(FilterConfig arg0)throws ServletException {}
@Override
public void destroy( ){}
}

http://appengine-cookbook.appspot.com/recipe/persistencefilter/?id=ahJhcHBlbmdpbmUtY29va2Jvb2tyigELEgtSZWNpcGVJbmRleCI2YWhKaGNIQmxibWRwYm1VdFkyOXZhMkp2YjJ0eUVnc1NDRU5oZEdWbmIzSjVJZ1JLWVhaaERBDAsSBlJlY2lwZSI3YWhKaGNIQmxibWRwYm1VdFkyOXZhMkp2YjJ0eUVnc1NDRU5oZEdWbmIzSjVJZ1JLWVhaaERBMAw


Is there a way to ensure the same PersistenceManager instance is used throughout the different code parts executed in the context of the same RPC request?

Having to manually handle out the persistence manager instance from function to function is quite a pain:

for example:

private void updateItem(ItemModel listItem)
        throws UserNotLoggedInException {
   PersistenceManager pm = PMF.get().getPersistenceManager();
   if (isItemIsNew(pm, listItem)) { 
        workOnItem(pm, listItem);
   }
   workSomeMoreOnItem(pm, listItem);

}

解决方案

Google Cookbook offers the following answer:

Using a filter, you can make the PersistenceManager available and guarantee that it is closed at the end of the request. If you are using an MVC framework that handles the rendering for you such as Spring, then this is also useful to keep the manager open long enough for the View to still be able to fetch persistent objects that haven't already been accessed.

public final class PersistenceFilter implements Filter {
    private static final PersistenceManagerFactory persistenceManagerFactory
        = JDOHelper.getPersistenceManagerFactory("transactions-optional");

    private static PersistenceManagerFactory factory() {
        return persistenceManagerFactory;
    }

    private static ThreadLocal currentManager = new ThreadLocal();

    public static PersistenceManager getManager() {
        if (currentManager.get() == null) {
            currentManager.set(factory().getPersistenceManager());
        }
        return currentManager.get();
    }
    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        PersistenceManager manager  = null;
        try {
            manager = getManager();
            //Optional: allow all persistent objects implementing a custom interface
            //to be notified of when they are saved and loaded.
            manager.addInstanceLifecycleListener(new PersistHookListener(), PersistHooks.class);
            chain.doFilter(req, res);
        } finally {
            if (manager != null) {
                manager.flush();
                manager.close();
            }
        }
    }
    @Override
    public void init(FilterConfig arg0) throws ServletException {}
    @Override
    public void destroy() {}
}

http://appengine-cookbook.appspot.com/recipe/persistencefilter/?id=ahJhcHBlbmdpbmUtY29va2Jvb2tyigELEgtSZWNpcGVJbmRleCI2YWhKaGNIQmxibWRwYm1VdFkyOXZhMkp2YjJ0eUVnc1NDRU5oZEdWbmIzSjVJZ1JLWVhaaERBDAsSBlJlY2lwZSI3YWhKaGNIQmxibWRwYm1VdFkyOXZhMkp2YjJ0eUVnc1NDRU5oZEdWbmIzSjVJZ1JLWVhaaERBMAw

这篇关于如何在Google App Engine上的相同RPC请求中始终使用相同的PersistenceManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆