Skip to content

Commit b104ae1

Browse files
committed
Append the batched sql statement when StaleStateException occurs
1 parent 70c2bab commit b104ae1

15 files changed

+99
-16
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by AsyncGenerator.
4+
//
5+
// Changes to this file may cause incorrect behavior and will be lost if
6+
// the code is regenerated.
7+
// </auto-generated>
8+
//------------------------------------------------------------------------------
9+
10+
11+
using System.Collections;
12+
using System.Collections.Generic;
13+
using NHibernate.AdoNet;
14+
using NHibernate.Cfg;
15+
using NHibernate.DomainModel;
16+
using NHibernate.DomainModel.NHSpecific;
17+
using NUnit.Framework;
18+
19+
namespace NHibernate.Test.NHSpecificTest
20+
{
21+
using System.Threading.Tasks;
22+
[TestFixture]
23+
public class OptimisticConcurrencyBatchedFixtureAsync : OptimisticConcurrencyFixtureAsync
24+
{
25+
protected override void Configure(Configuration configuration)
26+
{
27+
configuration.SetProperty(Environment.BatchStrategy, typeof(GenericBatchingBatcherFactory).AssemblyQualifiedName);
28+
configuration.SetProperty(Environment.BatchSize, "5");
29+
configuration.SetProperty(Environment.BatchVersionedData, "true");
30+
}
31+
}
32+
}

src/NHibernate.Test/Async/NHSpecificTest/OptimisticConcurrencyFixture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using NHibernate.DomainModel;
1515
using NHibernate.DomainModel.NHSpecific;
1616
using NUnit.Framework;
17+
using NUnit.Framework.Constraints;
1718

1819
namespace NHibernate.Test.NHSpecificTest
1920
{
@@ -68,7 +69,7 @@ public async Task StaleObjectStateCheckWithNormalizedEntityPersisterAsync()
6869
top.Name = "new name";
6970

7071
var expectedException = Sfi.Settings.IsBatchVersionedDataEnabled
71-
? Throws.InstanceOf<StaleStateException>()
72+
? (IResolveConstraint) Throws.InstanceOf<StaleStateException>().And.Message.Contains("UPDATE rootclass")
7273
: Throws.InstanceOf<StaleObjectStateException>();
7374

7475
Assert.That(() => session.FlushAsync(), expectedException);
@@ -107,7 +108,7 @@ public async Task StaleObjectStateCheckWithEntityPersisterAndOptimisticLockAsync
107108
optimistic.String = "new string";
108109

109110
var expectedException = Sfi.Settings.IsBatchVersionedDataEnabled
110-
? Throws.InstanceOf<StaleStateException>()
111+
? (IResolveConstraint) Throws.InstanceOf<StaleStateException>().And.Message.Contains("UPDATE Optimistic")
111112
: Throws.InstanceOf<StaleObjectStateException>();
112113

113114
Assert.That(() => session.FlushAsync(), expectedException);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using NHibernate.AdoNet;
4+
using NHibernate.Cfg;
5+
using NHibernate.DomainModel;
6+
using NHibernate.DomainModel.NHSpecific;
7+
using NUnit.Framework;
8+
9+
namespace NHibernate.Test.NHSpecificTest
10+
{
11+
[TestFixture]
12+
public class OptimisticConcurrencyBatchedFixture : OptimisticConcurrencyFixture
13+
{
14+
protected override void Configure(Configuration configuration)
15+
{
16+
configuration.SetProperty(Environment.BatchStrategy, typeof(GenericBatchingBatcherFactory).AssemblyQualifiedName);
17+
configuration.SetProperty(Environment.BatchSize, "5");
18+
configuration.SetProperty(Environment.BatchVersionedData, "true");
19+
}
20+
}
21+
}

src/NHibernate.Test/NHSpecificTest/OptimisticConcurrencyFixture.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using NHibernate.DomainModel;
55
using NHibernate.DomainModel.NHSpecific;
66
using NUnit.Framework;
7+
using NUnit.Framework.Constraints;
78

89
namespace NHibernate.Test.NHSpecificTest
910
{
@@ -57,7 +58,7 @@ public void StaleObjectStateCheckWithNormalizedEntityPersister()
5758
top.Name = "new name";
5859

5960
var expectedException = Sfi.Settings.IsBatchVersionedDataEnabled
60-
? Throws.InstanceOf<StaleStateException>()
61+
? (IResolveConstraint) Throws.InstanceOf<StaleStateException>().And.Message.Contains("UPDATE rootclass")
6162
: Throws.InstanceOf<StaleObjectStateException>();
6263

6364
Assert.That(() => session.Flush(), expectedException);
@@ -96,7 +97,7 @@ public void StaleObjectStateCheckWithEntityPersisterAndOptimisticLock()
9697
optimistic.String = "new string";
9798

9899
var expectedException = Sfi.Settings.IsBatchVersionedDataEnabled
99-
? Throws.InstanceOf<StaleStateException>()
100+
? (IResolveConstraint) Throws.InstanceOf<StaleStateException>().And.Message.Contains("UPDATE Optimistic")
100101
: Throws.InstanceOf<StaleObjectStateException>();
101102

102103
Assert.That(() => session.Flush(), expectedException);

src/NHibernate/AdoNet/Expectations.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Data.Common;
33
using NHibernate.Engine;
4+
using NHibernate.Exceptions;
45

56
namespace NHibernate.AdoNet
67
{
@@ -99,6 +100,8 @@ private Expectations()
99100
{
100101
}
101102

103+
// Since 5.2
104+
[Obsolete]
102105
public static void VerifyOutcomeBatched(int expectedRowCount, int rowCount)
103106
{
104107
if (expectedRowCount > rowCount)
@@ -115,5 +118,30 @@ public static void VerifyOutcomeBatched(int expectedRowCount, int rowCount)
115118
throw new TooManyRowsAffectedException(msg, expectedRowCount, rowCount);
116119
}
117120
}
121+
122+
public static void VerifyOutcomeBatched(int expectedRowCount, int rowCount, DbCommand statement)
123+
{
124+
if (expectedRowCount > rowCount)
125+
{
126+
throw new StaleStateException(
127+
ADOExceptionHelper.ExtendMessage(
128+
$"Batch update returned unexpected row count from update; actual row count: {rowCount}; expected: {expectedRowCount}",
129+
statement.CommandText,
130+
null,
131+
null)
132+
);
133+
}
134+
if (expectedRowCount < rowCount)
135+
{
136+
throw new TooManyRowsAffectedException(
137+
ADOExceptionHelper.ExtendMessage(
138+
$"Batch update returned unexpected row count from update; actual row count: {rowCount}; expected: {expectedRowCount}",
139+
statement.CommandText,
140+
null,
141+
null),
142+
expectedRowCount,
143+
rowCount);
144+
}
145+
}
118146
}
119147
}

src/NHibernate/AdoNet/GenericBatchingBatcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected override void DoExecuteBatch(DbCommand ps)
6767
{
6868
if (_currentBatch.CountOfCommands == 0)
6969
{
70-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, 0);
70+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, 0, ps);
7171
return;
7272
}
7373
try
@@ -89,7 +89,7 @@ protected override void DoExecuteBatch(DbCommand ps)
8989
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
9090
}
9191

92-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);
92+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected, ps);
9393
}
9494
finally
9595
{

src/NHibernate/AdoNet/HanaBatchingBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected override void DoExecuteBatch(DbCommand ps)
116116
}
117117
}
118118

119-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowCount);
119+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowCount, ps);
120120
}
121121
finally
122122
{

src/NHibernate/AdoNet/MySqlClientBatchingBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected override void DoExecuteBatch(DbCommand ps)
9292
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
9393
}
9494

95-
Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected);
95+
Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected, ps);
9696
}
9797
finally
9898
{

src/NHibernate/AdoNet/OracleDataClientBatchingBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ protected override void DoExecuteBatch(DbCommand ps)
136136
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
137137
}
138138

139-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);
139+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected, ps);
140140
}
141141
finally
142142
{

src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected override void DoExecuteBatch(DbCommand ps)
9191
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
9292
}
9393

94-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);
94+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected, ps);
9595
}
9696
finally
9797
{

src/NHibernate/Async/AdoNet/GenericBatchingBatcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected override async Task DoExecuteBatchAsync(DbCommand ps, CancellationToke
4949
cancellationToken.ThrowIfCancellationRequested();
5050
if (_currentBatch.CountOfCommands == 0)
5151
{
52-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, 0);
52+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, 0, ps);
5353
return;
5454
}
5555
try
@@ -71,7 +71,7 @@ protected override async Task DoExecuteBatchAsync(DbCommand ps, CancellationToke
7171
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
7272
}
7373

74-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);
74+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected, ps);
7575
}
7676
finally
7777
{

src/NHibernate/Async/AdoNet/HanaBatchingBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected override async Task DoExecuteBatchAsync(DbCommand ps, CancellationToke
116116
}
117117
}
118118

119-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowCount);
119+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowCount, ps);
120120
}
121121
finally
122122
{

src/NHibernate/Async/AdoNet/MySqlClientBatchingBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ protected override async Task DoExecuteBatchAsync(DbCommand ps, CancellationToke
7878
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
7979
}
8080

81-
Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected);
81+
Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected, ps);
8282
}
8383
finally
8484
{

src/NHibernate/Async/AdoNet/OracleDataClientBatchingBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ protected override async Task DoExecuteBatchAsync(DbCommand ps, CancellationToke
139139
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
140140
}
141141

142-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);
142+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected, ps);
143143
}
144144
finally
145145
{

src/NHibernate/Async/AdoNet/SqlClientBatchingBatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected override async Task DoExecuteBatchAsync(DbCommand ps, CancellationToke
8686
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
8787
}
8888

89-
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected);
89+
Expectations.VerifyOutcomeBatched(_totalExpectedRowsAffected, rowsAffected, ps);
9090
}
9191
finally
9292
{

0 commit comments

Comments
 (0)