Skip to content

Commit 6d402f7

Browse files
committed
Merge branch '3.1.0GA_column-cache-concurency-fix' of git://github.com/grzpas/nhibernate-core into grzpas-3.1.0GA_column-cache-concurency-fix
2 parents 5c7ec9a + 2aac2bc commit 6d402f7

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System.Collections.Generic;
2+
using System.Threading;
23

34
namespace NHibernate.AdoNet
45
{
56
/// <summary> Implementation of ColumnNameCache. </summary>
67
public class ColumnNameCache
78
{
9+
private readonly ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
810
private readonly Dictionary<string, int?> columnNameToIndexCache;
911

1012
public ColumnNameCache(int columnCount)
@@ -15,18 +17,45 @@ public ColumnNameCache(int columnCount)
1517

1618
public int GetIndexForColumnName(string columnName, ResultSetWrapper rs)
1719
{
18-
int? cached;
19-
columnNameToIndexCache.TryGetValue(columnName, out cached);
20+
int? cached = Read(columnName);
2021
if (cached.HasValue)
2122
{
2223
return cached.Value;
2324
}
2425
else
2526
{
2627
int index = rs.Target.GetOrdinal(columnName);
27-
columnNameToIndexCache[columnName] = index;
28+
Insert(columnName, index);
2829
return index;
2930
}
3031
}
32+
33+
private int? Read(string key)
34+
{
35+
cacheLock.EnterReadLock();
36+
try
37+
{
38+
int? value;
39+
columnNameToIndexCache.TryGetValue(key, out value);
40+
return value;
41+
}
42+
finally
43+
{
44+
cacheLock.ExitReadLock();
45+
}
46+
}
47+
48+
private void Insert(string key, int value)
49+
{
50+
cacheLock.EnterWriteLock();
51+
try
52+
{
53+
columnNameToIndexCache[key] = value;
54+
}
55+
finally
56+
{
57+
cacheLock.ExitWriteLock();
58+
}
59+
}
3160
}
3261
}

0 commit comments

Comments
 (0)