Runs body in a transaction on a connection from the pool. The transaction commits when body
returns no error, and rolls back when it returns one (which comes out of transaction) or a
statement in it failed. A serialization failure or deadlock (40001, 40P01) runs body again, up
to retries more times, so body must be safe to repeat. data is passed through, as Jai
procedures capture nothing.
- pool: the pool to borrow a connection from.
- data: passed to
body. - body: the work, run on the transaction's connection.
- isolation: the transaction's isolation level.
- retries: how many more times to run
bodyafter a serialization failure or deadlock. - read_only: start the transaction READ ONLY.
Transfer :: struct { from: s64; to: s64; amount: s64; }
error := Pg.transaction(pool, Transfer.{ 1, 2, 10 }, (tx: *Pg.Connection, t: Transfer) -> Pg.Pg_Error {
_, error := Pg.query_dynamic(tx, "update accounts set balance = balance - $1 where id = $2", t.amount, t.from);
if error.kind != .NONE return error;
_, error = Pg.query_dynamic(tx, "update accounts set balance = balance + $1 where id = $2", t.amount, t.to);
return error;
}, .SERIALIZABLE, retries = 3);Not run: the example is marked ignore.
Not run: it needs a PostgreSQL server.
transaction :: (
pool: *Pool,
data: $T,
body: (tx: *Connection, data: T) -> Pg_Error,
isolation := Isolation.DEFAULT,
retries := 0,
read_only := false
) -> Pg_Error
Runs body in a transaction on a connection from the pool. The transaction commits when body
returns no error, and rolls back when it returns one (which comes out of transaction) or a
statement in it failed. A serialization failure or deadlock (40001, 40P01) runs body again, up
to retries more times, so body must be safe to repeat. data is passed through, as Jai
procedures capture nothing.
Not run: it needs a PostgreSQL server.
Parameters
- pool: *Pool
- the pool to borrow a connection from.
- data: $T
- passed to
body. - body: (tx: *Connection, data: T) -> Pg_Error
- the work, run on the transaction's connection.
- isolation := Isolation.DEFAULT
- the transaction's isolation level.
- retries := 0
- how many more times to run
bodyafter a serialization failure or deadlock. - read_only := false
- start the transaction READ ONLY.
Returns
Transfer :: struct { from: s64; to: s64; amount: s64; }
error := Pg.transaction(pool, Transfer.{ 1, 2, 10 }, (tx: *Pg.Connection, t: Transfer) -> Pg.Pg_Error {
_, error := Pg.query_dynamic(tx, "update accounts set balance = balance - $1 where id = $2", t.amount, t.from);
if error.kind != .NONE return error;
_, error = Pg.query_dynamic(tx, "update accounts set balance = balance + $1 where id = $2", t.amount, t.to);
return error;
}, .SERIALIZABLE, retries = 3);Not run: the example is marked ignore.
transaction :: (
pool: *Pool,
body: (tx: *Connection) -> Pg_Error,
isolation := Isolation.DEFAULT,
retries := 0,
read_only := false
) -> Pg_Error
transaction for a body that needs no data.
Parameters
- pool: *Pool
- the pool to borrow a connection from.
- body: (tx: *Connection) -> Pg_Error
- the work, run on the transaction's connection.
- isolation := Isolation.DEFAULT
- the transaction's isolation level.
- retries := 0
- how many more times to run
bodyafter a serialization failure or deadlock. - read_only := false
- start the transaction READ ONLY.
Returns
transaction :: (
c: *Connection,
data: $T,
body: (tx: *Connection, data: T) -> Pg_Error,
isolation := Isolation.DEFAULT,
retries := 0,
read_only := false
) -> Pg_Error
The same on a connection of your own.
Parameters
- c: *Connection
- the connection. It must not be in a transaction.
- data: $T
- passed to
body. - body: (tx: *Connection, data: T) -> Pg_Error
- the work, run on
c. - isolation := Isolation.DEFAULT
- the transaction's isolation level.
- retries := 0
- how many more times to run
bodyafter a serialization failure or deadlock. - read_only := false
- start the transaction READ ONLY.
Returns
transaction :: (
c: *Connection,
body: (tx: *Connection) -> Pg_Error,
isolation := Isolation.DEFAULT,
retries := 0,
read_only := false
) -> Pg_Error
transaction on your own connection, for a body that needs no data.
Parameters
- c: *Connection
- the connection. It must not be in a transaction.
- body: (tx: *Connection) -> Pg_Error
- the work, run on
c. - isolation := Isolation.DEFAULT
- the transaction's isolation level.
- retries := 0
- how many more times to run
bodyafter a serialization failure or deadlock. - read_only := false
- start the transaction READ ONLY.