2011-07-14 5 views
0

フレームワークがトランザクションのネストを処理する方法について多くの悩みがありましたが、トランザクションが開始された場合に返されるmySql RDMSに対する実行可能なクエリがあるかどうかはわかりませんでした。それは可能ですか?おかげ接続がトランザクションを開始した場合、mySqlを照会する方法はありますか?

+0

'show full processlist;'何とか役立つかもしれません。 – Igor

+0

@Igor nop、私は次のようにします:show full processlist; START TRANSACTION; は完全なプロセスリストを表示します。 と変更なし –

答えて

0

でこれを行うための例を見つけることができません。

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) 

*/ 
関連する問題