views:

44

answers:

2

According to the App Engine docs, you wrap a datstore event in a transaction like this:

import javax.jdo.Transaction;

import ClubMembers;   // not shown

// ...
        // PersistenceManager pm = ...;

        Transaction tx = pm.currentTransaction();

        try {
            tx.begin();

            ClubMembers members = pm.getObjectById(ClubMembers.class, "k12345");
            members.incrementCounterBy(1);
            pm.makePersistent(members);

            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
        }

Would it negatively impact the performance of the application if you wrapped not only every write event in a transaction but every read event also?

+2  A: 

There is certainly overhead to performing reads and writes in transactions.

It doesn't make sense in a lot of cases - if you're simply reading a value from a datastore, then wrapping a single read operation up in a transaction has no advantages. Save transactions for when you have a set of multiple operations which need to be performed together (like the get, increment, persist example you included in your question). The GAE documentation includes a section on uses for transactions which you might find helpful.

Also, it is often handy to batch datastore operations to minimize the number of round-trips between your application and the datastore. If you wrap every operation in a transaction, then you will have to send each transaction individually (which will be quite a bit slower, especially if you make the each request synchronously).

David Underhill
+1  A: 

Since declaring a transaction is more work, it must incur an overhead. That being said, I have never had a time when unnecessary transactions were the cause of my performance issues.

Tony Ennis