Skip to content

Commit 7bca7ea

Browse files
committed
Merge branch 'fix_increment_generator' of github.com:amroel/nhibernate-core into amroel-fix_increment_generator
2 parents 38d28d8 + f8bd460 commit 7bca7ea

File tree

4 files changed

+127
-19
lines changed

4 files changed

+127
-19
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using System.Data;
2+
using NHibernate.Driver;
3+
using NUnit.Framework;
4+
using SharpTestsEx;
5+
6+
namespace NHibernate.Test.DriverTest
7+
{
8+
[TestFixture]
9+
public class FirebirdClientDriverFixture
10+
{
11+
#region Fields
12+
private string _connectionString;
13+
private FirebirdClientDriver _driver;
14+
#endregion
15+
16+
#region Tests
17+
18+
[Test]
19+
public void ConnectionPooling_OpenThenCloseThenOpenAnotherOne_OnlyOneConnectionIsPooled()
20+
{
21+
MakeDriver();
22+
var connection1 = MakeConnection();
23+
var connection2 = MakeConnection();
24+
25+
//open first connection
26+
connection1.Open();
27+
VerifyCountOfEstablishedConnectionsIs(1);
28+
29+
//return it to the pool
30+
connection1.Close();
31+
VerifyCountOfEstablishedConnectionsIs(1);
32+
33+
//open the second connection
34+
connection2.Open();
35+
VerifyCountOfEstablishedConnectionsIs(1);
36+
37+
//return it to the pool
38+
connection2.Close();
39+
VerifyCountOfEstablishedConnectionsIs(1);
40+
}
41+
42+
[Test]
43+
public void ConnectionPooling_OpenThenCloseTwoAtTheSameTime_TowConnectionsArePooled()
44+
{
45+
MakeDriver();
46+
var connection1 = MakeConnection();
47+
var connection2 = MakeConnection();
48+
49+
//open first connection
50+
connection1.Open();
51+
VerifyCountOfEstablishedConnectionsIs(1);
52+
53+
//open second one
54+
connection2.Open();
55+
VerifyCountOfEstablishedConnectionsIs(2);
56+
57+
//return connection1 to the pool
58+
connection1.Close();
59+
VerifyCountOfEstablishedConnectionsIs(2);
60+
61+
//return connection2 to the pool
62+
connection2.Close();
63+
VerifyCountOfEstablishedConnectionsIs(2);
64+
}
65+
66+
#endregion
67+
68+
#region Private Members
69+
private void MakeDriver()
70+
{
71+
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
72+
var dlct = cfg.GetProperty("dialect");
73+
if (!dlct.Contains("Firebird"))
74+
Assert.Ignore("Applies only to Firebird");
75+
76+
_driver = new FirebirdClientDriver();
77+
_connectionString = cfg.GetProperty("connection.connection_string");
78+
}
79+
80+
private IDbConnection MakeConnection()
81+
{
82+
var result = _driver.CreateConnection();
83+
result.ConnectionString = _connectionString;
84+
return result;
85+
}
86+
87+
private void VerifyCountOfEstablishedConnectionsIs(int expectedCount)
88+
{
89+
var physicalConnections = GetEstablishedConnections();
90+
physicalConnections.Should().Be(expectedCount);
91+
}
92+
93+
private int GetEstablishedConnections()
94+
{
95+
using (var conn = _driver.CreateConnection())
96+
{
97+
conn.ConnectionString = _connectionString;
98+
conn.Open();
99+
using (var cmd = conn.CreateCommand())
100+
{
101+
cmd.CommandText = "select count(*) from mon$attachments where mon$attachment_id <> current_connection";
102+
return (int)cmd.ExecuteScalar();
103+
}
104+
}
105+
}
106+
#endregion
107+
}
108+
}

src/NHibernate.Test/NHSpecificTest/NH1061/Fixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public void IncrementGeneratorShouldIncludeClassLevelSchemaWhenGettingNextId()
3131
// think this would be a huge problem.
3232
// Having said that, if someone sees this and have a better idea to test,
3333
// please feel free to change it.
34-
FieldInfo sqlFieldInfo = generator.GetType().GetField("sql", BindingFlags.NonPublic | BindingFlags.Instance);
35-
string sql = (string)sqlFieldInfo.GetValue(generator);
34+
FieldInfo sqlFieldInfo = generator.GetType().GetField("_sql", BindingFlags.NonPublic | BindingFlags.Instance);
35+
string sql = sqlFieldInfo.GetValue(generator).ToString();
3636

3737
Assert.AreEqual("select max(Id) from test.TestNH1061", sql);
3838
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@
222222
<Compile Include="DialectTest\MsSqlCe40DialectFixture.cs" />
223223
<Compile Include="DialectTest\SchemaTests\ColumnMetaDataFixture.cs" />
224224
<Compile Include="DriverTest\DbProviderFactoryDriveConnectionCommandProviderTest.cs" />
225+
<Compile Include="DriverTest\FirebirdClientDriverFixture.cs" />
225226
<Compile Include="DriverTest\ReflectionBasedDriverTest.cs" />
226227
<Compile Include="DriverTest\Sql2008DateTime2Test.cs" />
227228
<Compile Include="DriverTest\SqlClientDriverFixture.cs" />

src/NHibernate/Id/IncrementGenerator.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data;
4+
using System.Data.Common;
45
using System.Runtime.CompilerServices;
56
using System.Text;
6-
77
using NHibernate.Engine;
88
using NHibernate.Exceptions;
9+
using NHibernate.SqlCommand;
10+
using NHibernate.SqlTypes;
911
using NHibernate.Type;
10-
using NHibernate.Util;
11-
using System.Data.Common;
1212

1313
namespace NHibernate.Id
1414
{
@@ -30,7 +30,7 @@ public class IncrementGenerator : IIdentifierGenerator, IConfigurable
3030
private static readonly IInternalLogger log = LoggerProvider.LoggerFor(typeof(IncrementGenerator));
3131

3232
private long next;
33-
private string sql;
33+
private SqlString _sql;
3434
private System.Type returnClass;
3535

3636
/// <summary>
@@ -72,7 +72,8 @@ public void Configure(IType type, IDictionary<string, string> parms, Dialect.Dia
7272
column = "ids_." + column;
7373
}
7474

75-
sql = "select max(" + column + ") from " + buf;
75+
var sqlTxt = string.Format("select max({0}) from {1}", column, buf);
76+
_sql = new SqlString(sqlTxt);
7677
}
7778

7879
/// <summary>
@@ -84,7 +85,7 @@ public void Configure(IType type, IDictionary<string, string> parms, Dialect.Dia
8485
[MethodImpl(MethodImplOptions.Synchronized)]
8586
public object Generate(ISessionImplementor session, object obj)
8687
{
87-
if (sql != null)
88+
if (_sql != null)
8889
{
8990
GetNext(session);
9091
}
@@ -93,38 +94,36 @@ public object Generate(ISessionImplementor session, object obj)
9394

9495
private void GetNext(ISessionImplementor session)
9596
{
96-
log.Debug("fetching initial value: " + sql);
97+
log.Debug("fetching initial value: " + _sql);
9798

9899
try
99100
{
100-
IDbConnection conn = session.Factory.ConnectionProvider.GetConnection();
101-
IDbCommand qps = conn.CreateCommand();
102-
qps.CommandText = sql;
103-
qps.CommandType = CommandType.Text;
101+
var cmd = session.Batcher.PrepareCommand(CommandType.Text, _sql, SqlTypeFactory.NoTypes);
102+
IDataReader reader = null;
104103
try
105104
{
106-
IDataReader rs = qps.ExecuteReader();
105+
reader = session.Batcher.ExecuteReader(cmd);
107106
try
108107
{
109-
if (rs.Read())
108+
if (reader.Read())
110109
{
111-
next = !rs.IsDBNull(0) ? Convert.ToInt64(rs.GetValue(0)) + 1 : 1L;
110+
next = !reader.IsDBNull(0) ? Convert.ToInt64(reader.GetValue(0)) + 1 : 1L;
112111
}
113112
else
114113
{
115114
next = 1L;
116115
}
117-
sql = null;
116+
_sql = null;
118117
log.Debug("first free id: " + next);
119118
}
120119
finally
121120
{
122-
rs.Close();
121+
reader.Close();
123122
}
124123
}
125124
finally
126125
{
127-
session.Factory.ConnectionProvider.CloseConnection(conn);
126+
session.Batcher.CloseCommand(cmd, reader);
128127
}
129128
}
130129
catch (DbException sqle)

0 commit comments

Comments
 (0)