Skip to content

Commit

Permalink
NH-1160 - Parameter compatibility problem in cached batch SQL command.
Browse files Browse the repository at this point in the history
SVN: branches/1.2.x@3111
  • Loading branch information
Sergey Koshcheyev committed Nov 18, 2007
1 parent 0be34cb commit bfaf3e4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/NHibernate/Impl/BatcherImpl.cs
Expand Up @@ -9,6 +9,7 @@
using NHibernate.Exceptions;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
using NHibernate.Util;

namespace NHibernate.Impl
{
Expand All @@ -31,6 +32,7 @@ public abstract class BatcherImpl : IBatcher
// just update. However I haven't seen this being used with read statements...
private IDbCommand batchCommand;
private SqlString batchCommandSql;
private SqlType[] batchCommandParameterTypes;

private ISet commandsToClose = new HashedSet();
private ISet readersToClose = new HashedSet();
Expand Down Expand Up @@ -114,18 +116,20 @@ protected void Prepare(IDbCommand cmd)

public IDbCommand PrepareBatchCommand(CommandType type, SqlString sql, SqlType[] parameterTypes)
{
if (!sql.Equals(batchCommandSql))
{
batchCommand = PrepareCommand(type, sql, parameterTypes); // calls ExecuteBatch()
batchCommandSql = sql;
}
else
if (sql.Equals(batchCommandSql) &&
ArrayHelper.ArrayEquals(parameterTypes, batchCommandParameterTypes))
{
if (log.IsDebugEnabled)
{
log.Debug("reusing command " + batchCommand.CommandText);
}
}
else
{
batchCommand = PrepareCommand(type, sql, parameterTypes); // calls ExecuteBatch()
batchCommandSql = sql;
batchCommandParameterTypes = parameterTypes;
}

return batchCommand;
}
Expand Down Expand Up @@ -159,6 +163,7 @@ public void AbortBatch(Exception e)
IDbCommand cmd = batchCommand;
batchCommand = null;
batchCommandSql = null;
batchCommandParameterTypes = null;
// close the statement closeStatement(cmd)
if (cmd != null)
{
Expand Down Expand Up @@ -307,6 +312,7 @@ public void ExecuteBatch()
IDbCommand ps = batchCommand;
batchCommand = null;
batchCommandSql = null;
batchCommandParameterTypes = null;
try
{
DoExecuteBatch(ps);
Expand Down
18 changes: 18 additions & 0 deletions src/NHibernate/Util/ArrayHelper.cs
Expand Up @@ -257,5 +257,23 @@ public static int CountTrue(bool[] array)
}
return result;
}

public static bool ArrayEquals(SqlType[] a, SqlType[] b)
{
if (a.Length != b.Length)
{
return false;
}

for(int i = 0; i < a.Length; i++)
{
if (!Equals(a[i], b[i]))
{
return false;
}
}

return true;
}
}
}

0 comments on commit bfaf3e4

Please sign in to comment.