Safe Haskell | None |
---|---|
Language | Haskell98 |
- 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
- withTransactionSerializable :: Connection -> IO a -> IO a
- data TransactionMode = TransactionMode {}
- data IsolationLevel
- data ReadWriteMode
- 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
Like withTransactionMode
, but also takes a custom callback to
determine if a transaction should be retried if an SqlError
occurs.
If the callback returns True, then the transaction will be retried.
If the callback returns False, or an exception other than an SqlError
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
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 http://www.postgresql.org/docs/9.1/static/transaction-iso.html
for more information. Note that prior to PostgreSQL 9.0, RepeatableRead
was equivalent to Serializable
.
DefaultIsolationLevel | the isolation level will be taken from
PostgreSQL's per-connection
|
ReadCommitted | |
RepeatableRead | |
Serializable |
data ReadWriteMode Source
DefaultReadWriteMode | the read-write mode will be taken from
PostgreSQL's per-connection
|
ReadWrite | |
ReadOnly |
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 http://www.postgresql.org/docs/current/static/sql-savepoint.html
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.