一台服务器上跨多个InnoDB数据库的MySQL事务

一台服务器上跨多个InnoDB数据库的MySQL事务,mysql,database,transactions,innodb,Mysql,Database,Transactions,Innodb,我有一些关于MySQL InnoDB引擎事务的快速问题。我有一些这种格式的数据库(db_1、db_2、db_3等) 我想插入/更新事务中的任何数据库,如下所示 BEGIN USE DB_1; //Insert / Update into DB_1 tables USE DB_2; //Insert / Update into DB_2 tables COMMIT BEGIN USE DB_1; //Insert / Update into DB_1 tables COMMIT BEGIN

我有一些关于MySQL InnoDB引擎事务的快速问题。我有一些这种格式的数据库(db_1、db_2、db_3等)

我想插入/更新事务中的任何数据库,如下所示

BEGIN
USE DB_1;
//Insert / Update into DB_1 tables

USE DB_2;
//Insert / Update into DB_2 tables
COMMIT
BEGIN
USE DB_1;
//Insert / Update into DB_1 tables
COMMIT

BEGIN
USE DB_2;
//Insert / Update into DB_2 tables
COMMIT
可以吗?或者我需要为每个数据库设置单独的事务,如下所示

BEGIN
USE DB_1;
//Insert / Update into DB_1 tables

USE DB_2;
//Insert / Update into DB_2 tables
COMMIT
BEGIN
USE DB_1;
//Insert / Update into DB_1 tables
COMMIT

BEGIN
USE DB_2;
//Insert / Update into DB_2 tables
COMMIT

在第一种方法中,在单个事务中切换多个数据库是否存在任何问题。事务数据库级别还是全局级别?

应该可以。显然
DB_1
DB_2
在同一台服务器上。如果它们将在不同的服务器上,那么集群将是您的未来!:-)

您可以考虑对表的名称进行限定,而不是使用<代码>使用< /COD>语句。这可能会简化您的逻辑

比如说,

  BEGIN;
  INSERT INTO DB_1.mytable (myid, mycol) VALUES (1,'a');
  INSERT INTO DB_2.histable (hisid, hiscol) VALUES (1,'b');
  SELECT whatever
    FROM DB_1.mytable AS m
    JOIN DB_2.histable AS h ON m.myid = h.hisid;
  COMMIT;

对不起,我迟了回复。正如您所说,在我的例子中,所有数据库都在同一台服务器中。谢谢你提供的信息。@Prabu这对你有用吗?使用一个RDBMS的两个数据库是否真的是事务性的?