Skip to content

Commit 8a82880

Browse files
committed
Fix checking the current PoolName
1 parent 347ee4e commit 8a82880

1 file changed

Lines changed: 36 additions & 28 deletions

File tree

src/pgapp_worker.erl

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,70 @@
2626
-define(MAXIMUM_DELAY, 5 * 60 * 1000). % Five minutes
2727
-define(TIMEOUT, 5 * 1000).
2828

29-
-define(TX_CONNECTION_VAR(PoolName), {'$pgapp_tx_connection', PoolName}).
29+
-define(STATE_VAR, '$pgapp_state').
3030

3131
squery(Sql) ->
32-
squery(epgsql_pool, Sql).
32+
case get(?STATE_VAR) of
33+
undefined ->
34+
squery(epgsql_pool, Sql);
35+
{_PoolName, Conn} ->
36+
epgsql:squery(Conn, Sql)
37+
end.
3338

3439
squery(PoolName, Sql) when is_atom(PoolName) ->
3540
squery(PoolName, Sql, ?TIMEOUT);
3641
squery(Sql, Timeout) ->
3742
squery(epgsql_pool, Sql, Timeout).
3843

3944
squery(PoolName, Sql, Timeout) ->
40-
case get(?TX_CONNECTION_VAR(PoolName)) of
41-
undefined ->
45+
case get(?STATE_VAR) of
46+
{PoolName, Conn} ->
47+
epgsql:squery(Conn, Sql);
48+
_ ->
4249
middle_man_transaction(PoolName,
4350
fun (W) ->
4451
gen_server:call(W, {squery, Sql}, Timeout)
45-
end, Timeout);
46-
Conn ->
47-
epgsql:squery(Conn, Sql)
52+
end, Timeout)
4853
end.
4954

5055
equery(Sql, Params) ->
51-
equery(epgsql_pool, Sql, Params).
56+
case get(?STATE_VAR) of
57+
undefined ->
58+
equery(epgsql_pool, Sql, Params);
59+
{_PoolName, Conn} ->
60+
epgsql:equery(Conn, Sql, Params)
61+
end.
5262

5363
equery(PoolName, Sql, Params) when is_atom(PoolName) ->
5464
equery(PoolName, Sql, Params, ?TIMEOUT);
5565
equery(Sql, Params, Timeout) ->
5666
equery(epgsql_pool, Sql, Params, Timeout).
5767

5868
equery(PoolName, Sql, Params, Timeout) ->
59-
case get(?TX_CONNECTION_VAR(PoolName)) of
60-
undefined ->
69+
case get(?STATE_VAR) of
70+
{PoolName, Conn} ->
71+
epgsql:equery(Conn, Sql, Params);
72+
_ ->
6173
middle_man_transaction(PoolName,
6274
fun (W) ->
6375
gen_server:call(W, {equery, Sql, Params}, Timeout)
64-
end, Timeout);
65-
Conn ->
66-
epgsql:equery(Conn, Sql, Params)
76+
end, Timeout)
6777
end.
6878

6979
with_transaction(PoolName, Fun) ->
7080
with_transaction(PoolName, Fun, ?TIMEOUT).
7181

7282
with_transaction(PoolName, Fun, Timeout) ->
73-
middle_man_transaction(PoolName,
74-
fun (W) ->
75-
gen_server:call(W, {transaction, PoolName, Fun},
76-
Timeout)
77-
end, Timeout).
83+
case get(?STATE_VAR) of
84+
{PoolName, _Conn} ->
85+
Fun();
86+
_ ->
87+
middle_man_transaction(PoolName,
88+
fun (W) ->
89+
gen_server:call(W, {transaction, PoolName, Fun},
90+
Timeout)
91+
end, Timeout)
92+
end.
7893

7994
middle_man_transaction(Pool, Fun, Timeout) ->
8095
Tag = make_ref(),
@@ -111,16 +126,9 @@ handle_call({equery, Sql, Params}, _From,
111126
{reply, epgsql:equery(Conn, Sql, Params), State};
112127
handle_call({transaction, PoolName, Fun}, _From,
113128
#state{conn = Conn} = State) ->
114-
Result = case get(?TX_CONNECTION_VAR(PoolName)) of
115-
undefined ->
116-
put(?TX_CONNECTION_VAR(PoolName), Conn),
117-
Res = epgsql:with_transaction(Conn, fun(_) -> Fun() end),
118-
erase(?TX_CONNECTION_VAR(PoolName)),
119-
Res;
120-
_ ->
121-
% transaction is already in progress
122-
Fun()
123-
end,
129+
put(?STATE_VAR, {PoolName, Conn}),
130+
Result = epgsql:with_transaction(Conn, fun(_) -> Fun() end),
131+
erase(?STATE_VAR),
124132
{reply, Result, State}.
125133

126134
handle_cast(reconnect, State) ->

0 commit comments

Comments
 (0)