Java Reference
In-Depth Information
you think your tests call all possible queries but your application still fails at runtime,
you'll have to edit the datastore-index.xml file and add these indexes manually.
Using Transactions
At a high level, the App Engine datastore supports transactions like most relational
databases. A transaction consists of one or more database operations that either
succeed or fail in entirety. If a transaction succeeds, then all operations are
committed to the datastore. However, if one of the operations fails, then all
operations are rolled back to their original state. An example method using
transactions is shown in Listing 7-3.
Listing 7-3. Sample transaction
import javax.jdo.Transaction;
public void createContact(Contact contact, String accountId) {
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
// start the transaction
tx.begin();
// persist the contact
pm.makePersistent(contact);
// fetch the parent account
Account account = pm.getObjectById(Account.class, accountId);
account.incrementContacts(1);
pm.makePersistent(account);
// commit if no errors
tx.commit();
} finally {
// roll back the transactions in case of an error
if (tx.isActive()) {
tx.rollback();
}
}
}
 
Search WWH ::




Custom Search