PostgreSQLCopyHelper: copyHelper.SaveAll deadlocks
Description
It appears that I am getting a deadlock when calling copyHelper.SaveAll(connection, tableEntities);
. The same data inserts without a problem when I use an INSERT
command. The SaveAll
deadlocks whether I’m attempting to insert 1 or a million records. It even deadlocks when I attempt to SaveAll
with an empty collection of tableEntities.
Environment Windows 10 Enterprise .NET 4.8 PostgreSQL 12 WPF application C#
To Reproduce My copyHelper looks like this:
var copyHelper = new PostgreSQLCopyHelper<TableEntity>("MY_TABLE")
.UsePostgresQuoting(true)
.MapUUID("HARVEST_ID", x => x.HarvestGuid)
.MapVarchar("OWNER", x => x.Owner)
.MapVarchar("NAME", x => x.Name);
TableEntity is simply this:
public class TableEntity
{
public Guid HarvestGuid { get; set; }
public string Owner { get; set; }
public string Name { get; set; }
}
and tableEntities is:
List<TableEntity> tableEntities = new List<TableEntity>();
To insert the records, the following code is executed:
public bool InsertTables(Guid harvestGuid, DataTable dt)
{
if (!DataStoreReady)
return false;
if (dt == null || dt.Rows.Count == 0)
return true;
// prepare the data for insertion.
var copyHelper = new PostgreSQLCopyHelper<TableEntity>("MY_TABLE")
.UsePostgresQuoting(true)
.MapUUID("HARVEST_ID", x => x.HarvestGuid)
.MapVarchar("OWNER", x => x.Owner)
.MapVarchar("NAME", x => x.Name);
List<TableEntity> tableEntities = new List<TableEntity>();
//brute force...
foreach(DataRow row in dt.Rows)
{
TableEntity t = new TableEntity();
t.HarvestGuid = harvestGuid;
t.Owner = row["OWNER"].ToString();
t.Name = row["TABLE_NAME"].ToString();
tableEntities.Add(t);
}
try
{
using (NpgsqlConnection conn = new NpgsqlConnection(_connectionString.ConnectionString))
{
conn.Open();
Logger.Debug("Beginning insertion of TABLE records.");
copyHelper.SaveAll(conn, tableEntities); // <-- here is where the code deadlocks.
Logger.Debug("Completed insertion of TABLE records.");
}
return true;
}
catch (Exception ex)
{
Logger.Error("An exception was thrown while attempting to insert harvest inforamtion.");
Logger.Error(ex);
return false;
}
}
Expected behavior I expect the records to be inserted into the table.
Desktop (please complete the following information): Windows 10 Enterprise
Additional context
I’ve tried this with 10,000 records, 1 record, even 0 records in the tableEntities
list, but it always locks up. No exception is thrown. This is all executing on the UI thread.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 18 (12 by maintainers)
Commits related to this issue
- Implement Issue #59 Deadlock Scenario — committed to PostgreSQLCopyHelper/PostgreSQLCopyHelper by bytefish 5 years ago
- Issue #59 Use ConfigureAwait(false) for SaveAll — committed to PostgreSQLCopyHelper/PostgreSQLCopyHelper by bytefish 5 years ago
- Issue #59 Use NoSynchronizationContextScope — committed to PostgreSQLCopyHelper/PostgreSQLCopyHelper by bytefish 5 years ago
- Issue #59 Create 2.6.1 Release — committed to PostgreSQLCopyHelper/PostgreSQLCopyHelper by bytefish 5 years ago
Just upgraded to 2.6.2 and can confirm that
SaveAll(...)
no longer deadlocks.