Skip to content

Commit

Permalink
Don't close the active reader
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Nov 8, 2024
1 parent d3bd950 commit 03198ef
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public T ProcessExecutedCommands<T>(DbCommand command, DbContext? context, T res

using (var dbReaderLoader = new EFDataReaderLoader(dataReader))
{
tableRows = dbReaderLoader.LoadAndClose();
tableRows = dbReaderLoader.Load();
}

if (!ShouldSkipCachingResults(commandText, tableRows))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Entity Framework Core Second Level Caching Library.</Description>
<VersionPrefix>4.8.4</VersionPrefix>
<VersionPrefix>4.8.5</VersionPrefix>
<Authors>Vahid Nasiri</Authors>
<TargetFrameworks>net8.0;net7.0;net6.0;net5.0;netstandard2.1;netstandard2.0;net462;netcoreapp3.1;</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
32 changes: 19 additions & 13 deletions src/EFCoreSecondLevelCacheInterceptor/EFDataReaderLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ public class EFDataReaderLoader : DbDataReader
public EFDataReaderLoader(DbDataReader dbReader)
{
_dbReader = dbReader;

_tableRows = new EFTableRows(_dbReader)
{
FieldCount = _dbReader.FieldCount,
VisibleFieldCount = _dbReader.VisibleFieldCount,
};
{
FieldCount = _dbReader.FieldCount,
VisibleFieldCount = _dbReader.VisibleFieldCount
};
}

/// <summary>
Expand Down Expand Up @@ -182,6 +183,7 @@ public EFDataReaderLoader(DbDataReader dbReader)
public override int GetValues(object[] values)
{
Array.Copy(_rowValues, values, _rowValues.Length);

return _rowValues.Length;
}

Expand Down Expand Up @@ -231,15 +233,17 @@ public override bool Read()
}

_tableRows?.Add(new EFTableRow(_rowValues)
{
Depth = _dbReader.Depth,
});
{
Depth = _dbReader.Depth
});

return true;
}

private byte[] getSqlBytes(int ordinal)
{
byte[] buffer;

using (var stream = _dbReader.GetStream(ordinal))
{
if (stream.Length > int.MaxValue)
Expand All @@ -248,12 +252,14 @@ private byte[] getSqlBytes(int ordinal)
}

buffer = new byte[stream.Length];

if (stream.Position != 0)
{
stream.Seek(0, SeekOrigin.Begin);
stream.Seek(offset: 0, SeekOrigin.Begin);
}

var count = stream.Read(buffer, 0, checked((int)stream.Length));
var count = stream.Read(buffer, offset: 0, checked((int)stream.Length));

if (count <= 0)
{
return buffer;
Expand All @@ -266,21 +272,21 @@ private byte[] getSqlBytes(int ordinal)
private bool isBinary(int ordinal)
{
var typeName = _tableRows.GetFieldTypeName(ordinal);
return string.Equals(typeName, "Microsoft.SqlServer.Types.SqlGeography", StringComparison.Ordinal)
|| string.Equals(typeName, "Microsoft.SqlServer.Types.SqlGeometry", StringComparison.Ordinal);

return string.Equals(typeName, b: "Microsoft.SqlServer.Types.SqlGeography", StringComparison.Ordinal) ||
string.Equals(typeName, b: "Microsoft.SqlServer.Types.SqlGeometry", StringComparison.Ordinal);
}

/// <summary>
/// Converts a DbDataReader to an EFTableRows
/// </summary>
public EFTableRows LoadAndClose()
public EFTableRows Load()
{
while (Read())
{
// Read all data
}

Close();
return _tableRows;
}
}
Loading

0 comments on commit 03198ef

Please sign in to comment.