Transactions

In Node.js, Nymph supports nested transactions. Nothing will be permanently written to the database's storage until the highest level transaction is committed. When a nested transaction is rolled back, the database will be in the state where the transaction above it started that nested transaction.

When you start a transaction, you give it a name, then you can commit or roll back that transaction, by name. This means if you're doing transactions recursively, you should use a counter in your name. Nymph uses internal transactions that start with the "nymph-" prefix, so don't use that. You should be sure to commit or rollback transactions in the reverse order that they were started, or you might run into trouble.

Nymph has the following methods for managing transactions.

  • startTransaction - Start a named transaction.
  • commit - Commit a named transaction.
  • rollback - Rollback a named transaction.
  • inTransaction - Check if Nymph is currently running within a transaction.

startTransaction will return a new instance of Nymph. For the rest of the transaction, until it is committed or rolled back, you should use this instance. It will be tied to a specific connection to the database.

When you start a new transaction, entities retrieved from that transaction's Nymph instance will have that instance within their static nymph property and instance $nymph property. As long as the entity code always uses these instances, every query and change should occur within that transaction.

Not all databases that Nymph supports (namely, MySQL with an engine that doesn't support transactions, like the MyISAM engine) can use transactions. To ensure data consistency, it's highly recommended to use a configuration that supports transactions.