如何在 Firebird 中使用事务?
在 MS SQL Server 中,我可以轻松地将多个插入语句放入一个事务中,如下所示:
begin tran
insert into mytable values (1, 2, 3)
insert into mytable values (4, 5, 6)
commit tran
我试图在 Firebird 中做同样的事情,但我无法弄清楚语法。谷歌搜索“Firebird 事务语法”没有返回任何有用的信息。我已经足够了解事务支持存在,但没有关于如何正确使用它的示例。
所以我想我不妨在这里问一下。有谁知道如何使用 Firebird 数据库的多次插入来编写事务?
In MS SQL Server, I can easily put multiple insert statements into a transaction, like so:
begin tran
insert into mytable values (1, 2, 3)
insert into mytable values (4, 5, 6)
commit tran
I'm trying to do the same thing in Firebird, but I can't figure out the syntax. Googling for "Firebird transaction syntax" returns nothing useful. I've found enough to know that transaction support exists, but no examples for how to use it right.
So I figure I may as well ask on here. Does anyone know how to write a transaction using multiple inserts for a Firebird database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
补充@Allan的答案(顺便说一句,我投了赞成票),这里有一些更多信息。
当您在 SQL Server 中执行
begin tran
时,并不意味着您现在正在开始事务。由于您已连接到数据库,因此您已经处于事务状态!begin tran
真正做的是禁用“在每个语句处自动提交”,这是 SQL Server 中的默认状态(除非另有指定)。commit tran
分别提交连接并将其恢复为“在每个语句处自动提交”状态。在任何数据库中,当您连接时,您已经处于事务中。这就是数据库的样子。例如,在 Firebird 中,即使只运行查询,您也可以执行提交或回滚。
另一方面,某些数据库和连接库允许您使用“在每个语句处自动提交”连接状态,这就是 SQL Server 正在做的事情。尽管该功能可能很有用,但它并不是很有指导性,并且会导致初学者认为他们“不在事务中”。
Complementing @Allan's answer (which I upvoted, BTW), here's some more information.
When you do
begin tran
in SQL Server, it does not mean that you're starting the transaction now. You are already in transaction, since you are connected to the database! Whatbegin tran
really does is disable the "auto-commit at each statement", which is the default state in SQL Server (unless otherwise specified).Respectively,
commit tran
commits and reverts the connection to "auto-commit at each statement" state.In any database, when you are connected, you are already in transaction. This is how databases are. For instance, in Firebird, you can perform a commit or rollback even if only ran a query.
Some databases and connection libs, in the other hand, let you use the "auto-commit at each statement" state of connection, which is what SQL Server is doing. As useful as that feature might be, it's not very didactic and lead beginners to think they are "not in a transaction".
Firebird 始终使用事务。一旦您在数据库中进行更改,事务就会启动,并对该会话保持打开状态,直到您提交。使用您的代码,很简单:
Firebird always uses transactions. The transaction is started as soon as you make a change in the database and remains open for that session until you commit. Using your code, it's simply:
从 FB 2.5 开始,可以从当前事务内部开始一项新事务。
http://www.firebirdsql.org/rlsnotesh/rlsnotes25.html# rnfb25-psql-auton
Since FB 2.5 it's possible to start a new transaction from inside the current one.
http://www.firebirdsql.org/rlsnotesh/rlsnotes25.html#rnfb25-psql-auton
如果 Firebird 不需要额外的语法,下面
我提供了 Firebird 会话的屏幕截图,展示了它的工作原理。
If Firebird there is no need for extra syntax beyond
Below I provide screenshot from Firebird session showing how it works.