Understanding transactions and rollback

4 views
Skip to first unread message

Brad

unread,
Jul 25, 2010, 10:39:44 AM7/25/10
to Google App Engine
The documentation says that transactions can only be done within a
single entity group. Does this mean the tr tansaction in the
'removeMessage', is not safe?

In the following code, I need to break the link between the parent and
the child, ie by removing the child, and re-inserting it with no
parent. Perhaps what I am doing is crazy? (:

// Not meant to compile, it is for example only
public class UndeliveredMessages {

private Key key;
private long count = 0;
private Set<Message> messages = HashSet<Message>();

public void removeMessage(Message message) {
Transaction tx = pm.currentTransaction();
try {
count--;
messages.remove(message);
pm.makePersistent(this);
Message item = new Message();
item.setFrom(message.getFrom());
item.setTo(message.getTo());
item.setText(message.getText());
pm.makePersistent(message); // If this one fails,
will all of the above be rolled back?
tx.commit();
} finally {
if (tx.isActive())
tx.rollback();
}
}

public void addMessage(String from, String to, String message) {
count++;
messages.put(new Message(from, to, message));
pm.makePersistent();
}

public Message getNextMessage() {
if(!messages.isEmpty())
return messages.iterator().next();

return null;
}

}

This is a contrived example to help me understand if this will work. I
realise/suspect I could use "weak" references, ie just store a set of
references to the child objects, ie "Set<Key>".
Reply all
Reply to author
Forward
0 new messages