Google App Engine PersistenceManager can process multiple objects?
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,"nm67@yahoo.com","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 clo开发者_C百科sing it. Or do I have to get and close pm for processing each object ?
- 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:
- Some sort of Exception (PMF refuses to give more PM)
- 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.
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.
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,"nm67@yahoo.com","Servlet Error Message [ "+time+" ]",new Text(e.toString()));
}
}
}
}
finally
{
if(pm != null) pm.close();
}
精彩评论