Is a function atomic in MySQL?

44 views Asked by At

I created test table as shown below:

CREATE TABLE test (
  num int
);

Then, I inserted the row whose num is 2 as shown below:

INSERT INTO test (num) VALUES (2);

Then, I created my_func() function which updates num to 5, then causes error by SIGNAL statement as shown below:

DELIMITER $$

CREATE FUNCTION my_func() RETURNS INT 
DETERMINISTIC
BEGIN
  UPDATE test SET num = 5;
  SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'An error occurred';
  RETURN NULL;
END$$ 

DELIMITER ;

Then, calling my_func() gets the error, then num was rollbacked to 2 as shown below:

mysql> SELECT my_func();
ERROR 1644 (45000): An error occurred
...
mysql> SELECT num FROM test;
+------+
| num  |
+------+
|    2 |
+------+

Actually, I created my_proc() procedure which updates num to 5, then causes error by SIGNAL statement as shown below:

DELIMITER $$

CREATE PROCEDURE my_proc() 
BEGIN
  UPDATE test SET num = 5;
  SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'An error occurred';
END$$ 

DELIMITER ;

Then, calling my_proc() gets the error but num is not rollbacked to 2 as shown below:

mysql> CALL my_proc();
ERROR 1644 (45000): An error occurred
...
mysql> SELECT num FROM test;
+------+
| num  |
+------+
|    5 |
+------+

So, is a function atomic in MySQL?

0

There are 0 answers