Skip to content

smarkets/epgsql_connpool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Erlang PostgreSQL Database Client Pool for epgsql

Configure application env like so:

    application:start(epgsql),
    application:set_env(epgsql_connpool, pools, 
        [ {maindb, 
                [
                    {host,      "127.0.0.1"}, 
                    {username,  "postgres"}, 
                    {password,  ""}, 
                    {size,      10}, 
                    {opts, [
                            {timeout,  5000}, 
                            {database, "my_database"}
                           ]}
                ]
          }
        ]
    ),
    application:start(epgsql_connpool).

And to make queries:
    
    {atomic, NumRows} = 
    epgsql_connpool:transaction( maindb, fun(_Db) ->
        Sql = "SELECT count(*) FROM my_table WHERE id = $1;"
        {ok, _, [{Ret}]} = epgsql_query:equery(Sql, [123]),
        Ret
    end).

With transaction(), all queries made within the fun are surrounded by a 
BEGIN/COMMIT/ROLLBACK.

The fun passed to epgsql_connpool:transaction/* will be handed a pid
as its first argument. epgsql_connpool stores the connection pid in
the process dictionary. By storing the pid, calls to the epgsql_query
module (which is very similar in interface to the pgsql module) don't
need to pass the pid directly. To support older applications, the pid
can either be used to make calls to the pgsql module directly.

With dirty(), the statements are run as-is, not in a transaction.

About

Pool application for epgsql

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages