Skip to content

Commit 0a2e605

Browse files
committed
Apply readme code review suggestions
1 parent eea9943 commit 0a2e605

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -970,16 +970,14 @@ If you are familiar with [Eloquent Queries](http://laravel.com/docs/queries), th
970970
To see the available operations, check the [Eloquent](#eloquent) section.
971971

972972
Transactions
973-
-------
973+
------------
974974
Transactions require MongoDB version ^4.0 as well as deployment of replica set or sharded clusters. You can find more information [in the MongoDB docs](https://docs.mongodb.com/manual/core/transactions/)
975975

976976
### Basic Usage
977977

978-
Transaction supports all operations.
979-
980978
```php
981979
DB::transaction(function () {
982-
User::create(['name' => 'john', 'age' => 19, 'title' => 'admin', 'email' => 'klinsonup@gmail.com']);
980+
User::create(['name' => 'john', 'age' => 19, 'title' => 'admin', 'email' => 'john@example.com']);
983981
DB::collection('users')->where('name', 'john')->update(['age' => 20]);
984982
DB::collection('users')->where('name', 'john')->delete();
985983
});
@@ -988,24 +986,32 @@ DB::transaction(function () {
988986
```php
989987
// begin a transaction
990988
DB::beginTransaction();
991-
User::create(['name' => 'john', 'age' => 19, 'title' => 'admin', 'email' => 'klinsonup@gmail.com']);
989+
User::create(['name' => 'john', 'age' => 19, 'title' => 'admin', 'email' => 'john@example.com']);
992990
DB::collection('users')->where('name', 'john')->update(['age' => 20]);
993991
DB::collection('users')->where('name', 'john')->delete();
994992

995-
// you can commit your changes
993+
// commit changes
996994
DB::commit();
995+
```
996+
997+
To abort a transaction, call the `rollBack` method at any point during the transaction:
998+
```php
999+
DB::beginTransaction();
1000+
User::create(['name' => 'john', 'age' => 19, 'title' => 'admin', 'email' => '[email protected]']);
9971001

998-
// you can also rollback them
999-
//DB::rollBack();
1002+
// Abort the transaction, discarding any data created as part of it
1003+
DB::rollBack();
10001004
```
1005+
10011006
**NOTE:** Transactions in MongoDB cannot be nested. DB::beginTransaction() function will start new transactions in a new created or existing session and will raise the RuntimeException when transactions already exist. See more in MongoDB official docs [Transactions and Sessions](https://www.mongodb.com/docs/manual/core/transactions/#transactions-and-sessions)
10021007
```php
1003-
// This code will raise a RuntimeException
10041008
DB::beginTransaction();
1005-
User::create(['name' => 'john', 'age' => 20, 'title' => 'admin']);
1006-
DB::beginTransaction()
1007-
DB::collection('users')->where('name', 'john')->update(['age' => 20]);
1008-
DB::commit()
1009+
User::create(['name' => 'john', 'age' => 20, 'title' => 'admin']);
1010+
1011+
// This call to start a nested transaction will raise a RuntimeException
1012+
DB::beginTransaction();
1013+
DB::collection('users')->where('name', 'john')->update(['age' => 20]);
1014+
DB::commit();
10091015
DB::rollBack();
10101016
```
10111017

0 commit comments

Comments
 (0)