db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Steve Smith <ssm...@vislab.usyd.edu.au>
Subject Transactions: Is this valid
Date Wed, 13 Aug 2003 00:25:18 GMT
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


Mime
View raw message