Skip to content

Commit

Permalink
Avoid stackoverflow when updating thousands of entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorium committed Jun 15, 2023
1 parent 9ce164c commit e1c768a
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.Firebird.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ type internal FirebirdProvider(resolutionPath, contextSchemaPath, owner, referen
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"

do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)

if scope<>null then scope.Complete()

Expand Down
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.MSAccess.fs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ type internal MSAccessProvider(contextSchemaPath) =
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"

do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)
trnsx.Commit()

with _ ->
Expand Down
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.MsSqlServer.Dynamic.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ type internal MSSqlServerDynamicProvider(resolutionPath, contextSchemaPath, refe
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"

do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)
if scope<>null then scope.Complete()

finally
Expand Down
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.MsSqlServer.Ssdt.fs
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ type internal MSSqlServerProviderSsdt(tableNames: string, ssdtPath: string) =
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"

do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)
if scope<>null then scope.Complete()

finally
Expand Down
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.MsSqlServer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ type internal MSSqlServerProvider(contextSchemaPath, tableNames:string) =
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"

do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)
if scope<>null then scope.Complete()

finally
Expand Down
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.MySql.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,7 +1106,7 @@ type internal MySqlProvider(resolutionPath, contextSchemaPath, owner:string, ref
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"

do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)

if scope<>null then scope.Complete()

Expand Down
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.Odbc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ type internal OdbcProvider(contextSchemaPath, quotechar : OdbcQuoteCharacter) =
e._State <- Deleted
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"
do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)
if scope<>null then scope.Complete()

finally
Expand Down
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.Oracle.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ type internal OracleProvider(resolutionPath, contextSchemaPath, owner, reference
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"

do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)
if scope<>null then scope.Complete()

finally
Expand Down
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.Postgresql.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,7 @@ type internal PostgresqlProvider(resolutionPath, contextSchemaPath, owner, refer
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"

do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)
if scope<>null then scope.Complete()

finally
Expand Down
2 changes: 1 addition & 1 deletion src/SQLProvider.Runtime/Providers.SQLite.fs
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ type internal SQLiteProvider(resolutionPath, contextSchemaPath, referencedAssemb
}
| Deleted | Unchanged -> failwith "Unchanged entity encountered in update list - this should not be possible!"

do! Utilities.executeOneByOne handleEntity (entities.Keys|>Seq.toList)
let! _ = Sql.evaluateOneByOne handleEntity (entities.Keys|>Seq.toList)
if scope<>null then scope.Complete()

finally
Expand Down
13 changes: 2 additions & 11 deletions src/SQLProvider.Runtime/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,6 @@ module internal Utilities =
| count -> name + "_" + (string count)
)

/// DB-connections are not usually supporting parallel SQL-query execution, so even when
/// async thread is available, it can't be used to execute another SQL at the same time.
let rec executeOneByOne asyncFunc entityList =
match entityList with
| h::t ->
task {
do! asyncFunc h
return! executeOneByOne asyncFunc t
}
| [] -> task { () }

let parseAggregates fieldNotat fieldNotationAlias query =
let rec parseAggregates' fieldNotation fieldNotationAlias query (selectColumns:string list) =
match query with
Expand Down Expand Up @@ -662,6 +651,8 @@ module Sql =

/// Helper function to run async computation non-parallel style for list of objects.
/// This is needed if async database opreation is executed for a list of entities.
/// DB-connections are not usually supporting parallel SQL-query execution, so even when
/// async thread is available, it can't be used to execute another SQL at the same time.
let evaluateOneByOne asyncFunc entityList =
let rec executeOneByOneTask' asyncFunc entityList acc =
match entityList with
Expand Down

0 comments on commit e1c768a

Please sign in to comment.