Skip to content

Commit 690e96f

Browse files
bahusoidfredericDelaporte
authored andcommitted
Scoped table aliases to session factory configuration (#1517)
1 parent f5aa820 commit 690e96f

File tree

6 files changed

+21
-54
lines changed

6 files changed

+21
-54
lines changed

lib/teamcity/oracle/oracle_installation.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ This is needed because NHibernate test uses the managed driver NuGet package, bu
4141
version needs to be removed from the GAC. Read more on https://stackoverflow.com/a/35176586/1178314.
4242
Not doing this may notably cause failures in distributed transaction tests.
4343

44+
Adjust the connection string for the tests:
45+
The tests involve creating and dropping many tables, sometimes with the same names but different data
46+
types. This does not play well with Oracle meta data pooling, which needs to be disabled.
47+
Add into your ODP.NET connection string:
48+
Metadata Pooling=false;Self Tuning=false;
49+
4450
Please note that some tests are dependent on the machine locales, and may fail if they are not English.

src/NHibernate.Test/MappingTest/ColumnFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void GetAliasWithTableSuffixRespectsMaxAliasLength(string columnName)
8181
// Test case is meant for a max length of 10, adjusts name if it is more.
8282
columnName = AdjustColumnNameToMaxLength(columnName, dialect, 10);
8383

84-
var table = new Table();
84+
var table = new Table() {UniqueInteger = 1};
8585
var column = new Column(columnName);
8686

8787
string generatedAlias = column.GetAlias(dialect, table);

src/NHibernate.Test/MappingTest/TableFixture.cs

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,5 @@ public void SchemaNameQuoted()
6161

6262
Assert.AreEqual("[schema].name", tbl.GetQualifiedName(dialect));
6363
}
64-
65-
[Test]
66-
public void TablesUniquelyNamed()
67-
{
68-
Table tbl1 = new Table();
69-
Table tbl2 = new Table();
70-
71-
Assert.AreEqual(tbl1.UniqueInteger + 1, tbl2.UniqueInteger);
72-
}
73-
74-
[Test]
75-
public void TablesUniquelyNamedOnlyWithinThread()
76-
{
77-
var uniqueIntegerList = new System.Collections.Concurrent.ConcurrentBag<int>();
78-
var method = new ThreadStart(() =>
79-
{
80-
Table tbl1 = new Table();
81-
Table tbl2 = new Table();
82-
83-
// Store these values for later comparison
84-
uniqueIntegerList.Add(tbl1.UniqueInteger);
85-
uniqueIntegerList.Add(tbl2.UniqueInteger);
86-
87-
// Ensure that within a thread we have unique integers
88-
Assert.AreEqual(tbl1.UniqueInteger + 1, tbl2.UniqueInteger);
89-
});
90-
91-
var thread1 = new CrossThreadTestRunner(method);
92-
var thread2 = new CrossThreadTestRunner(method);
93-
94-
thread1.Start();
95-
thread2.Start();
96-
97-
thread1.Join();
98-
thread2.Join();
99-
100-
// There should in total be 4 tables, but only two distinct identifiers.
101-
Assert.AreEqual(4, uniqueIntegerList.Count);
102-
Assert.AreEqual(2, uniqueIntegerList.Distinct().Count());
103-
}
10464
}
105-
}
65+
}

src/NHibernate/Cfg/Mappings.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ public Table AddTable(string schema, string catalog, string name, string subsele
271271
table.Catalog = catalog;
272272
table.Subselect = subselect;
273273
table.SchemaActions = GetSchemaActions(schemaAction);
274+
table.UniqueInteger = tables.Count;
274275
tables[key] = table;
275276
}
276277
else
@@ -337,15 +338,17 @@ public Table AddDenormalizedTable(string schema, string catalog, string name, bo
337338
Subselect = subselect
338339
};
339340

340-
Table existing;
341-
if (tables.TryGetValue(key, out existing))
341+
var tableIndex = tables.Count;
342+
if (tables.TryGetValue(key, out var existing))
342343
{
343344
if (existing.IsPhysicalTable)
344345
{
345346
throw new DuplicateMappingException("table", name);
346347
}
348+
tableIndex = existing.UniqueInteger;
347349
}
348350

351+
table.UniqueInteger = tableIndex;
349352
tables[key] = table;
350353
return table;
351354
}

src/NHibernate/Mapping/Table.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@ public enum SchemaAction
2626
[Serializable]
2727
public class Table : IRelationalModel
2828
{
29-
[ThreadStatic]
30-
private static int tableCounter;
31-
3229
private readonly List<string> checkConstraints = new List<string>();
3330
private readonly LinkedHashMap<string, Column> columns = new LinkedHashMap<string, Column>();
3431
private readonly Dictionary<ForeignKeyKey, ForeignKey> foreignKeys = new Dictionary<ForeignKeyKey, ForeignKey>();
3532
private readonly Dictionary<string, Index> indexes = new Dictionary<string, Index>();
36-
private readonly int uniqueInteger;
33+
private int? uniqueInteger;
3734
private readonly Dictionary<string, UniqueKey> uniqueKeys = new Dictionary<string, UniqueKey>();
3835
private string catalog;
3936
private string comment;
@@ -53,7 +50,6 @@ public class Table : IRelationalModel
5350
/// </summary>
5451
public Table()
5552
{
56-
uniqueInteger = tableCounter++;
5753
}
5854

5955
public Table(string name) : this()
@@ -190,11 +186,13 @@ public string Schema
190186

191187
/// <summary>
192188
/// Gets the unique number of the Table.
189+
/// Used for SQL alias generation
193190
/// </summary>
194191
/// <value>The unique number of the Table.</value>
195192
public int UniqueInteger
196193
{
197-
get { return uniqueInteger; }
194+
get { return uniqueInteger ?? throw new InvalidOperationException(nameof(UniqueInteger) + " has not been supplied"); }
195+
set { uniqueInteger = value; }
198196
}
199197

200198
/// <summary>

teamcity.build

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
<target name="setup-teamcity-oracle32">
125125
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.OracleDataClientDriver" />
126126
<property name="nhibernate.dialect" value="NHibernate.Dialect.Oracle10gDialect" />
127-
<property name="nhibernate.connection.connection_string" value="User ID=nhibernate;Password=nhibernate;Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))" />
127+
<property name="nhibernate.connection.connection_string" value="User ID=nhibernate;Password=nhibernate;Metadata Pooling=false;Self Tuning=false;Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))" />
128128
<!-- Teamcity Oracle test database is configured with a non-Unicode encoding -->
129129
<property name="nhibernate.oracle.use_n_prefixed_types_for_unicode" value="true" />
130130
<copy todir="${bin.dir}">
@@ -140,7 +140,7 @@
140140
<property name="nunit-x64" value="true" />
141141
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.OracleDataClientDriver" />
142142
<property name="nhibernate.dialect" value="NHibernate.Dialect.Oracle10gDialect" />
143-
<property name="nhibernate.connection.connection_string" value="User ID=nhibernate;Password=nhibernate;Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))" />
143+
<property name="nhibernate.connection.connection_string" value="User ID=nhibernate;Password=nhibernate;Metadata Pooling=false;Self Tuning=false;Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))" />
144144
<!-- Teamcity Oracle test database is configured with a non-Unicode encoding -->
145145
<property name="nhibernate.oracle.use_n_prefixed_types_for_unicode" value="true" />
146146
<copy todir="${bin.dir}">
@@ -155,7 +155,7 @@
155155
<target name="setup-teamcity-oracle-managed32">
156156
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.OracleManagedDataClientDriver" />
157157
<property name="nhibernate.dialect" value="NHibernate.Dialect.Oracle10gDialect" />
158-
<property name="nhibernate.connection.connection_string" value="User ID=nhibernate;Password=nhibernate;Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))" />
158+
<property name="nhibernate.connection.connection_string" value="User ID=nhibernate;Password=nhibernate;Metadata Pooling=false;Self Tuning=false;Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))" />
159159
<!-- Teamcity Oracle test database is configured with a non-Unicode encoding -->
160160
<property name="nhibernate.oracle.use_n_prefixed_types_for_unicode" value="true" />
161161
</target>
@@ -164,7 +164,7 @@
164164
<property name="nunit-x64" value="true" />
165165
<property name="nhibernate.connection.driver_class" value="NHibernate.Driver.OracleManagedDataClientDriver" />
166166
<property name="nhibernate.dialect" value="NHibernate.Dialect.Oracle10gDialect" />
167-
<property name="nhibernate.connection.connection_string" value="User ID=nhibernate;Password=nhibernate;Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))" />
167+
<property name="nhibernate.connection.connection_string" value="User ID=nhibernate;Password=nhibernate;Metadata Pooling=false;Self Tuning=false;Data Source=(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = XE)))" />
168168
<!-- Teamcity Oracle test database is configured with a non-Unicode encoding -->
169169
<property name="nhibernate.oracle.use_n_prefixed_types_for_unicode" value="true" />
170170
</target>

0 commit comments

Comments
 (0)