Skip to content

Commit

Permalink
Cancel the query when an exception occurs
Browse files Browse the repository at this point in the history
This moves a Dblib.cancel from the next query into the current query.
This should make us hold onto resources in failure cases for a shorter
amount of time.
  • Loading branch information
brendanlong committed May 12, 2020
1 parent a8f1107 commit 582481c
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions src/client.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,36 @@ let execute' ?params ~query ~formatted_query ({ month_offset; _ } as t) ~f =
In_thread.run
@@ fun () ->
Mssql_error.with_wrap ~query ?params ~formatted_query [%here] (fun () ->
Dblib.cancel conn;
Dblib.sqlexec conn formatted_query;
let previous_result_set = ref Iter.empty in
Iter.from_fun (fun () ->
(* Ensure the previous result set was fully consumed *)
IterLabels.iter !previous_result_set ~f:ignore;
if Dblib.results conn
then (
Dblib.numcols conn
|> List.range 0
|> List.map ~f:(fun i -> Dblib.colname conn (i + 1))
|> function
| [] ->
(* Skip this result set if there are no columns, since this indicates results from things
like inserts with no row data *)
Some None
| colnames ->
previous_result_set
:= Iter.from_fun (fun () ->
try
let row = Dblib.nextrow conn in
let row = Row.create_exn ~month_offset ~colnames row in
Some row
with
| Caml.Not_found -> None);
!previous_result_set |> Option.some |> Option.some)
else None)
|> IterLabels.filter_map ~f:Fn.id
|> f)
let iter =
let previous_result_set = ref Iter.empty in
Iter.from_fun (fun () ->
(* Ensure the previous result set was fully consumed *)
IterLabels.iter !previous_result_set ~f:ignore;
if Dblib.results conn
then (
Dblib.numcols conn
|> List.range 0
|> List.map ~f:(fun i -> Dblib.colname conn (i + 1))
|> function
| [] ->
(* Skip this result set if there are no columns, since this indicates results from things
like inserts with no row data *)
Some None
| colnames ->
previous_result_set
:= Iter.from_fun (fun () ->
try
let row = Dblib.nextrow conn in
let row = Row.create_exn ~month_offset ~colnames row in
Some row
with
| Caml.Not_found -> None);
!previous_result_set |> Option.some |> Option.some)
else None)
|> IterLabels.filter_map ~f:Fn.id
in
Exn.protect ~f:(fun () -> f iter) ~finally:(fun () -> Dblib.cancel conn))
;;

let execute_multi_result' ?(params = []) conn query =
Expand Down

0 comments on commit 582481c

Please sign in to comment.