Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save mcintyre321/1175496 to your computer and use it in GitHub Desktop.

Select an option

Save mcintyre321/1175496 to your computer and use it in GitHub Desktop.
ProfiledSql2008ClientDriver.BatchFix.cs
public partial class ProfiledSql2008ClientDriver : IEmbeddedBatcherFactoryProvider
{
System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass
{
get { return typeof(ProfiledSqlClientBatchingBatcherFactory); }
}
}
public class ProfiledSqlClientBatchingBatcherFactory : SqlClientBatchingBatcherFactory
{
public override NHibernate.Engine.IBatcher CreateBatcher(ConnectionManager connectionManager, NHibernate.IInterceptor interceptor)
{
return new ProfiledSqlClientBatchingBatcher(connectionManager, interceptor);
}
}
public class ProfiledSqlClientBatchingBatcher : AbstractBatcher
{
private int batchSize;
private int totalExpectedRowsAffected;
private SqlClientSqlCommandSet currentBatch;
private StringBuilder currentBatchCommandsLog;
private readonly int defaultTimeout;
public ProfiledSqlClientBatchingBatcher(ConnectionManager connectionManager, NHibernate.IInterceptor interceptor)
: base(connectionManager, interceptor)
{
batchSize = Factory.Settings.AdoBatchSize;
defaultTimeout = PropertiesHelper.GetInt32(NHibernate.Cfg.Environment.CommandTimeout, NHibernate.Cfg.Environment.Properties, -1);
currentBatch = CreateConfiguredBatch();
//we always create this, because we need to deal with a scenario in which
//the user change the logging configuration at runtime. Trying to put this
//behind an if(log.IsDebugEnabled) will cause a null reference exception
//at that point.
currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
}
public override int BatchSize
{
get { return batchSize; }
set { batchSize = value; }
}
protected override int CountOfStatementsInCurrentBatch
{
get { return currentBatch.CountOfCommands; }
}
public override void AddToBatch(IExpectation expectation)
{
totalExpectedRowsAffected += expectation.ExpectedRowCount;
IDbCommand batchUpdate = CurrentCommand;
Driver.AdjustCommand(batchUpdate);
string lineWithParameters = null;
var sqlStatementLogger = Factory.Settings.SqlStatementLogger;
if (sqlStatementLogger.IsDebugEnabled || log.IsDebugEnabled)
{
lineWithParameters = sqlStatementLogger.GetCommandLineWithParameters(batchUpdate);
var formatStyle = sqlStatementLogger.DetermineActualStyle(FormatStyle.Basic);
lineWithParameters = formatStyle.Formatter.Format(lineWithParameters);
currentBatchCommandsLog.Append("command ")
.Append(currentBatch.CountOfCommands)
.Append(":")
.AppendLine(lineWithParameters);
}
if (log.IsDebugEnabled)
{
log.Debug("Adding to batch:" + lineWithParameters);
}
if (batchUpdate is System.Data.SqlClient.SqlCommand)
currentBatch.Append((System.Data.SqlClient.SqlCommand)batchUpdate);
else
{
var sqlCommand = new System.Data.SqlClient.SqlCommand(
batchUpdate.CommandText,
(SqlConnection)batchUpdate.Connection,
(SqlTransaction)batchUpdate.Transaction);
foreach (SqlParameter p in batchUpdate.Parameters)
{
sqlCommand.Parameters.Add(
new SqlParameter(
p.ParameterName,
p.SqlDbType,
p.Size,
p.Direction,
p.IsNullable,
p.Precision,
p.Scale,
p.SourceColumn,
p.SourceVersion,
p.Value));
}
currentBatch.Append(sqlCommand);
}
if (currentBatch.CountOfCommands >= batchSize)
{
ExecuteBatchWithTiming(batchUpdate);
}
}
protected override void DoExecuteBatch(IDbCommand ps)
{
log.DebugFormat("Executing batch");
CheckReaders();
Prepare(currentBatch.BatchCommand);
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
Factory.Settings.SqlStatementLogger.LogBatchCommand(currentBatchCommandsLog.ToString());
currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
}
int rowsAffected;
try
{
rowsAffected = currentBatch.ExecuteNonQuery();
}
catch (DbException e)
{
throw ADOExceptionHelper.Convert(Factory.SQLExceptionConverter, e, "could not execute batch command.");
}
Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected);
currentBatch.Dispose();
totalExpectedRowsAffected = 0;
currentBatch = CreateConfiguredBatch();
}
private SqlClientSqlCommandSet CreateConfiguredBatch()
{
var result = new SqlClientSqlCommandSet();
if (defaultTimeout > 0)
{
try
{
result.CommandTimeout = defaultTimeout;
}
catch (Exception e)
{
if (log.IsWarnEnabled)
{
log.Warn(e.ToString());
}
}
}
return result;
}
}
@longda
Copy link
Copy Markdown

longda commented Sep 8, 2011

Hi, sorry for the stupid question but I can't resolve the Driver.AdjustCommand(batchUpdate); on line 52. Could you tell me where that is coming from? Is that an NHibernate library?

@mcintyre321
Copy link
Copy Markdown
Author

Its a property on the base class, AbstractBatcher. I'm running the latest NH off Nuget so maybe it's not in your version?

@longda
Copy link
Copy Markdown

longda commented Sep 9, 2011

Ah, that makes sense! Unfortunately, I'm on an older version. Thanks again for this!

@mcintyre321
Copy link
Copy Markdown
Author

mcintyre321 commented Sep 9, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment