|
@@ -3,9 +3,11 @@ MySQL/OTP + Poolboy
|
|
|
|
|
|
Status: Beta.
|
|
|
|
|
|
-**MySQL/OTP + Poolboy** provides connection pooling for [MySQL/OTP](//github.com/mysql-otp/mysql-otp) using [Poolboy](//github.com/devinus/poolboy). It provides convenience functions for executing
|
|
|
-SQL queries on a connection in a pool and lets you choose between two methods for createing
|
|
|
-and managing connection pools:
|
|
|
+**MySQL/OTP + Poolboy** provides connection pooling for
|
|
|
+[MySQL/OTP](//github.com/mysql-otp/mysql-otp) using
|
|
|
+[Poolboy](//github.com/devinus/poolboy). It contains convenience functions for
|
|
|
+executing SQL queries on a connection in a pool and lets you choose between two
|
|
|
+methods for creating and managing connection pools:
|
|
|
|
|
|
1. Use it as a library that helps you supervise your own MySQL connection pools.
|
|
|
2. Use it as an application that manages its own supervisor for connection pools.
|
|
@@ -13,8 +15,8 @@ and managing connection pools:
|
|
|
I want to supervise my own connection pools
|
|
|
-------------------------------------------
|
|
|
|
|
|
-Use `mysql_poolboy:child_spec/3` to get a child spec for a pool that you can use in
|
|
|
-your own supervisor.
|
|
|
+Use `mysql_poolboy:child_spec/3` to get a child spec for a pool that you can use
|
|
|
+in your own supervisor.
|
|
|
|
|
|
```Erlang
|
|
|
%% my own supervisor
|
|
@@ -34,15 +36,20 @@ init([]) ->
|
|
|
Let MySQL/OTP + Poolboy supervise my pools
|
|
|
------------------------------------------
|
|
|
|
|
|
-This approach requires you to start the application using `application:ensure_started(mysql_poolboy)` (or by letting your release tool do this for you).
|
|
|
+This approach requires you to start the application `mysql_poolboy`. Typically
|
|
|
+this is done by adding `{applications, [mysql_poolboy]}` to your `.app.src`
|
|
|
+file and then relying on your favourite release tool for the rest.
|
|
|
|
|
|
Pools can be added at run-time using `mysql_poolboy:add_pool/3`.
|
|
|
|
|
|
-Pools can also be created at start-up by defining configuration parameters for `mysql_poolboy`. The name of each configuration parameter is the pool name and the value is a pair on the form `{PoolOptions, MySqlOptions}`.
|
|
|
+Pools can also be created at start-up by defining configuration parameters for
|
|
|
+`mysql_poolboy`. The name of each configuration parameter is the pool name and
|
|
|
+the value is a pair on the form `{PoolOptions, MySqlOptions}`.
|
|
|
|
|
|
Example:
|
|
|
|
|
|
-Start your Erlang node with `erl -config mypools.config` where mypools.config:
|
|
|
+Start your Erlang node with `erl -config mypools.config` where `mypools.config`
|
|
|
+is a file with the following contents:
|
|
|
|
|
|
```Erlang
|
|
|
{mysql_poolboy, [
|
|
@@ -54,7 +61,8 @@ Start your Erlang node with `erl -config mypools.config` where mypools.config:
|
|
|
Using the connection pools
|
|
|
--------------------------
|
|
|
|
|
|
-The most commonly used MySQL functions are available with wrappers in mysql_poolboy.
|
|
|
+The most commonly used MySQL functions are available with wrappers in
|
|
|
+`mysql_poolboy`.
|
|
|
|
|
|
```Erlang
|
|
|
1> mysql_poolboy:query(pool1, "SELECT * FROM foo WHERE id=?", [42]).
|
|
@@ -63,7 +71,8 @@ The most commonly used MySQL functions are available with wrappers in mysql_pool
|
|
|
{ok,[<<"id">>,<<"bar">>],[[42,<<"baz">>]]}
|
|
|
```
|
|
|
|
|
|
-For transactions, the connection pid is passed to the transaction fun as the first parameter.
|
|
|
+For transactions, the connection pid is passed to the transaction fun as the
|
|
|
+first parameter.
|
|
|
|
|
|
```Erlang
|
|
|
3> mysql_poolboy:transaction(pool1, fun (Pid) ->
|
|
@@ -74,7 +83,10 @@ For transactions, the connection pid is passed to the transaction fun as the fir
|
|
|
{atomic, hello}
|
|
|
```
|
|
|
|
|
|
-Sometimes you need to checkout a connection to execute multiple queries on it, without wrapping it in an SQL transaction. For this purpose you can use either a pair of `checkout/1` and `checking/2` or a call to `with/2` with a fun.
|
|
|
+Sometimes you need to checkout a connection to execute multiple queries on it,
|
|
|
+without wrapping it in an SQL transaction. For this purpose you can use either
|
|
|
+a pair of calls to `checkout/1` and `checkin/2` or a call to `with/2` with a
|
|
|
+fun as in this example:
|
|
|
|
|
|
```Erlang
|
|
|
4> mysql_poolboy:with(pool1, fun (Pid) ->
|
|
@@ -89,19 +101,19 @@ ok
|
|
|
Use this as a dependency
|
|
|
------------------------
|
|
|
|
|
|
-Using *erlang.mk*:
|
|
|
+Using *erlang.mk*, put this in your `Makefile`:
|
|
|
|
|
|
```Erlang
|
|
|
DEPS = mysql_poolboy
|
|
|
dep_mysql_poolboy = git https://github.com/mysql-otp/mysql-otp-poolboy 0.1.0
|
|
|
```
|
|
|
|
|
|
-Using *rebar*:
|
|
|
+Using *rebar*, put this in your `rebar.config`:
|
|
|
|
|
|
```Erlang
|
|
|
{deps, [
|
|
|
{mysql_poolboy, ".*", {git, "https://github.com/mysql-otp/mysql-otp-poolboy",
|
|
|
- {tag, "0.1.0"}}}
|
|
|
+ {tag, "0.1.0"}}}
|
|
|
]}.
|
|
|
```
|
|
|
|