Laravel Eloquent ORM Transactions

后端 未结 7 2198
闹比i
闹比i 2020-12-22 17:30

The Eloquent ORM is quite nice, though I\'m wondering if there is an easy way to setup MySQL transactions using innoDB in the same fashion as PDO, or if I would have to exte

7条回答
  •  渐次进展
    2020-12-22 18:00

    For some reason it is quite difficult to find this information anywhere, so I decided to post it here, as my issue, while related to Eloquent transactions, was exactly changing this.

    After reading THIS stackoverflow answer, I realized my database tables were using MyISAM instead of InnoDB.

    For transactions to work on Laravel (or anywhere else as it seems), it is required that your tables are set to use InnoDB

    Why?

    Quoting MySQL Transactions and Atomic Operations docs (here):

    MySQL Server (version 3.23-max and all versions 4.0 and above) supports transactions with the InnoDB and BDB transactional storage engines. InnoDB provides full ACID compliance. See Chapter 14, Storage Engines. For information about InnoDB differences from standard SQL with regard to treatment of transaction errors, see Section 14.2.11, “InnoDB Error Handling”.

    The other nontransactional storage engines in MySQL Server (such as MyISAM) follow a different paradigm for data integrity called “atomic operations.” In transactional terms, MyISAM tables effectively always operate in autocommit = 1 mode. Atomic operations often offer comparable integrity with higher performance.

    Because MySQL Server supports both paradigms, you can decide whether your applications are best served by the speed of atomic operations or the use of transactional features. This choice can be made on a per-table basis.

提交回复
热议问题