Download the PHP package lomocoin/php-mongo-transaction without Composer
On this page you can find all versions of the php package lomocoin/php-mongo-transaction. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lomocoin/php-mongo-transaction
More information about lomocoin/php-mongo-transaction
Files in lomocoin/php-mongo-transaction
Package php-mongo-transaction
Short Description Provide simple transaction feature for Mongodb
License MIT
Informations about the package php-mongo-transaction
PHP-Mongo-Transaction
Overview
Do you miss transaction in RDBMS while using MongoDB? You are not alone.
A news said that MongoDB will support ACID in the future, but what if we need it now?
Well, PHP-Mongo-Transaction
provides a simply basic transaction feature similar to RDBMS.
The flow is simple, just begin a transaction, do something, then commit or rollback.
The concept to achieve this is also simple: build a record collection in MongoDB to trace the modification of the data, and recover when rollback.
That is, once a transaction wants to rollback, it will:
- delete what has been inserted
- insert back what has been deleted (with the same ID)
- replace the modified data with the original copy (with the same ID)
To achieve this, this lib wraps the basic insertOne
, updateOne
, deleteOne
functions which provided by MongoDB Driver.
Have a look at #Usage
part of this document to see how easy to use it.
Limitation
-
We assume the database just works, so if there is a database failure, the transaction may not be rollback correctly and will be leaving as
ongoing
state. You may need to have a cron job to detect if everything works fine, and investigate manually when something goes wrong. If everything goes smoothly, the state of transactions should be eithercommit
orrollback
, except the realinit
andongoing
ones. - We can't handle concurrency issues at the current stage. It's too complicated to ensure data consist under the concurrent scenario. We recommend that you consider using a simple lock mechanism to avoid two transactions that may write to the same record happen at the same time, or adapting a message queue system to maintain the order of transaction execution.
Roadmap
- More unit tests
- Enhance docs
- Support
insertMany
,updateMany
,deleteMany
Notice
You can use Persistable
class with this library, but do not do any magic in the bsonSerialize
and bsonUnserialize
, e.g: auto update the "last modified date".
Any magic in these two functions will definitely destroy the valid data state
Install
Usage
1. Require autoload.php
file
2. Create transaction object
3. Make the change
3.1 insert
3.2 update
3.3 delete
4. Commit or Rollback
Contributors
License
Apache License 2.0