如何在Firebird中使用交易?

时间:2022-08-26 00:10:14

In MS SQL Server, I can easily put multiple insert statements into a transaction, like so:

在MS SQL Server中,我可以轻松地将多个insert语句放入事务中,如下所示:

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.

我试图在Firebird中做同样的事情,但我无法弄清楚语法。谷歌搜索“Firebird事务语法”返回没有任何用处。我已经发现足够知道存在事务支持,但没有关于如何正确使用它的示例。

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?

所以我想我也可以在这里问一下。有没有人知道如何使用Firebird数据库的多个插入来编写事务?

4 个解决方案

#1


6  

Complementing @Allan's answer (which I upvoted, BTW), here's some more information.

补充@ Allan的答案(我赞成,BTW),这里有更多信息。

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! What begin tran really does is disable the "auto-commit at each statement", which is the default state in SQL Server (unless otherwise specified).

当您在SQL Server中开始tran时,并不意味着您现在正在启动事务。您已经在事务中,因为您已连接到数据库! begin tran真正做的是禁用“每个语句的自动提交”,这是SQL Server中的默认状态(除非另有说明)。

Respectively, commit tran commits and reverts the connection to "auto-commit at each statement" state.

提交tran提交并将连接恢复为“在每个语句处自动提交”状态。

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.

在任何数据库中,当您连接时,您已经处于事务中。这就是数据库的方式。例如,在Firebird中,即使只运行查询,也可以执行提交或回滚。

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".

另一方面,某些数据库和连接库允许您使用“每个语句的自动提交”连接状态,这正是SQL Server正在执行的操作。尽管该功能可能很有用,但它并不是很有说服力,并且会让初学者认为他们“不在交易中”。

#2


5  

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:

Firebird总是使用事务。只要您在数据库中进行更改,该事务就会立即启动,并在您提交之前保持对该会话的打开状态。使用您的代码,它只是:

insert into mytable values (1, 2, 3);
insert into mytable values (4, 5, 6);
commit;

#3


3  

Since FB 2.5 it's possible to enter a transaction that differs from current one.

从FB 2.5开始,可以输入与当前交易不同的交易。

IN AUTONOMOUS TRANSACTION
DO
  < simple statement | compound statement >

http://www.firebirdsql.org/rlsnotesh/rlsnotes25.html#rnfb25-psql-auton

http://www.firebirdsql.org/rlsnotesh/rlsnotes25.html#rnfb25-psql-auton

#4


0  

If Firebird there is no need for extra syntax beyond

如果Firebird不需要额外的语法

commit; 

Below I provide screenshot from Firebird session showing how it works.

下面我提供Firebird会话的屏幕截图,展示它是如何工作的。

如何在Firebird中使用交易?

#1


6  

Complementing @Allan's answer (which I upvoted, BTW), here's some more information.

补充@ Allan的答案(我赞成,BTW),这里有更多信息。

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! What begin tran really does is disable the "auto-commit at each statement", which is the default state in SQL Server (unless otherwise specified).

当您在SQL Server中开始tran时,并不意味着您现在正在启动事务。您已经在事务中,因为您已连接到数据库! begin tran真正做的是禁用“每个语句的自动提交”,这是SQL Server中的默认状态(除非另有说明)。

Respectively, commit tran commits and reverts the connection to "auto-commit at each statement" state.

提交tran提交并将连接恢复为“在每个语句处自动提交”状态。

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.

在任何数据库中,当您连接时,您已经处于事务中。这就是数据库的方式。例如,在Firebird中,即使只运行查询,也可以执行提交或回滚。

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".

另一方面,某些数据库和连接库允许您使用“每个语句的自动提交”连接状态,这正是SQL Server正在执行的操作。尽管该功能可能很有用,但它并不是很有说服力,并且会让初学者认为他们“不在交易中”。

#2


5  

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:

Firebird总是使用事务。只要您在数据库中进行更改,该事务就会立即启动,并在您提交之前保持对该会话的打开状态。使用您的代码,它只是:

insert into mytable values (1, 2, 3);
insert into mytable values (4, 5, 6);
commit;

#3


3  

Since FB 2.5 it's possible to enter a transaction that differs from current one.

从FB 2.5开始,可以输入与当前交易不同的交易。

IN AUTONOMOUS TRANSACTION
DO
  < simple statement | compound statement >

http://www.firebirdsql.org/rlsnotesh/rlsnotes25.html#rnfb25-psql-auton

http://www.firebirdsql.org/rlsnotesh/rlsnotes25.html#rnfb25-psql-auton

#4


0  

If Firebird there is no need for extra syntax beyond

如果Firebird不需要额外的语法

commit; 

Below I provide screenshot from Firebird session showing how it works.

下面我提供Firebird会话的屏幕截图,展示它是如何工作的。

如何在Firebird中使用交易?