Skip to content

[WIP] Try fix ODP.NET DTC issues #532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
4 changes: 2 additions & 2 deletions src/NHibernate.Test/ConnectionTest/AggressiveReleaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void SuppliedConnection()
session.Flush();

Release(session);
originalConnection.Close();
sessions.ConnectionProvider.CloseConnection(originalConnection);
Done();
}

Expand Down Expand Up @@ -245,4 +245,4 @@ public void ConnectionMaintanenceDuringFlush()
Done();
}
}
}
}
16 changes: 11 additions & 5 deletions src/NHibernate.Test/DialectTest/DialectFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,24 @@ public void CurrentTimestampSelection()
sessions.ConnectionProvider.Configure(conf.Properties);
IDriver driver = sessions.ConnectionProvider.Driver;

using (IDbConnection connection = sessions.ConnectionProvider.GetConnection())
var connection = sessions.ConnectionProvider.GetConnection();
try
{
IDbCommand statement = driver.GenerateCommand(CommandType.Text, new SqlString(dialect.CurrentTimestampSelectString),
new SqlType[0]);
IDbCommand statement = driver.GenerateCommand(
CommandType.Text,
new SqlString(dialect.CurrentTimestampSelectString),
new SqlType[0]);
statement.Connection = connection;
using (IDataReader reader = statement.ExecuteReader())
{
Assert.That(reader.Read(), "should return one record");
Assert.That(reader[0], Is.InstanceOf<DateTime>());
}
}
finally
{
sessions.ConnectionProvider.CloseConnection(connection);
}
}

}
}
}
29 changes: 15 additions & 14 deletions src/NHibernate.Test/DriverTest/FirebirdClientDriverFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,25 @@ public class FirebirdClientDriverFixture
public void ConnectionPooling_OpenThenCloseThenOpenAnotherOne_OnlyOneConnectionIsPooled()
{
MakeDriver();
var connection1 = MakeConnection();
var connection2 = MakeConnection();
using (var connection1 = MakeConnection())
using (var connection2 = MakeConnection())
{
connection1.Open();
VerifyCountOfEstablishedConnectionsIs(1);

//open first connection
connection1.Open();
VerifyCountOfEstablishedConnectionsIs(1);
//return it to the pool
connection1.Close();
VerifyCountOfEstablishedConnectionsIs(1);

//return it to the pool
connection1.Close();
VerifyCountOfEstablishedConnectionsIs(1);
//open the second connection
connection2.Open();
VerifyCountOfEstablishedConnectionsIs(1);

//open the second connection
connection2.Open();
VerifyCountOfEstablishedConnectionsIs(1);
//return it to the pool
connection2.Close();

//return it to the pool
connection2.Close();
VerifyCountOfEstablishedConnectionsIs(1);
VerifyCountOfEstablishedConnectionsIs(1);
}
}

[Test]
Expand Down
31 changes: 19 additions & 12 deletions src/NHibernate.Test/Legacy/FooBarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4978,20 +4978,27 @@ public void AutoFlushCollections()
public void UserProvidedConnection()
{
IConnectionProvider prov = ConnectionProviderFactory.NewConnectionProvider(cfg.Properties);
ISession s = sessions.OpenSession(prov.GetConnection());
ITransaction tx = s.BeginTransaction();
s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
tx.Commit();
var conn = prov.GetConnection();
try
{
ISession s = sessions.OpenSession(conn);
ITransaction tx = s.BeginTransaction();
s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
tx.Commit();

IDbConnection c = s.Disconnect();
Assert.IsNotNull(c);
IDbConnection c = s.Disconnect();
Assert.IsNotNull(c);

s.Reconnect(c);
tx = s.BeginTransaction();
s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
tx.Commit();
Assert.AreSame(c, s.Close());
c.Close();
s.Reconnect(c);
tx = s.BeginTransaction();
s.CreateQuery("from foo in class NHibernate.DomainModel.Fo").List();
tx.Commit();
Assert.AreSame(c, s.Close());
}
finally
{
prov.CloseConnection(conn);
}
}

[Test]
Expand Down
9 changes: 7 additions & 2 deletions src/NHibernate.Test/Linq/LinqReadonlyTestsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ private void ExecuteScriptFile(Configuration configuration, string scripFileName
var file = new FileInfo(scripFileName);
string script = file.OpenText().ReadToEnd().Replace("GO", "");
var connectionProvider = ConnectionProviderFactory.NewConnectionProvider(configuration.GetDerivedProperties());
using (var conn = connectionProvider.GetConnection())
var conn = connectionProvider.GetConnection();
try
{
if (conn.State == ConnectionState.Closed)
{
Expand All @@ -83,6 +84,10 @@ private void ExecuteScriptFile(Configuration configuration, string scripFileName
command.ExecuteNonQuery();
}
}
finally
{
connectionProvider.CloseConnection(conn);
}
}

[TearDown]
Expand Down Expand Up @@ -143,4 +148,4 @@ private void CreateTestData(ISessionFactory sessionFactory)
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2420/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void ShouldBeAbleToReleaseSuppliedConnectionAfterDistributedTransaction()
// fires *after* the transaction is committed and so it doesn't affect the success
// of the transaction.

Assert.That(s.IsConnected, Is.False);
Assert.That(() => s.IsConnected, Is.False.After(500, 100));
Assert.That(((ISessionImplementor)s).ConnectionManager.IsConnected, Is.False);
Assert.That(((ISessionImplementor)s).IsClosed, Is.True);
}
Expand Down
29 changes: 17 additions & 12 deletions src/NHibernate.Test/NHSpecificTest/UserTypeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,32 @@ public void InsertNull()
// manually read from the db
IConnectionProvider provider = ConnectionProviderFactory.NewConnectionProvider(cfg.Properties);
IDbConnection conn = provider.GetConnection();
IDbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from usertype";
try
{
IDbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from usertype";

IDataReader reader = cmd.ExecuteReader();
IDataReader reader = cmd.ExecuteReader();

while (reader.Read())
while (reader.Read())
{
Assert.AreEqual(5, reader[0]);
Assert.AreEqual(4, reader[1]);
Assert.AreEqual(DBNull.Value, reader[2]);
break;
}
}
finally
{
Assert.AreEqual(5, reader[0]);
Assert.AreEqual(4, reader[1]);
Assert.AreEqual(DBNull.Value, reader[2]);
break;
provider.CloseConnection(conn);
}

conn.Close();

using (ISession s = OpenSession())
{
s.Delete("from ClassWithNullColumns");
s.Flush();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,12 @@ public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doComm

using (var tx = new TransactionScope())
{
IDbConnection ownConnection1 = sessions.ConnectionProvider.GetConnection();

var ownConnection = sessions.ConnectionProvider.GetConnection();
try
{
try
{
s1 = sessions.OpenSession(ownConnection1, interceptor);
s1 = sessions.OpenSession(ownConnection, interceptor);

s1.CreateCriteria<object>().List();
}
Expand All @@ -182,7 +181,7 @@ public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doComm
}
finally
{
sessions.ConnectionProvider.CloseConnection(ownConnection1);
sessions.ConnectionProvider.CloseConnection(ownConnection);
}
}

Expand All @@ -191,6 +190,5 @@ public void ShouldNotifyAfterDistributedTransactionWithOwnConnection(bool doComm

Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
}

}
}
}
13 changes: 7 additions & 6 deletions src/NHibernate.Test/TestCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,15 @@ protected virtual bool CheckDatabaseWasCleaned()

private bool CheckConnectionsWereClosed()
{
if (connectionProvider == null || !connectionProvider.HasOpenConnections)
{
if (connectionProvider == null)
return true;
}

log.Error("Test case didn't close all open connections, closing");
var hasOpenConnections = connectionProvider.HasOpenConnections;
if (hasOpenConnections)
log.Error("Test case didn't close all open connections, closing");

connectionProvider.CloseAllConnections();
return false;
return !hasOpenConnections;
}

private void Configure()
Expand Down Expand Up @@ -275,7 +276,7 @@ public int ExecuteStatement(string sql)

using (IConnectionProvider prov = ConnectionProviderFactory.NewConnectionProvider(cfg.Properties))
{
IDbConnection conn = prov.GetConnection();
var conn = prov.GetConnection();

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public void ShouldNotifyAfterTransactionWithOwnConnection(bool usePrematureClose
var interceptor = new RecordingInterceptor();
ISession s;

using (IDbConnection ownConnection = sessions.ConnectionProvider.GetConnection())
var ownConnection = sessions.ConnectionProvider.GetConnection();
try
{
using (s = sessions.OpenSession(ownConnection, interceptor))
using (s.BeginTransaction())
Expand All @@ -108,9 +109,13 @@ public void ShouldNotifyAfterTransactionWithOwnConnection(bool usePrematureClose
s.Close();
}
}
finally
{
sessions.ConnectionProvider.CloseConnection(ownConnection);
}

Assert.That(s.IsOpen, Is.False);
Assert.That(interceptor.afterTransactionCompletionCalled, Is.EqualTo(1));
}
}
}
}
27 changes: 19 additions & 8 deletions src/NHibernate/AdoNet/AbstractBatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Threading;

Expand Down Expand Up @@ -358,8 +359,11 @@ public void CloseReader(IDataReader reader)
var actualReader = rsw == null ? reader : rsw.Target;
_readersToClose.Remove(actualReader);

var duration = GetReaderStopwatch(actualReader);

try
{
//TODO: Shouldn't we close reader instead?
reader.Dispose();
}
catch (Exception e)
Expand All @@ -369,17 +373,24 @@ public void CloseReader(IDataReader reader)
}

LogCloseReader();
LogDuration(duration);
}

if (!Log.IsDebugEnabled)
return;

var nhReader = actualReader as NHybridDataReader;
actualReader = nhReader == null ? actualReader : nhReader.Target;
private Stopwatch GetReaderStopwatch(IDataReader reader)
{
var nhReader = reader as NHybridDataReader;
var actualReader = nhReader == null ? reader : nhReader.Target;

Stopwatch duration;
if (_readersDuration.TryGetValue(actualReader, out duration) == false)
return;
_readersDuration.Remove(actualReader);
if (_readersDuration.TryGetValue(actualReader, out duration))
_readersDuration.Remove(actualReader);
return duration;
}

private static void LogDuration(Stopwatch duration)
{
if (!Log.IsDebugEnabled || duration == null) return;

Log.DebugFormat("DataReader was closed after {0} ms", duration.ElapsedMilliseconds);
}

Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Dialect/Dialect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,7 @@ public virtual bool IsKnownToken(string currentToken, string nextToken)

protected void RegisterKeyword(string word)
{
Keywords.Add(word);
_sqlKeywords.Add(word);
}

protected void RegisterFunction(string name, ISQLFunction function)
Expand Down
Loading