Skip to content

Commit c7e97d2

Browse files
committed
Better connection disposing
1 parent 357204a commit c7e97d2

3 files changed

Lines changed: 21 additions & 25 deletions

File tree

src/SQLProvider.Common/Utils.fs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -744,16 +744,22 @@ module Sql =
744744
if Convert.IsDBNull(v) then def else unbox v
745745

746746
let connect (con:IDbConnection) f =
747-
if con.State <> ConnectionState.Open then con.Open()
748-
let result = f con
749-
con.Close(); result
747+
use connection = con
748+
try
749+
if connection.State <> ConnectionState.Open then connection.Open()
750+
f connection
751+
finally
752+
if connection.State = ConnectionState.Open then connection.Close()
750753

751754
let connectAsync (con:System.Data.Common.DbConnection) f =
752755
task {
753-
if con.State <> ConnectionState.Open then
754-
do! con.OpenAsync()
755-
let result = f con
756-
con.Close(); result
756+
use connection = con
757+
try
758+
if connection.State <> ConnectionState.Open then
759+
do! connection.OpenAsync()
760+
f connection
761+
finally
762+
if connection.State = ConnectionState.Open then connection.Close()
757763
}
758764

759765
let executeSql createCommand sql (con:IDbConnection) =

src/SQLProvider.Runtime/Providers.MsSqlServer.Dynamic.fs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,6 @@ module MSSqlServerDynamic =
174174
let dbUnboxWithDefault<'a> def (v:obj) : 'a =
175175
if Convert.IsDBNull(v) then def else unbox v
176176

177-
let connect (con:IDbConnection) f =
178-
if con.State <> ConnectionState.Open then con.Open()
179-
let result = f con
180-
con.Close(); result
181-
182177
let executeSql sql (con:IDbConnection) =
183178
use com = Activator.CreateInstance(commandType.Value,[|box sql;box con|]) :?> IDbCommand
184179
com.ExecuteReader()
@@ -238,7 +233,7 @@ module MSSqlServerDynamic =
238233
let query = sprintf "SET NO_BROWSETABLE ON; SET FMTONLY ON; exec %s %s" sname.DbName parameterStr
239234
let derivedCols =
240235
let initialSchemas =
241-
connect con (fun con ->
236+
Sql.connect con (fun con ->
242237
try
243238
let dr = executeSql query con
244239
[ yield dr.GetSchemaTable();
@@ -641,7 +636,7 @@ type internal MSSqlServerDynamicProvider(resolutionPath, contextSchemaPath, refe
641636
match tableNames with
642637
| "" -> ""
643638
| x -> " where 1=1 " + (SchemaProjections.buildTableNameWhereFilter "TABLE_NAME" tableNames)
644-
MSSqlServerDynamic.connect con (fun con ->
639+
Sql.connect con (fun con ->
645640
use reader = MSSqlServerDynamic.executeSql ("select TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE from INFORMATION_SCHEMA.TABLES" + tableNamesFilter) con
646641
[ while reader.Read() do
647642
let table ={ Schema = reader.GetString(0) ; Name = reader.GetString(1) ; Type=reader.GetString(2).ToLower() }
@@ -761,7 +756,7 @@ type internal MSSqlServerDynamicProvider(resolutionPath, contextSchemaPath, refe
761756
AND KCU2.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
762757
AND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION "
763758

764-
let res = MSSqlServerDynamic.connect con (fun con ->
759+
let res = Sql.connect con (fun con ->
765760
let baseq1 = sprintf "%s WHERE KCU2.TABLE_NAME = @tblName" baseQuery
766761
use com1 = (this:>ISqlProvider).CreateCommand(con,baseq1)
767762
com1.Parameters.Add((this:>ISqlProvider).CreateCommandParameter(QueryParameter.Create("@tblName", 0), table.Name)) |> ignore
@@ -785,7 +780,7 @@ type internal MSSqlServerDynamicProvider(resolutionPath, contextSchemaPath, refe
785780
(children,parents))
786781
res)
787782

788-
member __.GetSprocs(con) = MSSqlServerDynamic.connect con MSSqlServerDynamic.getSprocs
783+
member __.GetSprocs(con) = Sql.connect con MSSqlServerDynamic.getSprocs
789784
member __.GetIndividualsQueryText(table,amount) = sprintf "SELECT TOP %i * FROM %s" amount table.FullName
790785
member __.GetIndividualQueryText(table,column) = sprintf "SELECT * FROM [%s].[%s] WHERE [%s].[%s].[%s] = @id" table.Schema table.Name table.Schema table.Name column
791786

src/SQLProvider.Runtime/Providers.MsSqlServer.fs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,6 @@ module MSSqlServer =
110110
let dbUnboxWithDefault<'a> def (v:obj) : 'a =
111111
if Convert.IsDBNull(v) then def else unbox v
112112

113-
let connect (con:IDbConnection) f =
114-
if con.State <> ConnectionState.Open then con.Open()
115-
let result = f con
116-
con.Close(); result
117-
118113
let executeSql sql (con:IDbConnection) =
119114
use com = new SqlCommand(sql,con:?>SqlConnection)
120115
com.ExecuteReader()
@@ -164,7 +159,7 @@ module MSSqlServer =
164159
let query = sprintf "SET NO_BROWSETABLE ON; SET FMTONLY ON; exec %s %s" sname.DbName parameterStr
165160
let derivedCols =
166161
let initialSchemas =
167-
connect con (fun con ->
162+
Sql.connect con (fun con ->
168163
try
169164
let dr = executeSql query con
170165
[ yield dr.GetSchemaTable();
@@ -568,7 +563,7 @@ type internal MSSqlServerProvider(contextSchemaPath, tableNames:string) =
568563
match tableNames with
569564
| "" -> ""
570565
| x -> " where 1=1 " + (SchemaProjections.buildTableNameWhereFilter "TABLE_NAME" tableNames)
571-
MSSqlServer.connect con (fun con ->
566+
Sql.connect con (fun con ->
572567
use reader = MSSqlServer.executeSql ("select TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE from INFORMATION_SCHEMA.TABLES" + tableNamesFilter) con
573568
[ while reader.Read() do
574569
let table ={ Schema = reader.GetSqlString(0).Value ; Name = reader.GetSqlString(1).Value ; Type=reader.GetSqlString(2).Value.ToLower() }
@@ -684,7 +679,7 @@ type internal MSSqlServerProvider(contextSchemaPath, tableNames:string) =
684679
AND KCU2.CONSTRAINT_NAME = RC.UNIQUE_CONSTRAINT_NAME
685680
AND KCU2.ORDINAL_POSITION = KCU1.ORDINAL_POSITION "
686681

687-
let res = MSSqlServer.connect con (fun con ->
682+
let res = Sql.connect con (fun con ->
688683
let baseq1 = sprintf "%s WHERE KCU2.TABLE_NAME = @tblName" baseQuery
689684
use com1 = new SqlCommand(baseq1,con:?>SqlConnection)
690685
com1.Parameters.AddWithValue("@tblName",table.Name) |> ignore
@@ -713,7 +708,7 @@ type internal MSSqlServerProvider(contextSchemaPath, tableNames:string) =
713708
(children,parents))
714709
res)
715710

716-
member __.GetSprocs(con) = MSSqlServer.connect con MSSqlServer.getSprocs
711+
member __.GetSprocs(con) = Sql.connect con MSSqlServer.getSprocs
717712
member __.GetIndividualsQueryText(table,amount) = sprintf "SELECT TOP %i * FROM %s" amount table.FullName
718713
member __.GetIndividualQueryText(table,column) = sprintf "SELECT * FROM [%s].[%s] WHERE [%s].[%s].[%s] = @id" table.Schema table.Name table.Schema table.Name column
719714

0 commit comments

Comments
 (0)