hasql-transaction-0.10: Composable abstraction over retryable transactions for Hasql

Safe HaskellNone
LanguageHaskell2010

Hasql.Transaction

Contents

Description

DSL for composition of transactions with automated conflict resolution.

Synopsis

Sessions

transact :: Transaction a -> Session a Source #

Execute transaction in session.

Transaction

data Transaction a Source #

Composable transaction providing for automated conflict resolution.

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.

It cannot have an instance of Monad, because it makes it impossible to implement composition of mode and level associated with consituent transactions. It still however is possible to compose transactions in such a way that the result of a transaction is used to decide which transaction to execute next, thanks to the recently discovered Selective interface.

Supports alternative branching, where the alternative gets executed in case of a transaction conflict.

Instances
Functor Transaction Source # 
Instance details

Defined in Hasql.Transaction

Methods

fmap :: (a -> b) -> Transaction a -> Transaction b #

(<$) :: a -> Transaction b -> Transaction a #

Applicative Transaction Source # 
Instance details

Defined in Hasql.Transaction

Methods

pure :: a -> Transaction a #

(<*>) :: Transaction (a -> b) -> Transaction a -> Transaction b #

liftA2 :: (a -> b -> c) -> Transaction a -> Transaction b -> Transaction c #

(*>) :: Transaction a -> Transaction b -> Transaction b #

(<*) :: Transaction a -> Transaction b -> Transaction a #

Selective Transaction Source # 
Instance details

Defined in Hasql.Transaction

Methods

select :: Transaction (Either a b) -> Transaction (a -> b) -> Transaction b #

Alt Transaction Source # 
Instance details

Defined in Hasql.Transaction

statement :: Mode -> Level -> Statement i o -> i -> Transaction o Source #

Execute a single statement under 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 -> Session a -> Transaction a Source #

Execute a composition of statements under the same mode and level.

Warning:

  1. You must know that it is possible to break the abstraction, if you execute statements such as BEGIN inside of the session.
  2. For the same reason you cannot execute other transactions inside of that session.
  3. 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.

restrict :: Level -> Transaction a -> Transaction a Source #

Restrict the transaction isolation level.

Will pick up the strictest level among the one you've specified and the one associated with the updated transaction.

Settings

data Mode Source #

Execution mode: either read or write.

Constructors

Read

Read-only. No writes possible.

Write

Write and commit.

Instances
Bounded Mode Source # 
Instance details

Defined in Hasql.Transaction.Types

Enum Mode Source # 
Instance details

Defined in Hasql.Transaction.Types

Methods

succ :: Mode -> Mode #

pred :: Mode -> Mode #

toEnum :: Int -> Mode #

fromEnum :: Mode -> Int #

enumFrom :: Mode -> [Mode] #

enumFromThen :: Mode -> Mode -> [Mode] #

enumFromTo :: Mode -> Mode -> [Mode] #

enumFromThenTo :: Mode -> Mode -> Mode -> [Mode] #

Eq Mode Source # 
Instance details

Defined in Hasql.Transaction.Types

Methods

(==) :: Mode -> Mode -> Bool #

(/=) :: Mode -> Mode -> Bool #

Ord Mode Source # 
Instance details

Defined in Hasql.Transaction.Types

Methods

compare :: Mode -> Mode -> Ordering #

(<) :: Mode -> Mode -> Bool #

(<=) :: Mode -> Mode -> Bool #

(>) :: Mode -> Mode -> Bool #

(>=) :: Mode -> Mode -> Bool #

max :: Mode -> Mode -> Mode #

min :: Mode -> Mode -> Mode #

Show Mode Source # 
Instance details

Defined in Hasql.Transaction.Types

Methods

showsPrec :: Int -> Mode -> ShowS #

show :: Mode -> String #

showList :: [Mode] -> ShowS #

data Level Source #

Transaction isolation level.

For reference see the Postgres' documentation.

Instances
Bounded Level Source # 
Instance details

Defined in Hasql.Transaction.Types

Enum Level Source # 
Instance details

Defined in Hasql.Transaction.Types

Eq Level Source # 
Instance details

Defined in Hasql.Transaction.Types

Methods

(==) :: Level -> Level -> Bool #

(/=) :: Level -> Level -> Bool #

Ord Level Source # 
Instance details

Defined in Hasql.Transaction.Types

Methods

compare :: Level -> Level -> Ordering #

(<) :: Level -> Level -> Bool #

(<=) :: Level -> Level -> Bool #

(>) :: Level -> Level -> Bool #

(>=) :: Level -> Level -> Bool #

max :: Level -> Level -> Level #

min :: Level -> Level -> Level #

Show Level Source # 
Instance details

Defined in Hasql.Transaction.Types

Methods

showsPrec :: Int -> Level -> ShowS #

show :: Level -> String #

showList :: [Level] -> ShowS #

Reexports

This module also reexports the following types for you to require lesser imports: