2017-04-16 15 views
0

私は生成されたプロシージャを呼び出すトランザクションを持っていますが、プロシージャの後でエラーが発生した場合、手順は次のとおりです。MySQL:トランザクション内のプロシージャコールによってコミットが発生します

DELIMITER $$  
CREATE PROCEDURE location_processor(in _city varchar(20), in _country_code varchar(2), out id int) 
    begin 
     select location_id into id from location where city = _city and country_code = _country_code limit 0,1; 
     if id is null then 
      select @id := max(location_id) from location; 
      if @id is null then 
       set @id = 0; 
      end if; 
      set @id = @id + 1; 
      insert into location (location_id, city, country_code) 
      values(@id, _city, _country_code); 
      set id = @id; 
     end if; 
    end; $$ 
DELIMITER ; 

注:この手順では、開始/終了トランザクションの構文は使用されません。私はそれが始まり、手順自体の端信じる理由を有するがコミット引き起こしているとして:

注:すべての格納されたプログラム内

(ストアド・プロシージャおよび関数、トリガ、およびイベント)、パーサ扱いBEGIN [WORK]をBEGIN ... ENDブロックの先頭にします。代わりにSTART TRANSACTIONを使用してこのコンテキストでトランザクションを開始してください。

https://dev.mysql.com/doc/refman/5.7/en/commit.html

私は、エラーチェックの目的のために、この手順が必要になります。そこにプロシージャを使用している間にトランザクション内でコミットすることを避けるためにとにかくありますか?

答えて

0

簡単な例では、私は問題を再現することはできません。

mysql> DROP PROCEDURE IF EXISTS `location_processor`; 
Query OK, 0 rows affected (0.00 sec) 

mysql> DROP TABLE IF EXISTS `location`; 
Query OK, 0 rows affected (0.00 sec) 

mysql> CREATE TABLE IF NOT EXISTS `location` (
    ->  `location_id` INT, 
    ->  `city` VARCHAR(255), 
    ->  `country_code` VARCHAR(255) 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> DELIMITER // 

mysql> CREATE PROCEDURE `location_processor`() 
    -> BEGIN 
    ->  INSERT INTO `location` 
    ->   (`location_id`, `city`, `country_code`) 
    ->  VALUES 
    ->   (2, 'city', 'country_code'); 
    -> END// 
Query OK, 0 rows affected (0.00 sec) 

mysql> DELIMITER ; 

mysql> START TRANSACTION; 
Query OK, 0 rows affected (0.00 sec) 

mysql> SELECT `location_id`, `city`, `country_code` 
    -> FROM `location`; 
Empty set (0.00 sec) 

mysql> INSERT INTO `location` 
    ->  (`location_id`, `city`, `country_code`) 
    -> VALUES 
    ->  (1, 'city', 'country_code'); 
Query OK, 1 row affected (0.00 sec) 

mysql> CALL `location_processor`; 
Query OK, 1 row affected (0.00 sec) 

mysql> INSERT INTO `location` 
    ->  (`location_id`, `city`, `country_code`) 
    -> VALUES 
    ->  (3, 'city', 'country_code'); 
Query OK, 1 row affected (0.00 sec) 

mysql> SELECT `location_id`, `city`, `country_code` 
    -> FROM `location`; 
+-------------+------+--------------+ 
| location_id | city | country_code | 
+-------------+------+--------------+ 
|   1 | city | country_code | 
|   2 | city | country_code | 
|   3 | city | country_code | 
+-------------+------+--------------+ 
3 rows in set (0.00 sec) 

mysql> ROLLBACK; 
Query OK, 0 rows affected (0.00 sec) 

mysql> SELECT `location_id`, `city`, `country_code` 
    -> FROM `location`; 
Empty set (0.00 sec) 
関連する問題