I'm working on a web project with PHP and MYSQL using InnoDB engine.
I have some important things to do within a transaction, and if any of those things fail, there must be a rollback. What I'm doing is throw an exception for every thing that goes wrong, and do the rollback inside catch block. The problem is that there can be an exception before "start transaction", so in that case the "rollback" will be executed without any transaction. It has nothing to rollback, I'm just asking myself if there is any penalty in doing a rollback for nothing. MYSQL ignores it without any consequence?
If yes then it's good because I can avoid making some more complicated logic that sends a rollback only if a transaction is started.
Thanks!
Easy to check
# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 652
Server version: 5.7.29-0ubuntu0.18.04.1-log (Ubuntu)
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> rollback;
Query OK, 0 rows affected (0.00 sec)
mysql>
No, there is no penalty.