首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果我的连接已经启动了一个事务,有没有办法查询mySql?

如果我的连接已经启动了一个事务,有没有办法查询mySql?
EN

Stack Overflow用户
提问于 2011-07-15 06:13:49
回答 2查看 66关注 0票数 0

我找到了许多关于框架如何处理事务嵌套的分析器,但我找不到是否存在针对mySql RDMS的可执行查询,该查询将返回事务是否已启动。这是可能的吗?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-15 06:35:13

这有点棘手,但您可以在http://forge.mysql.com/tools/tool.php?id=145上找到一个如何做到这一点的示例

票数 2
EN

Stack Overflow用户

发布于 2012-08-14 02:16:43

我在这里复制@wonk0链接,因为它不再是可避免的。

代码语言:javascript
复制
DELIMITER $$

CREATE PROCEDURE `myexample` (
  OUT errno    INT,
  OUT error    VARCHAR(255)
)
BEGIN

  DECLARE need_to_commit BOOL DEFAULT FALSE;

  main:BEGIN

    -- for example, catch duplicate key errors and roll back
    DECLARE EXIT HANDLER FOR 1062
    BEGIN
      ROLLBACK TO SAVEPOINT myexample;
      SET errno = 1060,
          error = 'Duplicate key.';
    END;

    -- catch any other errors that should cause automatic rollbacks

    -- ------
    -- set up the savepoint / trx handler
    -- 

    DECLARE CONTINUE HANDLER FOR 1305
    BEGIN
      START TRANSACTION;
      SET need_to_commit = TRUE;
    END;

    -- this will have no effect if we are not in a trx
    SAVEPOINT myexample;

    -- this will error if we are not in a trx, be caught above, and start a trx
    -- it will do nothing if we are already in a trx
    RELEASE SAVEPOINT myexample;

    -- this will always set a savepoint
    -- because we are now guaranteed to be in a trx
    SAVEPOINT myexample;

    -- 
    -- done setting up savepoint / trx
    -- ------

    -- initialize the OUT parameters
    SET errno = 0,
        error = '';

    -- do some stuff
    INSERT INTO mytable VALUES (1);
    INSERT INTO yourtable VALUES (2);

    -- you can even handle your own errors (without handlers!)
    IF ( 0 != 1 ) THEN
      ROLLBACK TO SAVEPOINT myexample;
      SET errno = 1234,
          error = 'Zero is not one!';
      LEAVE main;
    END IF;

  END; -- main

  -- if we were not in a transaction to start with
  -- we should not leave one dangling, so commit here
  IF need_to_commit THEN 
    COMMIT;
  END IF;

END $$


DELIMITER ;



/*

EXAMPLE

mysql> create table mytable (a int primary key) engine=innodb;
Query OK, 0 rows affected (0.07 sec)

mysql> create table yourtable (b int primary key) engine=innodb;
Query OK, 0 rows affected (0.03 sec)

mysql> call myexample(@e,@r); select @e,@r;
Query OK, 0 rows affected (0.00 sec)

+------+------------------+
| @e   | @r               |
+------+------------------+
| 1234 | Zero is not one! |
+------+------------------+
1 row in set (0.00 sec)

mysql> select * from mytable union select * from yourtable;
Empty set (0.00 sec)

mysql> insert into yourtable values (2);
Query OK, 1 row affected (0.00 sec)

mysql> call myexample(@e,@r); select @e,@r;
Query OK, 0 rows affected (0.00 sec)

+------+----------------+
| @e   | @r             |
+------+----------------+
| 1060 | Duplicate key. |
+------+----------------+
1 row in set (0.00 sec)

mysql> select * from mytable union select * from yourtable;
+---+
| a |
+---+
| 2 |
+---+
1 row in set (0.00 sec)

*/
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6700561

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档