Skip to content

Commit 347ee4e

Browse files
committed
with_transaction to ignore nested transactions
1 parent 4bb7aad commit 347ee4e

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/pgapp_worker.erl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,16 @@ handle_call({equery, Sql, Params}, _From,
111111
{reply, epgsql:equery(Conn, Sql, Params), State};
112112
handle_call({transaction, PoolName, Fun}, _From,
113113
#state{conn = Conn} = State) ->
114-
put(?TX_CONNECTION_VAR(PoolName), Conn),
115-
Result = epgsql:with_transaction(Conn, fun(_) -> Fun() end),
116-
erase(?TX_CONNECTION_VAR(PoolName)),
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,
117124
{reply, Result, State}.
118125

119126
handle_cast(reconnect, State) ->

0 commit comments

Comments
 (0)