postgresql-simple-0.7.0.1: Mid-Level PostgreSQL client library

Copyright | (c) 2011-2013 Leon P Smith (c) 2013 Joey Adams
License | BSD3
Maintainer | Leon P Smith <leon@melding-monads.com>
Safe Haskell | None
Language | Haskell2010

Database.PostgreSQL.Simple.Transaction

Contents

- Transaction handling
- Savepoint
- Error predicates

Description

Synopsis

- withTransaction :: Connection -> IO a -> IO a
- withTransactionLevel :: IsolationLevel -> Connection -> IO a -> IO a
- withTransactionMode :: TransactionMode -> Connection -> IO a -> IO a
- withTransactionModeRetry :: TransactionMode -> (SqlError -> Bool) -> Connection -> IO a -> IO a
- withTransactionModeRetry' :: forall a e. Exception e => TransactionMode -> (e -> Bool) -> Connection -> IO a -> IO a
- withTransactionSerializable :: Connection -> IO a -> IO a
- data TransactionMode = TransactionMode {
  - isolationLevel :: !IsolationLevel
  - readWriteMode :: !ReadWriteMode
  }
- data IsolationLevel
  - = DefaultIsolationLevel
  - | ReadCommitted
  - | RepeatableRead
  - | Serializable
- data ReadWriteMode
  - = DefaultReadWriteMode
  - | ReadWrite
  - | ReadOnly
- defaultTransactionMode :: TransactionMode
- defaultIsolationLevel :: IsolationLevel
- defaultReadWriteMode :: ReadWriteMode
- begin :: Connection -> IO ()
- beginLevel :: IsolationLevel -> Connection -> IO ()
- beginMode :: TransactionMode -> Connection -> IO ()
- commit :: Connection -> IO ()
- rollback :: Connection -> IO ()
- withSavepoint :: Connection -> IO a -> IO a
- data Savepoint
- newSavepoint :: Connection -> IO Savepoint
- releaseSavepoint :: Connection -> Savepoint -> IO ()
- rollbackToSavepoint :: Connection -> Savepoint -> IO ()
- rollbackToAndReleaseSavepoint :: Connection -> Savepoint -> IO ()
- isSerializationError :: SqlError -> Bool
- isNoActiveTransactionError :: SqlError -> Bool
- isFailedTransactionError :: SqlError -> Bool

# Transaction handling

withTransaction :: Connection -> IO a -> IO a Source #

Execute an action inside a SQL transaction.

This function initiates a transaction with a "`begin transaction`" statement, then executes the supplied action. If the action succeeds, the transaction will be completed with `commit` before this function returns.

If the action throws any kind of exception (not just a PostgreSQL-related exception), the transaction will be rolled back using `rollback`, then the exception will be rethrown.

For nesting transactions, see `withSavepoint`.

withTransactionLevel :: IsolationLevel -> Connection -> IO a -> IO a Source #

Execute an action inside a SQL transaction with a given isolation level.

withTransactionMode :: TransactionMode -> Connection -> IO a -> IO a Source #

Execute an action inside a SQL transaction with a given transaction mode.

withTransactionModeRetry :: TransactionMode -> (SqlError -> Bool) -> Connection -> IO a -> IO a Source #

`withTransactionModeRetry'` but with the exception type pinned to `SqlError`.

withTransactionModeRetry' :: forall a e. Exception e => TransactionMode -> (e -> Bool) -> Connection -> IO a -> IO a Source #

Like `withTransactionMode`, but also takes a custom callback to determine if a transaction should be retried if an exception occurs. If the callback returns `True`, then the transaction will be retried. If the callback returns `False`, or an exception other than an `e` occurs then the transaction will be rolled back and the exception rethrown.

This is used to implement `withTransactionSerializable`.

withTransactionSerializable :: Connection -> IO a -> IO a Source #

Execute an action inside of a `Serializable` transaction. If a serialization failure occurs, roll back the transaction and try again. Be warned that this may execute the IO action multiple times.

A `Serializable` transaction creates the illusion that your program has exclusive access to the database. This means that, even in a concurrent setting, you can perform queries in sequence without having to worry about what might happen between one statement and the next.

Think of it as STM, but without `retry`.

data TransactionMode Source #

Constructors

TransactionMode |

Fields

- isolationLevel :: !IsolationLevel
- readWriteMode :: !ReadWriteMode

#### Instances

Instances details

Show TransactionMode Source # |
Instance details Defined in Database.PostgreSQL.Simple.Transaction Methods showsPrec :: Int -> TransactionMode -> ShowS Source # show :: TransactionMode -> String Source # showList :: [TransactionMode] -> ShowS Source #
Eq TransactionMode Source # |
Instance details Defined in Database.PostgreSQL.Simple.Transaction Methods (==) :: TransactionMode -> TransactionMode -> Bool Source # (/=) :: TransactionMode -> TransactionMode -> Bool Source #

data IsolationLevel Source #

Of the four isolation levels defined by the SQL standard, these are the three levels distinguished by PostgreSQL as of version 9.0. See https://www.postgresql.org/docs/9.5/static/transaction-iso.html for more information. Note that prior to PostgreSQL 9.0, `RepeatableRead` was equivalent to `Serializable`.

Constructors

DefaultIsolationLevel | the isolation level will be taken from PostgreSQL's per-connection `default_transaction_isolation` variable, which is initialized according to the server's config. The default configuration is `ReadCommitted`.
ReadCommitted |
RepeatableRead |
Serializable |

#### Instances

Instances details

Bounded IsolationLevel Source # |
Instance details Defined in Database.PostgreSQL.Simple.Transaction Methods minBound :: IsolationLevel Source # maxBound :: IsolationLevel Source #
Enum IsolationLevel Source # |

Instance details

Defined in Database.PostgreSQL.Simple.Transaction

Methods

succ :: IsolationLevel -> IsolationLevel Source #

pred :: IsolationLevel -> IsolationLevel Source #

toEnum :: Int -> IsolationLevel Source #

fromEnum :: IsolationLevel -> Int Source #

enumFrom :: IsolationLevel -> [IsolationLevel] Source #

enumFromThen :: IsolationLevel -> IsolationLevel -> [IsolationLevel] Source #

enumFromTo :: IsolationLevel -> IsolationLevel -> [IsolationLevel] Source #

enumFromThenTo :: IsolationLevel -> IsolationLevel -> IsolationLevel -> [IsolationLevel] Source #

Show IsolationLevel Source # |
Instance details Defined in Database.PostgreSQL.Simple.Transaction Methods showsPrec :: Int -> IsolationLevel -> ShowS Source # show :: IsolationLevel -> String Source # showList :: [IsolationLevel] -> ShowS Source #
Eq IsolationLevel Source # |
Instance details Defined in Database.PostgreSQL.Simple.Transaction Methods (==) :: IsolationLevel -> IsolationLevel -> Bool Source # (/=) :: IsolationLevel -> IsolationLevel -> Bool Source #
Ord IsolationLevel Source # |

Instance details

Defined in Database.PostgreSQL.Simple.Transaction

Methods

compare :: IsolationLevel -> IsolationLevel -> Ordering Source #

(<) :: IsolationLevel -> IsolationLevel -> Bool Source #

(<=) :: IsolationLevel -> IsolationLevel -> Bool Source #

(>) :: IsolationLevel -> IsolationLevel -> Bool Source #

(>=) :: IsolationLevel -> IsolationLevel -> Bool Source #

max :: IsolationLevel -> IsolationLevel -> IsolationLevel Source #

min :: IsolationLevel -> IsolationLevel -> IsolationLevel Source #

data ReadWriteMode Source #

Constructors

DefaultReadWriteMode | the read-write mode will be taken from PostgreSQL's per-connection `default_transaction_read_only` variable, which is initialized according to the server's config. The default configuration is `ReadWrite`.
ReadWrite |
ReadOnly |

#### Instances

Instances details

Bounded ReadWriteMode Source # |
Instance details Defined in Database.PostgreSQL.Simple.Transaction Methods minBound :: ReadWriteMode Source # maxBound :: ReadWriteMode Source #
Enum ReadWriteMode Source # |

Instance details

Defined in Database.PostgreSQL.Simple.Transaction

Methods

succ :: ReadWriteMode -> ReadWriteMode Source #

pred :: ReadWriteMode -> ReadWriteMode Source #

toEnum :: Int -> ReadWriteMode Source #

fromEnum :: ReadWriteMode -> Int Source #

enumFrom :: ReadWriteMode -> [ReadWriteMode] Source #

enumFromThen :: ReadWriteMode -> ReadWriteMode -> [ReadWriteMode] Source #

enumFromTo :: ReadWriteMode -> ReadWriteMode -> [ReadWriteMode] Source #

enumFromThenTo :: ReadWriteMode -> ReadWriteMode -> ReadWriteMode -> [ReadWriteMode] Source #

Show ReadWriteMode Source # |
Instance details Defined in Database.PostgreSQL.Simple.Transaction Methods showsPrec :: Int -> ReadWriteMode -> ShowS Source # show :: ReadWriteMode -> String Source # showList :: [ReadWriteMode] -> ShowS Source #
Eq ReadWriteMode Source # |
Instance details Defined in Database.PostgreSQL.Simple.Transaction Methods (==) :: ReadWriteMode -> ReadWriteMode -> Bool Source # (/=) :: ReadWriteMode -> ReadWriteMode -> Bool Source #
Ord ReadWriteMode Source # |

Instance details

Defined in Database.PostgreSQL.Simple.Transaction

Methods

compare :: ReadWriteMode -> ReadWriteMode -> Ordering Source #

(<) :: ReadWriteMode -> ReadWriteMode -> Bool Source #

(<=) :: ReadWriteMode -> ReadWriteMode -> Bool Source #

(>) :: ReadWriteMode -> ReadWriteMode -> Bool Source #

(>=) :: ReadWriteMode -> ReadWriteMode -> Bool Source #

max :: ReadWriteMode -> ReadWriteMode -> ReadWriteMode Source #

min :: ReadWriteMode -> ReadWriteMode -> ReadWriteMode Source #

defaultTransactionMode :: TransactionMode Source #

defaultIsolationLevel :: IsolationLevel Source #

defaultReadWriteMode :: ReadWriteMode Source #

begin :: Connection -> IO () Source #

Begin a transaction.

beginLevel :: IsolationLevel -> Connection -> IO () Source #

Begin a transaction with a given isolation level

beginMode :: TransactionMode -> Connection -> IO () Source #

Begin a transaction with a given transaction mode

commit :: Connection -> IO () Source #

Commit a transaction.

rollback :: Connection -> IO () Source #

Rollback a transaction.

# Savepoint

withSavepoint :: Connection -> IO a -> IO a Source #

Create a savepoint, and roll back to it if an error occurs. This may only be used inside of a transaction, and provides a sort of "nested transaction".

See https://www.postgresql.org/docs/9.5/static/sql-savepoint.html

data Savepoint Source #

#### Instances

Instances details

Read Savepoint Source # |
Instance details Defined in Database.PostgreSQL.Simple.Types Methods readsPrec :: Int -> ReadS Savepoint Source # readList :: ReadS [Savepoint] Source # readPrec :: ReadPrec Savepoint Source # readListPrec :: ReadPrec [Savepoint] Source #
Show Savepoint Source # |
Instance details Defined in Database.PostgreSQL.Simple.Types Methods showsPrec :: Int -> Savepoint -> ShowS Source # show :: Savepoint -> String Source # showList :: [Savepoint] -> ShowS Source #
Eq Savepoint Source # |
Instance details Defined in Database.PostgreSQL.Simple.Types Methods (==) :: Savepoint -> Savepoint -> Bool Source # (/=) :: Savepoint -> Savepoint -> Bool Source #
Ord Savepoint Source # |

Instance details

Defined in Database.PostgreSQL.Simple.Types

Methods

compare :: Savepoint -> Savepoint -> Ordering Source #

(<) :: Savepoint -> Savepoint -> Bool Source #

(<=) :: Savepoint -> Savepoint -> Bool Source #

(>) :: Savepoint -> Savepoint -> Bool Source #

(>=) :: Savepoint -> Savepoint -> Bool Source #

max :: Savepoint -> Savepoint -> Savepoint Source #

min :: Savepoint -> Savepoint -> Savepoint Source #

newSavepoint :: Connection -> IO Savepoint Source #

Create a new savepoint. This may only be used inside of a transaction.

releaseSavepoint :: Connection -> Savepoint -> IO () Source #

Destroy a savepoint, but retain its effects.

Warning: this will throw a `SqlError` matching `isFailedTransactionError` if the transaction is aborted due to an error. `commit` would merely warn and roll back.

rollbackToSavepoint :: Connection -> Savepoint -> IO () Source #

Roll back to a savepoint. This will not release the savepoint.

rollbackToAndReleaseSavepoint :: Connection -> Savepoint -> IO () Source #

Roll back to a savepoint and release it. This is like calling `rollbackToSavepoint` followed by `releaseSavepoint`, but avoids a round trip to the database server.

# Error predicates

isSerializationError :: SqlError -> Bool Source #

isNoActiveTransactionError :: SqlError -> Bool Source #

isFailedTransactionError :: SqlError -> Bool Source #