You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-user@db.apache.org by Steve Smith <ss...@vislab.usyd.edu.au> on 2003/08/13 02:25:18 UTC

Transactions: Is this valid

Hi,

I want to use transactions over a series of operations with the ability
to commit or rollback.  However, by default Torque appears to
auto-commit all save operations.  Looking at the code I've come up with
the following scheme to do this that appears to work, but I was hoping
if experts on list could confirm that I'm doing the right thing:

import org.apache.torque.Torque;
import org.apache.torque.Brokers;
import org.apache.torque.BrokersPeer;
import org.apache.torque.Referrer;
import org.apache.torque.ReferrerPeer;
import org.apache.torque.util.Transaction;
import java.sql.Connection;

class Test
{

    public Test() {
         try {
             Torque.init("Torque.properties");

            Connection conn = Transaction.begin(Torque.getDefaultDB());

            for (int i=10; i < 100; i++) {
                Brokers b = new Brokers();
                b.setName("Broker " + i);
                b.save(conn);
            }

	    // Whoops, changed my mind ...
            Transaction.rollback(conn);

	    // Do it correctly:
            conn = Transaction.begin(Torque.getDefaultDB());

            for (int i=1; i < 4; i++) {
                Brokers b = new Brokers();
                b.setName("Broker " + i);
                b.save(conn);
            }

            Transaction.commit(conn);

        } catch (Exception e) {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        }
    }

    public static void main(String args[])
    {
        new Test();
    }

}

Hopefully the another advantage to this scheme is that if Torque is
configured via JNDI to use XAConnections the above could be be part of a
wider transaction.  Anybody care to comment on this?

Cheers,
Steve 

-- 
Steve Smith			Phone: (02) 9351 5967
Vislab, University of Sydney	Email: ssmith@vislab.usyd.edu.au


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org