博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

不同数据库中的事物处理

Posted on 2009-12-10 22:34  peswe  阅读(205)  评论(0编辑  收藏  举报

def self.test_actiondef self.test_action
    transaction do
      begin
        Project.connection.begin_db_transaction
        InfoProject.connection.begin_db_transaction
        City.update_all(["name_cn=?", 'dd'], ["id=?", 10003])
        ActiveRecord::Base.connection.execute("update cities@pl set name_cn = 'dd' where id = 10003")
        raise "aa"
        City.update_all(["name_cn=?", 'aa'], ["id=?", 10002])
        InfoCity.update_all(["name_cn=?", 'bb'], ["id=?", 10002])


      rescue => e
        puts e
        Project.connection.rollback_db_transaction
        InfoProject.connection.rollback_db_transaction
      end
    end
  end


begin
  Project.connection.begin_db_transaction
  InfoProject.connection.begin_db_transaction
  City.update_all(["name_cn=?", 'aa'], ["id=?", 1])
  InfoCity.update_all(["name_cn=?", 'bb'], ["id=?", 1])
  raise "aa"
  City.update_all(["name_cn=?", 'aa'], ["id=?", 2])
  InfoCity.update_all(["name_cn=?", 'bb'], ["id=?", 2])
  InfoProject.connection.commit_db_transaction
  Project.connection.commit_db_transaction
rescue => e
  puts e
  InfoProject.connection.rollback_db_transaction
  Project.connection.rollback_db_transaction
end