代码之家  ›  专栏  ›  技术社区  ›  Eduardo León

禁用activerecord中的事务

  •  1
  • Eduardo León  · 技术社区  · 16 年前

    如何在rails的activerecord中禁用事务?我有一个特殊的情况,我希望他们离开我似乎找不到任何有用的东西。有可能吗?

    3 回复  |  直到 10 年前
        1
  •  3
  •   Eduardo León    16 年前

    穷人的“无交易”

    # Force the loading of AR stuff
    ActiveRecord::Base.connection.execute('SELECT 1')
    
    # Remove transactions
    ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
      def begin_db_transaction
      end
    
      def commit_db_transaction
      end
    end
    
        2
  •  0
  •   Owen Kim    10 年前

    类似于丹尼尔的回答,但我发现我还必须禁用保存点才能正常工作。在钢轨3.2.22.2上进行试验。

    ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
      def begin_db_transaction
      end
    
      def commit_db_transaction
      end
    
      def create_savepoint
      end
    
      def rollback_to_savepoint
      end
    
      def release_savepoint
      end
    end
    
        3
  •  -3
  •   mcostanza    16 年前

    transaction是activerecord::base上的一个类方法,因此您可以这样做:

    Model.transaction do
      ...
    end
    

    或者如果您希望不使用特定型号:

    ActiveRecord::Base.transaction do
      ...
    end
    

    它还可能取决于您使用的数据库,我知道这在mysql上是有效的,但对其他数据库则不确定。

    推荐文章