generic-persistence-0.6.0: Database persistence using generics
Safe HaskellSafe-Inferred
LanguageGHC2021

Database.GP.Conn

Synopsis

Documentation

data Conn Source #

This module defines a wrapper around an HDBC IConnection. Using this wrapper Conn simplifies the signature of the functions in the GP module. It allows to use any HDBC connection without having to define a new function for each connection type. It also provides additional attributes to the connection, like the database type and the implicit commit flag. These attributes can be used to implement database specific functionality, modify transaction behaviour, etc.

This code has been inspired by the HDBC ConnectionWrapper and some parts have been copied verbatim from the HDBC Database.HDBC.Types module.

This module also defines a ConnectionPool type, which provides basic connection pooling functionality.

A wrapper around an HDBC IConnection.

Constructors

forall conn.IConnection conn => Conn 

Fields

  • implicitCommit :: Bool

    If True, the GenericPersistence functions will commit the transaction after each operation.

  • connection :: conn

    The wrapped connection

Instances

Instances details
IConnection Conn Source #

manually implement the IConnection type class for the Conn type.

Instance details

Defined in Database.GP.Conn

connect :: forall conn. IConnection conn => TxHandling -> conn -> Conn Source #

a smart constructor for the Conn type.

type ConnectionPool = Pool Conn Source #

A pool of connections.

createConnPool Source #

Arguments

:: IConnection conn 
=> TxHandling

the transaction mode

-> String

the connection string

-> (String -> IO conn)

a function that takes a connection string and returns an IConnection

-> Double

the time (in seconds) to keep idle connections open

-> Int

the maximum number of connections to keep open

-> IO ConnectionPool

the resulting connection pool

Creates a connection pool.

withResource :: Pool a -> (a -> IO r) -> IO r #

Take a resource from the pool, perform an action with it and return it to the pool afterwards.

  • If the pool has an idle resource available, it is used immediately.
  • Otherwise, if the maximum number of resources has not yet been reached, a new resource is created and used.
  • If the maximum number of resources has been reached, this function blocks until a resource becomes available.

If the action throws an exception of any type, the resource is destroyed and not returned to the pool.

It probably goes without saying that you should never manually destroy a pooled resource, as doing so will almost certainly cause a subsequent user (who expects the resource to be valid) to throw an exception.