ruby-****@sourc*****
ruby-****@sourc*****
2003年 9月 22日 (月) 07:20:41 JST
------------------------- REMOTE_ADDR = 217.117.55.140 REMOTE_HOST = URL = http://ruby-gnome2.sourceforge.jp/?tut-libgda-transactions ------------------------- = Managing transactions The special methods we need to do this are defined in Gda::Transaction, Gda::Connection and Gda::Command classes, and they are: * Gda::Transaction.new; * Gda::Connection#begin_transaction; * Gda::Connection#commit_transaction; * Gda::Connection#rollback_transaction; * Gda::Command#set_transaction or Gda::Command#transaction=. Things you have to do to manage transactions are: (1) Create transaction (2) Change, if needed, the isolation level (3) Link transaction to a connection (4) For each command you want to execute: (1) Create command (2) Link transaction to command (3) Execute command (5) Commit or rollback transaction Here you can see an example: def process_accounts(conn) # Creates first transaction. transaction_one = Gda::Transaction.new("accounts1") # Changes the isolation level. transaction_one.isolation_level = Gda::Transaction::ISOLATION_SERIALIZABLE # Links it to connection. conn.begin_transaction(transaction_one) command = Gda::Command.new("UPDATE accounts SET balance=balance+50 WHERE account_code=456", Gda::Command::TYPE_SQL, Gda::Command::OPTION_STOP_ON_ERRORS) # Links command to transaction. command.transaction = transaction_one conn.execute_non_query(command) command = Gda::Command.new("UPDATE accounts SET balance=balance-50 WHERE account_code=12", Gda::Command::TYPE_SQL, Gda::Command::OPTION_STOP_ON_ERRORS) command.transaction = transaction_one conn.execute_non_query(command) # Makes commit on transaction. conn.commit_transaction(transaction_one) transaction_two = Gda::Transaction.new("accounts2") transaction_two.isolation_level = Gda::Transaction::ISOLATION_SERIALIZABLE conn.begin_transaction(transaction_two) command = Gda::Command.new("UPDATE accounts SET balance=balance+400 WHERE account_code=456", Gda::Command::TYPE_SQL, Gda::Command::OPTION_STOP_ON_ERRORS) command.transaction = transaction_two conn.execute_non_query(command) command = Gda::Command.new("UPDATE accounts SET balance=balance-400 WHERE account_code=12", Gda::Command::TYPE_SQL, Gda::Command::OPTION_STOP_ON_ERRORS) command.transaction = transaction_two conn.execute_non_query(command) # Makes rollback on second transaction. conn.rollback_transaction(transaction_two) execute_sql_command(conn, "SELECT * FROM accounts") end