Skip to content

Commit 2c61c8a

Browse files
author
Jan Rochel
committed
don't use unnecessary transaction blocks
1 parent 022ac43 commit 2c61c8a

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

src/extensions/ocsipersist-pgsql/ocsipersist.ml

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,10 @@ let connect () = PGOCaml.connect
2828

2929
let (>>) f g = f >>= fun _ -> g
3030

31-
let transaction_block db f =
32-
PGOCaml.begin_work db >>= fun _ ->
33-
try_lwt
34-
lwt r = f () in
35-
PGOCaml.commit db >>
36-
Lwt.return r
37-
with e ->
38-
print_endline (Printexc.to_string e);
39-
PGOCaml.rollback db >>
40-
Lwt.fail e
41-
4231
let pool : (string, bool) Hashtbl.t PGOCaml.t Lwt_pool.t =
4332
Lwt_pool.create 16 ~validate:PGOCaml.alive connect
4433

45-
let full_transaction_block f = (* copied from Eba_db *)
46-
Lwt_pool.use pool (fun db -> transaction_block db (fun () -> f db))
34+
let use_pool f = Lwt_pool.use pool (fun db -> f db)
4735

4836
let exec db query params =
4937
PGOCaml.prepare db ~query () >>
@@ -81,10 +69,10 @@ type 'a t = {
8169
name : string;
8270
}
8371

84-
let open_store store = full_transaction_block @@ fun db ->
72+
let open_store store = use_pool @@ fun db ->
8573
create_table db store >> Lwt.return store
8674

87-
let make_persistent_lazy_lwt ~store ~name ~default = full_transaction_block @@ fun db ->
75+
let make_persistent_lazy_lwt ~store ~name ~default = use_pool @@ fun db ->
8876
let query = sprintf "SELECT value FROM %s WHERE key = $1 " store in
8977
lwt result = exec db query [name] in
9078
lwt _ = begin match result with
@@ -102,41 +90,41 @@ let make_persistent_lazy ~store ~name ~default =
10290
let make_persistent ~store ~name ~default =
10391
make_persistent_lazy ~store ~name ~default:(fun () -> default)
10492

105-
let get p = full_transaction_block @@ fun db ->
93+
let get p = use_pool @@ fun db ->
10694
let query = sprintf "SELECT value FROM %s WHERE key = $1 " p.store in
10795
Lwt.map (unmarshal @. one) (exec db query [p.name])
10896

109-
let set p v = full_transaction_block @@ fun db ->
97+
let set p v = use_pool @@ fun db ->
11098
insert db p.store p.name v
11199

112100
type 'value table = string
113101

114102
let table_name table = Lwt.return table
115103

116-
let open_table table = full_transaction_block @@ fun db ->
104+
let open_table table = use_pool @@ fun db ->
117105
create_table db table >> Lwt.return table
118106

119-
let find table key = full_transaction_block @@ fun db ->
107+
let find table key = use_pool @@ fun db ->
120108
let query = sprintf "SELECT value FROM %s WHERE key = $1 " table in
121109
Lwt.map (unmarshal @. one) (exec db query [key])
122110

123-
let add table key value = full_transaction_block @@ fun db ->
111+
let add table key value = use_pool @@ fun db ->
124112
insert db table key value
125113

126114
let replace_if_exists table key value =
127115
try_lwt
128116
find table key >> add table key value
129117
with Not_found -> Lwt.return ()
130118

131-
let remove table key = full_transaction_block @@ fun db ->
119+
let remove table key = use_pool @@ fun db ->
132120
let query = sprintf "DELETE FROM %s WHERE key = $1 " table in
133121
exec db query [key] >> Lwt.return ()
134122

135-
let length table = full_transaction_block @@ fun db ->
123+
let length table = use_pool @@ fun db ->
136124
let query = sprintf "SELECT count(*) FROM %s " table in
137125
Lwt.map (unmarshal @. one) (exec db query [])
138126

139-
let iter_step f table = full_transaction_block @@ fun db ->
127+
let iter_step f table = use_pool @@ fun db ->
140128
let query = sprintf "SELECT * FROM %s " table in
141129
PGOCaml.prepare db ~query () >>
142130
PGOCaml.cursor db ~params:[] @@

0 commit comments

Comments
 (0)