Question

Assume I'm doing the following steps in a transaction:

  • read some data A from the database
  • do some calculation based on it
  • write some data B to the database

Is it possible to make this transaction fail if A has changed in the mean time?

In short: How do I achieve isolation in the persistent package?

Était-ce utile?

La solution

I haven't used persistent myself, but the Yesod book mentions that

One important thing to note is that everything which occurs inside a single call to runSqlConn runs in a single transaction. This has two important implications:

  • For many databases, committing a transaction can be a costly activity. By putting multiple steps into a single transaction, you can speed up code dramatically.

  • If an exception is thrown anywhere inside a single call to runSqlConn, all actions will be rolled back (assuming your backend has rollback support).

Whether this gives you isolation guarantees probably depends on whether the given back-end gives isolation guarantees for transactions.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top