Permalink
Browse files

Prevent closing/disposing IDbCommand when batching (NH-3010)

Full credit for the solution goes to cremor. This issue is known to affect Oracle and Firebird. A previous workaround for Firebird has been removed now that the root cause of it has been fixed by this commit.
  • Loading branch information...
1 parent 2ebf741 commit b2565ebfbb5f310d92efd57464279a1b4d3fe136 @julian-maughan julian-maughan committed Feb 23, 2012
@@ -5,7 +5,7 @@ namespace NHibernate.Test.NHSpecificTest.NH3010
{
public class FixtureWithBatcher : BugTestCase
{
- protected override void Configure(Cfg.Configuration configuration)
+ protected override void Configure(Configuration configuration)
{
configuration.DataBaseIntegration(x =>
{
@@ -15,34 +15,26 @@ protected override void Configure(Cfg.Configuration configuration)
protected override void OnSetUp()
{
- base.OnSetUp();
-
using (ISession session = OpenSession())
+ using (ITransaction tx = session.BeginTransaction())
{
- using (ITransaction tx = session.BeginTransaction())
- {
- var parent = new Parent();
- var childOne = new Child();
- parent.Childs.Add(childOne);
- session.Save(parent);
-
- tx.Commit();
- }
+ var parent = new Parent();
+ var childOne = new Child();
+ parent.Childs.Add(childOne);
+ session.Save(parent);
+
+ tx.Commit();
}
}
protected override void OnTearDown()
{
- base.OnTearDown();
-
using (ISession session = OpenSession())
+ using (ITransaction tx = session.BeginTransaction())
{
- using (ITransaction tx = session.BeginTransaction())
- {
- session.Delete("from Child");
- session.Delete("from Parent");
- tx.Commit();
- }
+ session.Delete("from Child");
+ session.Delete("from Parent");
+ tx.Commit();
}
}
@@ -51,25 +43,23 @@ protected override void OnTearDown()
public void DisposedCommandShouldNotBeReusedAfterRemoveAtAndInsert()
{
using (ISession session = OpenSession())
+ using (ITransaction tx = session.BeginTransaction())
{
- using (ITransaction tx = session.BeginTransaction())
- {
- var parent = session.CreateCriteria<Parent>().UniqueResult<Parent>();
+ var parent = session.CreateCriteria<Parent>().UniqueResult<Parent>();
- Child childOne = parent.Childs[0];
+ Child childOne = parent.Childs[0];
- var childTwo = new Child();
- parent.Childs.Add(childTwo);
+ var childTwo = new Child();
+ parent.Childs.Add(childTwo);
- Child childToMove = parent.Childs[1];
- parent.Childs.RemoveAt(1);
- parent.Childs.Insert(0, childToMove);
+ Child childToMove = parent.Childs[1];
+ parent.Childs.RemoveAt(1);
+ parent.Childs.Insert(0, childToMove);
- Assert.DoesNotThrow(() => { tx.Commit(); });
+ Assert.DoesNotThrow(tx.Commit);
- Assert.AreEqual(childTwo.Id, parent.Childs[0].Id);
- Assert.AreEqual(childOne.Id, parent.Childs[1].Id);
- }
+ Assert.AreEqual(childTwo.Id, parent.Childs[0].Id);
+ Assert.AreEqual(childOne.Id, parent.Childs[1].Id);
}
}
@@ -78,24 +68,22 @@ public void DisposedCommandShouldNotBeReusedAfterRemoveAtAndInsert()
public void DisposedCommandShouldNotBeReusedAfterClearAndAdd()
{
using (ISession session = OpenSession())
+ using (ITransaction tx = session.BeginTransaction())
{
- using (ITransaction tx = session.BeginTransaction())
- {
- var parent = session.CreateCriteria<Parent>().UniqueResult<Parent>();
+ var parent = session.CreateCriteria<Parent>().UniqueResult<Parent>();
- parent.Childs.Clear();
+ parent.Childs.Clear();
- var childOne = new Child();
- parent.Childs.Add(childOne);
+ var childOne = new Child();
+ parent.Childs.Add(childOne);
- var childTwo = new Child();
- parent.Childs.Add(childTwo);
+ var childTwo = new Child();
+ parent.Childs.Add(childTwo);
- Assert.DoesNotThrow(() => { tx.Commit(); });
+ Assert.DoesNotThrow(tx.Commit);
- Assert.AreEqual(childOne.Id, parent.Childs[0].Id);
- Assert.AreEqual(childTwo.Id, parent.Childs[1].Id);
- }
+ Assert.AreEqual(childOne.Id, parent.Childs[0].Id);
+ Assert.AreEqual(childTwo.Id, parent.Childs[1].Id);
}
}
}
@@ -6,7 +6,7 @@ namespace NHibernate.Test.NHSpecificTest.NH3010
{
public class FixtureWithNoBatcher : BugTestCase
{
- protected override void Configure(Cfg.Configuration configuration)
+ protected override void Configure(Configuration configuration)
{
configuration.DataBaseIntegration(x =>
{
@@ -17,34 +17,26 @@ protected override void Configure(Cfg.Configuration configuration)
protected override void OnSetUp()
{
- base.OnSetUp();
-
using (ISession session = OpenSession())
+ using (ITransaction tx = session.BeginTransaction())
{
- using (ITransaction tx = session.BeginTransaction())
- {
- var parent = new Parent();
- var childOne = new Child();
- parent.Childs.Add(childOne);
- session.Save(parent);
-
- tx.Commit();
- }
+ var parent = new Parent();
+ var childOne = new Child();
+ parent.Childs.Add(childOne);
+ session.Save(parent);
+
+ tx.Commit();
}
}
protected override void OnTearDown()
{
- base.OnTearDown();
-
using (ISession session = OpenSession())
+ using (ITransaction tx = session.BeginTransaction())
{
- using (ITransaction tx = session.BeginTransaction())
- {
- session.Delete("from Child");
- session.Delete("from Parent");
- tx.Commit();
- }
+ session.Delete("from Child");
+ session.Delete("from Parent");
+ tx.Commit();
}
}
@@ -53,25 +45,23 @@ protected override void OnTearDown()
public void DisposedCommandShouldNotBeReusedAfterRemoveAtAndInsert()
{
using (ISession session = OpenSession())
+ using (ITransaction tx = session.BeginTransaction())
{
- using (ITransaction tx = session.BeginTransaction())
- {
- var parent = session.CreateCriteria<Parent>().UniqueResult<Parent>();
+ var parent = session.CreateCriteria<Parent>().UniqueResult<Parent>();
- Child childOne = parent.Childs[0];
+ Child childOne = parent.Childs[0];
- var childTwo = new Child();
- parent.Childs.Add(childTwo);
+ var childTwo = new Child();
+ parent.Childs.Add(childTwo);
- Child childToMove = parent.Childs[1];
- parent.Childs.RemoveAt(1);
- parent.Childs.Insert(0, childToMove);
+ Child childToMove = parent.Childs[1];
+ parent.Childs.RemoveAt(1);
+ parent.Childs.Insert(0, childToMove);
- Assert.DoesNotThrow(() => { tx.Commit(); });
+ Assert.DoesNotThrow(tx.Commit);
- Assert.AreEqual(childTwo.Id, parent.Childs[0].Id);
- Assert.AreEqual(childOne.Id, parent.Childs[1].Id);
- }
+ Assert.That(childTwo.Id, Is.EqualTo(parent.Childs[0].Id));
+ Assert.That(childOne.Id, Is.EqualTo(parent.Childs[1].Id));
}
}
@@ -80,24 +70,22 @@ public void DisposedCommandShouldNotBeReusedAfterRemoveAtAndInsert()
public void DisposedCommandShouldNotBeReusedAfterClearAndAdd()
{
using (ISession session = OpenSession())
+ using (ITransaction tx = session.BeginTransaction())
{
- using (ITransaction tx = session.BeginTransaction())
- {
- var parent = session.CreateCriteria<Parent>().UniqueResult<Parent>();
+ var parent = session.CreateCriteria<Parent>().UniqueResult<Parent>();
- parent.Childs.Clear();
+ parent.Childs.Clear();
- var childOne = new Child();
- parent.Childs.Add(childOne);
+ var childOne = new Child();
+ parent.Childs.Add(childOne);
- var childTwo = new Child();
- parent.Childs.Add(childTwo);
+ var childTwo = new Child();
+ parent.Childs.Add(childTwo);
- Assert.DoesNotThrow(() => { tx.Commit(); });
+ Assert.DoesNotThrow(tx.Commit);
- Assert.AreEqual(childOne.Id, parent.Childs[0].Id);
- Assert.AreEqual(childTwo.Id, parent.Childs[1].Id);
- }
+ Assert.That(childOne.Id, Is.EqualTo(parent.Childs[0].Id));
+ Assert.That(childTwo.Id, Is.EqualTo(parent.Childs[1].Id));
}
}
}
@@ -120,16 +120,7 @@ protected void Prepare(IDbCommand cmd)
public virtual IDbCommand PrepareBatchCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
{
- /* NH:
- * The code inside this block was added for a strange behaviour
- * discovered using Firebird (some times for us is external issue).
- * The problem is that batchCommandSql as a value, batchCommand is not null
- * BUT batchCommand.CommandText is null (I don't know who clear it)
- */
- bool forceCommandRecreate = _batchCommand == null || string.IsNullOrEmpty(_batchCommand.CommandText);
- /****************************************/
- if (sql.Equals(_batchCommandSql) &&
- ArrayHelper.ArrayEquals(parameterTypes, _batchCommandParameterTypes) && !forceCommandRecreate)
+ if (sql.Equals(_batchCommandSql) && ArrayHelper.ArrayEquals(parameterTypes, _batchCommandParameterTypes))
{
if (Log.IsDebugEnabled)
{
@@ -353,11 +344,11 @@ public void CloseCommand(IDbCommand st, IDataReader reader)
public void CloseReader(IDataReader reader)
{
- /* This method was added because PrepareCommand don't really prepare the command
- * with its connection.
- * In some case we need to manage a reader outsite the command scope.
- * To do it we need to use the Batcher.ExecuteReader and then we need something
- * to close the opened reader.
+ /* This method was added because PrepareCommand don't really prepare the command
+ * with its connection.
+ * In some case we need to manage a reader outsite the command scope.
+ * To do it we need to use the Batcher.ExecuteReader and then we need something
+ * to close the opened reader.
*/
// TODO NH: Study a way to use directly IDbCommand.ExecuteReader() outsite the batcher
// An example of it's use is the management of generated ID.
@@ -290,8 +290,8 @@ protected override int DoUpdateRows(object id, IPersistentCollection collection,
throw;
}
finally
- {
- if (st != null)
+ {
+ if (!useBatch && st != null)
{
session.Batcher.CloseCommand(st, null);
}

0 comments on commit b2565eb

Please sign in to comment.