Safe Haskell | None |
---|---|
Language | Haskell2010 |
Arrow DSL for composition of transactions with automated conflict resolution.
Synopsis
- transact :: Transaction i o -> i -> Session o
- data Transaction i o
- statement :: Mode -> Level -> Statement i o -> Transaction i o
- sql :: Mode -> Level -> ByteString -> Transaction () ()
- session :: Mode -> Level -> (i -> Session o) -> Transaction i o
- condemn :: Transaction () ()
- retry :: Transaction i o
- data Mode
- data Level
Session
transact :: Transaction i o -> i -> Session o Source #
Execute an alternating transaction arrow providing an input for it.
Transaction
data Transaction i o Source #
Composable transaction providing for automated conflict resolution
with input i
and output o
. Supports alternative branching.
Mode and level is associated with the transaction, which makes them participate in composition. In a composed transaction they become the strictest of the ones associated with the transactions that constitute it. This allows you to safely compose transactions with different ACID guarantees.
Instances
statement :: Mode -> Level -> Statement i o -> Transaction i o Source #
Execute a single statement under a mode and level.
Warning: The statement must not be transaction-related
like BEGIN
, COMMIT
or ABORT
, otherwise you'll break the abstraction.
sql :: Mode -> Level -> ByteString -> Transaction () () Source #
Execute a possibly multistatement SQL string under a mode and level. SQL strings cannot be dynamically parameterized or produce a result.
Warning: SQL must not be transaction-related
like BEGIN
, COMMIT
or ABORT
, otherwise you'll break the abstraction.
session :: Mode -> Level -> (i -> Session o) -> Transaction i o Source #
Execute a composition of statements under the same mode and level.
Warning:
- You must know that it is possible to break the abstraction,
if you execute statements such as
BEGIN
inside of the session. - For the same reason you cannot execute other transactions inside of that session.
- You must beware that in case of conflicts any IO code that you may lift into session will get executed multiple times. This is the way the automatic conflict resolution works: the transaction gets retried, when a conflict arises. So be cautious about doing any mutations or rocket launches in that IO! Simply pinging for things such as current time is totally fine though. Still it's not recommended because it's often a symptom of bad application design.
Due to the mentioned it's highly advised to keep all the session code inside of the definition of a transaction. Thus you'll be guaranteed to have control over what's going on inside of the executed session and it will not be possible for this code to be affected by any outside changes or used elsewhere.
condemn :: Transaction () () Source #
Cause transaction to eventually roll back.
This allows to perform some transactional actions, collecting their results, and decide, whether to commit the introduced changes to the DB based on those results, as well as emit those results outside of the transaction.
retry :: Transaction i o Source #
Settings
Execution mode: either read or write.
Transaction isolation level.
For reference see the Postgres' documentation.
Reexports
FAQ
Why does transaction have to be an arrow, why is monad or applicative not enough?
Arrow allows us to determine the read mode and isolation level, while composing transactions in such a way that one can depend on the result of the other.
A monadic interface wouldn't allow us to do the first, namely: to compose the modes and levels.
An applicative interface wouldn't allow the second:
to make a transaction depend on the result of the other.
For details see the docs on the Transaction
type.