Google App Engine PersistenceManager 可以处理多个对象吗?

发布于 2024-08-30 12:22:56 字数 1222 浏览 1 评论 0原文

我有一些这样的代码:

        PersistenceManager pm=PMF.get().getPersistenceManager();
        String query="select from "+PayPal_Message.class.getName()+" where processed == false order by time desc";
        List<PayPal_Message> messages=(List<PayPal_Message>)pm.newQuery(query).execute();
        if (messages.isEmpty())
        {
        }
        else
        {
          for (PayPal_Message g : messages)
          {
            Contact_Info_Entry A_Contact_Entry=Process_PayPal_Message_To_Get_A_License(g.getContent().getValue());
            pm=PMF.get().getPersistenceManager();
            try
            {
              pm.makePersistent(A_Contact_Entry);
              g.setProcessed(true);
              pm.makePersistent(g);
            }
            catch (Exception e)
            {
              Send_Email(Email_From,"[email protected]","Servlet Error Message [ "+time+" ]",new Text(e.toString()));
            }
//            finally { pm.close(); }

          }
        }
        pm.close();

我想知道在关闭之前使用上面的 pm 处理多个对象是否可以。或者我是否必须获取并关闭 pm 来处理每个对象?

I have some code like this :

        PersistenceManager pm=PMF.get().getPersistenceManager();
        String query="select from "+PayPal_Message.class.getName()+" where processed == false order by time desc";
        List<PayPal_Message> messages=(List<PayPal_Message>)pm.newQuery(query).execute();
        if (messages.isEmpty())
        {
        }
        else
        {
          for (PayPal_Message g : messages)
          {
            Contact_Info_Entry A_Contact_Entry=Process_PayPal_Message_To_Get_A_License(g.getContent().getValue());
            pm=PMF.get().getPersistenceManager();
            try
            {
              pm.makePersistent(A_Contact_Entry);
              g.setProcessed(true);
              pm.makePersistent(g);
            }
            catch (Exception e)
            {
              Send_Email(Email_From,"[email protected]","Servlet Error Message [ "+time+" ]",new Text(e.toString()));
            }
//            finally { pm.close(); }

          }
        }
        pm.close();

I wonder if it's ok to use the pm above to process multiple objects before closing it. Or do I have to get and close pm for processing each object ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Spring初心 2024-09-06 12:22:56
  • 每次您请求时,PersistentManagerFactory 都会为您提供一个新的 PersistenceManager。
  • 如果应用程序不能处理太多请求,那就没问题。
  • 如果应用程序处理大量请求,您可能会得到:
    1. 某种异常(PMF 拒绝提供更多 PM)
    2. Google 收取更多费用(效率低下)

使用 PM 对多个对象执行操作有 2 种方法:

  • 批处理(创建、更新、删除多个对象)
  • 事务(执行一个或多个业务逻辑/规则并持久化)

批处理仅限于同一类型的对象,而事务仅限于同一组的实体。

一些建议:

  • 始终关闭 PM
  • 或者,如果您想在关闭 PM 后使用对象(即您希望 JSP 呈现对象列表,但您的 servlet 已经关闭 PM),您可以使用分离机制

我希望这有帮助。

  • PersistentManagerFactory will give you a new PersistenceManager every time you ask for it.
  • If the app doesn't deal with too many requests, you're okay.
  • If the app deals with lots of requests, you might either get:
    1. Some sort of Exception (PMF refuses to give more PM)
    2. Billed more by Google (inefficient)

There are 2 ways to use PM to perform operations to multiple objects:

  • Batch processing (Create, Update, Delete multiple objects)
  • Transaction (Execute one or more business logics/rules and persist)

Batch processing is limited to objects with the same type while transaction is limited to entities of the same group.

Some advises:

  • Always close your PM
  • Alternatively you can use the detach mechanism if you want to use the objects after you close the PM (i.e. you want JSP to render a list of objects, but your servlet already close your PM)

I hope this help.

独守阴晴ぅ圆缺 2024-09-06 12:22:56

您应该尝试使用相同的 PersistenceManager 来完成尽可能多的工作。

一直购买新的可能会导致不必要的开销。如果您想使用事务,我相信无论如何您都必须使用同一个管理器来执行其中的所有活动。

You should try to use the same PersistenceManager for as much work as possible.

Getting a new one all the time probably results in unnecessary overhead. If you want to use transactions, I believe you have to use the same manager for all activities in there anyway.

゛时过境迁 2024-09-06 12:22:56

试试这个...

PersistenceManager pm = null;
try
{
            pm=PMF.get().getPersistenceManager();
            String query="select from "+PayPal_Message.class.getName()+" where processed == false order by time desc";
            List<PayPal_Message> messages=(List<PayPal_Message>)pm.newQuery(query).execute();
            if (!messages.isEmpty())
            {
              for (PayPal_Message g : messages)
              {
                Contact_Info_Entry A_Contact_Entry=Process_PayPal_Message_To_Get_A_License(g.getContent().getValue());
                try
                {
                  pm.makePersistent(A_Contact_Entry);
                  g.setProcessed(true);
                  pm.makePersistent(g);
                }
                catch (Exception e)
                {
                  Send_Email(Email_From,"[email protected]","Servlet Error Message [ "+time+" ]",new Text(e.toString()));
                }
              }
            }
}
finally
{
 if(pm != null) pm.close();
}

Try this ...

PersistenceManager pm = null;
try
{
            pm=PMF.get().getPersistenceManager();
            String query="select from "+PayPal_Message.class.getName()+" where processed == false order by time desc";
            List<PayPal_Message> messages=(List<PayPal_Message>)pm.newQuery(query).execute();
            if (!messages.isEmpty())
            {
              for (PayPal_Message g : messages)
              {
                Contact_Info_Entry A_Contact_Entry=Process_PayPal_Message_To_Get_A_License(g.getContent().getValue());
                try
                {
                  pm.makePersistent(A_Contact_Entry);
                  g.setProcessed(true);
                  pm.makePersistent(g);
                }
                catch (Exception e)
                {
                  Send_Email(Email_From,"[email protected]","Servlet Error Message [ "+time+" ]",new Text(e.toString()));
                }
              }
            }
}
finally
{
 if(pm != null) pm.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文