Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize distributed transaction based on reset in Laravel

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how Laravel implements distributed transactions based on reset". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how Laravel implements distributed transactions based on reset".

Quick Preview

Install the version between laravel5.5 and laravel8, and then install the Quick Service package

Composer require windawake/laravel-reset-transaction dev-master

First, create the ResetProductController.php controller, create the ResetProductModel.php model, and create two database tables, reset_transaction and reset_product. All these operations need to be done by executing the following command

Php artisan resetTransact:create-examples

Phpunit.xml adds testsuite Transaction

. . / vendor/windawake/laravel-reset-transaction/tests.

Finally, run the test command. / vendor/bin/phpunit-- testsuite=Transaction

The running results are shown below, and 5 examples pass the test.

Oot@DESKTOP-VQOELJ5:/web/linux/php/laravel/laravel62#. / vendor/bin/phpunit-- testsuite=TransactionPHPUnit 8.5.20 by Sebastian Bergmann and contributors. 5 / 5 Time: 219 ms, Memory: 22.00 MBOK (5 tests, 5 assertions) functional features

Out of the box, there is no need to reconstruct the code of the original project, which is consistent with the mysql transaction writing method, and is easy to use.

The service interface that supports http protocol. If you want to support other protocols, you need to rewrite the middleware.

Supports read committed, repeatable read, and synchronizes with mysql's transaction isolation level.

Principle analysis

After watching the "Edge of tomorrow" movie, you will know the operation of archiving and reading files. This distributed transaction component mimics the principle of the "Edge of tomorrow" movie, reading the file at the beginning of each request for the basic service, then continuing with the following operations, all operations are rolled back and archived at the end, and finally commit executes all the archives successfully. The whole process is to abide by the two-stage submission protocol, first prepare, and finally commit.

How to use

Take the vendor/windawake/laravel-reset-transaction/tests/TransactionTest.php file as an example

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report