单个“提交”没有“开始交易”的语句

发布于 2024-12-26 10:38:46 字数 693 浏览 2 评论 0原文

我正在使用 web2py dal 和 mysqldb 适配器来连接到 mysql 服务器。

我的问题:

  1. 之后需要一个“提交”而不需要引导“开始事务
  2. 为什么在“set autocommit=0”执行“选择” 如果 autocommit=0 则语句开始事务?

服务器信息: innodb引擎 自动提交=1(默认值) tx_isolation=可重复读取(默认值)

general_log:

100356 10:00:00 123456 Connect  [email protected] on dummydb
123456 Query    SET NAMES 'utf8'
123456 Query    SET AUTOCOMMIT = 0
123456 Query    COMMIT
123456 Query    SET FOREIGN_KEY_CHECKS=1
123456 Query    SET sql_mode='NO_BACKSLASH_ESCAPES'

i'm using web2py dal with mysqldb adapter to connect to mysql server.

my question:

  1. why does it need a single "commit" without leading "begin transaction" after "set autocommit=0"
  2. does "select" statement begin a transaction if autocommit=0?

server info:
innodb engine
autocommit=1 (default value)
tx_isolation=repeatable-read (default value)

general_log:

100356 10:00:00 123456 Connect  [email protected] on dummydb
123456 Query    SET NAMES 'utf8'
123456 Query    SET AUTOCOMMIT = 0
123456 Query    COMMIT
123456 Query    SET FOREIGN_KEY_CHECKS=1
123456 Query    SET sql_mode='NO_BACKSLASH_ESCAPES'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離殇 2025-01-02 10:38:46

呃...因为你禁用了自动提交???

这是一个很好的解释:

http://rpbouman.blogspot.com/2007/ 02/mysql-transactions-and-autocommit.html

启用自动提交后,每个语句都包含在自己的语句中
交易。语句的成功执行是隐式的
随后是 COMMIT,并且发生错误会中止
事务,回滚任何更改。

默认情况下,MySQL 中启用自动提交。

换句话说:

  1. “事务”不一定只是“将多个语句作为一个原子实体执行”

  2. 自动提交给你一个语句 == 1 个事务的“幻觉”

  3. 事实上,“automcommit off”给你“一个语句 ==” 0 个事务”

来自同一链接:

...
关闭自动提交的全部意义在于您可以发出
多个语句并一次提交它们。

Uh ... because you disabled auto-commit????

Here's a good explanation:

http://rpbouman.blogspot.com/2007/02/mysql-transactions-and-autocommit.html

With autocommit enabled, every statement is wrapped within its own
transaction. Successful execution of a statement is implicitly
followed by a COMMIT, and the occurrence of an error aborts the
transaction, rolling back any changes.

By default, autocommit is enabled in MySQL.

In other words:

  1. "transactions" aren't necessarily only about "executing multiple statements as one atomic entity"

  2. autocommit gives you the "illusion" of one statement == 1 transaction

  3. In fact, "automcommit off" gives you "one statement == 0 transactions"

From the same link:

...
The whole point of having autocommit off is that you can issue
multiple statements and commit them all at once.

娇纵 2025-01-02 10:38:46

在标准 SQL 中,您(几乎)总是处于事务内部。如果执行 COMMIT 或 ROLLBACK,则下一条语句将启动一个新事务。因此,如果您希望更改生效,则必须提交它们。

如果启用了 AutoCommit,则每个语句都是一个单例事务,如果成功则自动提交,如果失败则回滚。

当您关闭自动提交时,您必须提交以确保数据库更改生效。

一些 DBMS 在这个主题上有细微的变化。

特别是,Informix 有一种数据库模式,其中 AutoCommit 处于打开状态,直到您执行显式的 BEGIN [WORK];那么您就处于事务中,直到您执行 COMMIT [WORK] 或 ROLLBACK [WORK]。它还有一个“MODE ANSI”,其行为与标准 SQL 中相同;它有一个无日志模式,根本没有事务。

话虽如此,您所显示的陈述并不是不言而喻的,真正需要交易支持。它往往是 DML 语句(SELECT、INSERT、DELETE、UPDATE、MERGE 等),有时是需要事务支持的 DDL 语句。某些 DBMS 不允许回滚 DDL 语句(Oracle);其他人则这样做(Informix)。

In Standard SQL, you are (almost) always inside a transaction. If you do a COMMIT or ROLLBACK, the next statement starts a new transaction. Therefore, if you want changes to take effect, you have to COMMIT them.

If you have AutoCommit on, then each statement is a singleton transaction, and is automatically committed if it is successful, or rolled back if it fails.

When you turn AutoCommit off, you have to COMMIT to ensure database changes take effect.

Some DBMS have minor variations on this theme.

Informix, in particular, has one database mode where AutoCommit is on until you do an explicit BEGIN [WORK]; then you're in a transaction until you do COMMIT [WORK] or ROLLBACK [WORK]. It also has a 'MODE ANSI' which behaves as in standard SQL; and it has an unlogged mode where there are no transactions at all.

With all that said, the statements you show are not self-evidently ones that really need transactional support. It tends to be DML statements (SELECT, INSERT, DELETE, UPDATE, MERGE, etc) and sometimes DDL statements that need transactional support. Some DBMS do not allow DDL statements to be rolled back (Oracle); others do (Informix).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文