Skip to content

Commit e408d06

Browse files
liviochazzik
authored andcommitted
Remove unnecessary ID increment in SequenceHiLoGenerator (#478)
Fixes #843
1 parent 9b45f98 commit e408d06

File tree

6 files changed

+142
-2
lines changed

6 files changed

+142
-2
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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;
12+
using NUnit.Framework;
13+
14+
namespace NHibernate.Test.NHSpecificTest.NH3879
15+
{
16+
using System.Threading.Tasks;
17+
[TestFixture]
18+
public class Fixture3879Async : BugTestCase
19+
{
20+
protected override bool AppliesTo(Dialect.Dialect dialect)
21+
{
22+
return dialect.SupportsSequences;
23+
}
24+
25+
[Test]
26+
public async Task SeqhiloUsesFirstValidIdAsync()
27+
{
28+
//the first value of a sequence changes between the different databases (it can be 0 or 1)
29+
//but the bug associated to this test happens only when it is different from zero so
30+
//we explicitly increment the sequence to ensure it
31+
long previousSequenceValue;
32+
using (var session = OpenSession())
33+
{
34+
var getSequenceSql = Dialect.GetSequenceNextValString("seqhiloEntity_ids");
35+
//some adonet providers return an int, others a long
36+
var boxedPreviousSequenceValue = await (session.CreateSQLQuery(getSequenceSql).UniqueResultAsync());
37+
previousSequenceValue = Convert.ToInt64(boxedPreviousSequenceValue);
38+
}
39+
40+
long id;
41+
using (var session = OpenSession())
42+
using (var tx = session.BeginTransaction())
43+
{
44+
var entity = new SeqhiloEntity();
45+
await (session.SaveAsync(entity));
46+
await (tx.CommitAsync());
47+
id = entity.Id;
48+
}
49+
50+
var expectedId = (previousSequenceValue + 1) * 10; //max_lo (9) + 1
51+
Assert.That(id, Is.EqualTo(expectedId));
52+
}
53+
54+
protected override void OnTearDown()
55+
{
56+
using (var session = OpenSession())
57+
using (var tx = session.BeginTransaction())
58+
{
59+
session.CreateQuery("delete from SeqhiloEntity").ExecuteUpdate();
60+
tx.Commit();
61+
}
62+
}
63+
}
64+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3879
5+
{
6+
[TestFixture]
7+
public class Fixture3879 : BugTestCase
8+
{
9+
protected override bool AppliesTo(Dialect.Dialect dialect)
10+
{
11+
return dialect.SupportsSequences;
12+
}
13+
14+
[Test]
15+
public void SeqhiloUsesFirstValidId()
16+
{
17+
//the first value of a sequence changes between the different databases (it can be 0 or 1)
18+
//but the bug associated to this test happens only when it is different from zero so
19+
//we explicitly increment the sequence to ensure it
20+
long previousSequenceValue;
21+
using (var session = OpenSession())
22+
{
23+
var getSequenceSql = Dialect.GetSequenceNextValString("seqhiloEntity_ids");
24+
//some adonet providers return an int, others a long
25+
var boxedPreviousSequenceValue = session.CreateSQLQuery(getSequenceSql).UniqueResult();
26+
previousSequenceValue = Convert.ToInt64(boxedPreviousSequenceValue);
27+
}
28+
29+
long id;
30+
using (var session = OpenSession())
31+
using (var tx = session.BeginTransaction())
32+
{
33+
var entity = new SeqhiloEntity();
34+
session.Save(entity);
35+
tx.Commit();
36+
id = entity.Id;
37+
}
38+
39+
var expectedId = (previousSequenceValue + 1) * 10; //max_lo (9) + 1
40+
Assert.That(id, Is.EqualTo(expectedId));
41+
}
42+
43+
protected override void OnTearDown()
44+
{
45+
using (var session = OpenSession())
46+
using (var tx = session.BeginTransaction())
47+
{
48+
session.CreateQuery("delete from SeqhiloEntity").ExecuteUpdate();
49+
tx.Commit();
50+
}
51+
}
52+
}
53+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH3879"
5+
default-lazy="false">
6+
7+
<class name="SeqhiloEntity" table="SeqhiloEntity">
8+
<id name="Id" type="Int64">
9+
<generator class="seqhilo" >
10+
<param name="max_lo">9</param>
11+
<param name="sequence">seqhiloEntity_ids</param>
12+
</generator>
13+
</id>
14+
</class>
15+
16+
</hibernate-mapping>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3879
2+
{
3+
public class SeqhiloEntity
4+
{
5+
public long Id { get; set; }
6+
}
7+
}

src/NHibernate/Async/Id/SequenceHiLoGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public override async Task<object> GenerateAsync(ISessionImplementor session, ob
5353
if (lo > maxLo)
5454
{
5555
long hival = Convert.ToInt64(await (base.GenerateAsync(session, obj, cancellationToken)).ConfigureAwait(false));
56-
lo = 1;
56+
lo = (hival == 0) ? 1 : 0;
5757
hi = hival * (maxLo + 1);
5858
if (log.IsDebugEnabled)
5959
log.Debug("new hi value: " + hival);

src/NHibernate/Id/SequenceHiLoGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public override object Generate(ISessionImplementor session, object obj)
9090
if (lo > maxLo)
9191
{
9292
long hival = Convert.ToInt64(base.Generate(session, obj));
93-
lo = 1;
93+
lo = (hival == 0) ? 1 : 0;
9494
hi = hival * (maxLo + 1);
9595
if (log.IsDebugEnabled)
9696
log.Debug("new hi value: " + hival);

0 commit comments

Comments
 (0)