Rails的交易 [英] Rails transactions

查看:124
本文介绍了Rails的交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的ActiveRecord :: Base.transaction 。我想,回滚默认情况下不使用Rails 1.2.6和MySQL 5.0中工作。玩它一点点我发现自动提交未设置为0的MySQL连接。

I am trying to use ActiveRecord::Base.transaction. I figured that rollback doesn't work by default using Rails 1.2.6 and mysql 5.0. Playing with it a little bit more I found out that autocommit is not set to 0 in mysql connection.

问题:

1)如何在轨禁用自动提交的所有连接?

1) How do I disable autocommit in rails for all connections?

2),这会对其他code一定的负面影响,这并不一定要交易?

2) Will it have some negative impact on the other code that doesn't have to be transactional?

推荐答案

如果你有一个组合code,需要明确的交易和code,它可以依靠自动提交,也许你不应该关闭自动提交的所有连接。你是在正确的轨道想知道,这将影响到其他code。如果禁用自动提交,但其他code不知道它有承诺有它的工作承诺,那么这将是一个问题。提交的工作被回滚的连接关闭时。

If you have a mix of code that needs explicit transactions and code that can rely on autocommit, perhaps you shouldn't disable autocommit for all connections. You're on the right track wondering if this will impact other code. If you disable autocommit, but the other code doesn't know it has to commit to have its work committed, then that'll be a problem. Uncommitted work is rolled back when the connection closes.

您应该知道的默认存储引擎为MySQL是的MyISAM ,它不支持事务的。当您更改使用MyISAM数据表,这些更改将有效地立即提交,无论您的明确要求,开始和完成交易,且不论自动提交的状态。所以,你将无法回滚不管是什么,除非你使用的InnoDB创建的表存储引擎(或其他事务安全的存储引擎,如 BDB )。

You should be aware that the default storage engine for MySQL is MyISAM, which doesn't support transactions at all. When you make changes to a table that uses MyISAM, the changes are effectively committed immediately, regardless of your explicit requests to begin and finish transactions, and regardless of the state of autocommit. So you won't be able to roll back no matter what, unless you created your tables using the InnoDB storage engine (or other transaction-safe storage engines such as BDB).

这是不必要禁用自动提交模式使用事务在MySQL中。只要明确地开始一个事务。下面的SQL语句将成为交易的一部分,直到你提交或回滚该事务,无论自动提交的当前连接上的价值。

It's unnecessary to disable autocommit mode to use transactions in MySQL. Simply begin a transaction explicitly. The following SQL statements will be part of a transaction until you commit or rollback that transaction, regardless of the value of autocommit on your current connection.

<一个href="http://dev.mysql.com/doc/refman/5.0/en/commit.html">http://dev.mysql.com/doc/refman/5.0/en/commit.html说:

通过START TRANSACTION,自​​动提交   直到结束保持禁用   交易与COMMIT或ROLLBACK。   然后,自动提交模式恢复到   它的previous状态。

With START TRANSACTION, autocommit remains disabled until you end the transaction with COMMIT or ROLLBACK. The autocommit mode then reverts to its previous state.

这篇关于Rails的交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆