When using the managed transaction you should _never_ commit or rollback the transaction manually. If all queries are successful, but you still want to rollback the transaction (for example because of a validation failure) you should throw an error to break and reject the chain:
### Automatically pass transactions to all queries
In the examples above, the transaction is still manually passed, by passing `{ transaction: t }` as the second argument. To automatically pass the transaction to all queries you must install the [continuation local storage](https://github.com/othiym23/node-continuation-local-storage)(CLS) module and instantiate a namespace in your own code:
```js
...
...
@@ -67,11 +74,11 @@ CLS works like a thread-local storage for callbacks. What this means in practice
If you want to execute queries inside the callback without using the transaction you can pass `{ transaction: null }`, or another transaction if you have several concurrent ones:
# Concurrent/Partial transactions
You can have concurrent transactions within a sequence of queries or have some of them excluded from any transactions. Use the `{transaction: }` option to control which transaction a query belong to:
### Without CLS enabled
```js
sequelize.transaction(function(t1){
sequelize.transaction(function(t2){
// By default queries here will use t2
returnsequelize.transaction(function(t2){
// With CLS enable, queries here will by default use t2
// Pass in the `transaction` option to define/alter the transaction they belong to.
returnPromise.all([
User.create({name:'Bob'},{transaction:null}),
User.create({name:'Mallory'},{transaction:t1})
User.create({name:'Mallory'},{transaction:t1}),
User.create({name:'John'})// this would default to t2
]);
});
});
```
# Isolation levels
The possible isolations levels to use when starting a transaction:
By default, sequelize uses "REPEATABLE READ". If you want to use a different isolation level, pass in the desired level as the first argument:
```js
returnsequelize.transaction({
isolationLevel:Sequelize.Transaction.SERIALIZABLE
},function(t){
// your transactions
});
```
# Unmanaged transaction (then-callback)
Unmanaged transactions force you to manually rollback or commit the transaction. If you don't do that, the transaction will hang until it times out. To start an unmanaged transaction, call `sequelize.transaction()` without a callback (you can still pass an options object) and call `then` on the returned promise.