From: <fab...@us...> - 2009-05-13 22:32:16
|
Revision: 4294 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4294&view=rev Author: fabiomaulo Date: 2009-05-13 22:32:05 +0000 (Wed, 13 May 2009) Log Message: ----------- - Fix the problem created in the my previous commit. - Fixed SqlClientBatchingBatcher and now we have a test about it Modified Paths: -------------- trunk/nhibernate/src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs trunk/nhibernate/src/NHibernate.Test/LogSpy.cs trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj Added Paths: ----------- trunk/nhibernate/src/NHibernate.Test/Ado/ trunk/nhibernate/src/NHibernate.Test/Ado/BatcherFixture.cs trunk/nhibernate/src/NHibernate.Test/Ado/VerySimple.cs trunk/nhibernate/src/NHibernate.Test/Ado/VerySimple.hbm.xml Modified: trunk/nhibernate/src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs =================================================================== --- trunk/nhibernate/src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs 2009-05-13 22:07:13 UTC (rev 4293) +++ trunk/nhibernate/src/NHibernate/AdoNet/SqlClientBatchingBatcher.cs 2009-05-13 22:32:05 UTC (rev 4294) @@ -19,7 +19,10 @@ { batchSize = Factory.Settings.AdoBatchSize; currentBatch = new SqlClientSqlCommandSet(); - currentBatchCommandsLog = new StringBuilder(); + if (log.IsDebugEnabled) + { + currentBatchCommandsLog = new StringBuilder(); + } } public override int BatchSize @@ -32,20 +35,23 @@ { totalExpectedRowsAffected += expectation.ExpectedRowCount; IDbCommand batchUpdate = CurrentCommand; - - if (log.IsDebugEnabled || Factory.Settings.SqlStatementLogger.IsDebugEnabled) + if (log.IsDebugEnabled) { string lineWithParameters = Factory.Settings.SqlStatementLogger.GetCommandLineWithParameters(batchUpdate); - currentBatchCommandsLog.Append("Batch command: ").AppendLine(lineWithParameters); if (Factory.Settings.SqlStatementLogger.IsDebugEnabled) { Factory.Settings.SqlStatementLogger.LogCommand("Adding to batch:", batchUpdate, FormatStyle.Basic); } - else if (log.IsDebugEnabled) + else { log.Debug("Adding to batch:" + lineWithParameters); } + currentBatchCommandsLog.Append("Batch command: ").AppendLine(lineWithParameters); } + else + { + Factory.Settings.SqlStatementLogger.LogCommand(batchUpdate, FormatStyle.Basic); + } currentBatch.Append((System.Data.SqlClient.SqlCommand)batchUpdate); if (currentBatch.CountOfCommands >= batchSize) { @@ -58,12 +64,9 @@ log.Debug("Executing batch"); CheckReaders(); Prepare(currentBatch.BatchCommand); - if (log.IsDebugEnabled || Factory.Settings.SqlStatementLogger.IsDebugEnabled) + if (log.IsDebugEnabled) { - if (Factory.Settings.SqlStatementLogger.IsDebugEnabled) - Factory.Settings.SqlStatementLogger.LogBatchCommand(currentBatchCommandsLog.ToString()); - else if (log.IsDebugEnabled) - log.Debug(currentBatchCommandsLog.ToString()); + log.Debug(currentBatchCommandsLog.ToString()); currentBatchCommandsLog = new StringBuilder(); } int rowsAffected = currentBatch.ExecuteNonQuery(); Added: trunk/nhibernate/src/NHibernate.Test/Ado/BatcherFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Ado/BatcherFixture.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/Ado/BatcherFixture.cs 2009-05-13 22:32:05 UTC (rev 4294) @@ -0,0 +1,197 @@ +using System.Collections; +using NHibernate.AdoNet; +using NHibernate.Cfg; +using NUnit.Framework; + +namespace NHibernate.Test.Ado +{ + [TestFixture] + public class BatcherFixture: TestCase + { + protected override string MappingsAssembly + { + get { return "NHibernate.Test"; } + } + + protected override IList Mappings + { + get { return new[] { "Ado.VerySimple.hbm.xml" }; } + } + + protected override void Configure(Configuration configuration) + { + configuration.SetProperty(Environment.FormatSql, "true"); + configuration.SetProperty(Environment.GenerateStatistics, "true"); + configuration.SetProperty(Environment.BatchSize, "10"); + } + + protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory) + { + return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory); + } + + [Test] + [Description("The batcher should run all INSERT queries in only one roundtrip.")] + public void OneRoundTripInserts() + { + sessions.Statistics.Clear(); + FillDb(); + + Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1)); + Cleanup(); + } + + private void Cleanup() + { + using (ISession s = sessions.OpenSession()) + using (s.BeginTransaction()) + { + s.CreateQuery("delete from VerySimple").ExecuteUpdate(); + s.Transaction.Commit(); + } + } + + private void FillDb() + { + using (ISession s = sessions.OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + s.Save(new VerySimple {Id = 1, Name = "Fabio", Weight = 119.5}); + s.Save(new VerySimple {Id = 2, Name = "Fiamma", Weight = 9.8}); + tx.Commit(); + } + } + + [Test] + [Description("The batcher should run all UPDATE queries in only one roundtrip.")] + public void OneRoundTripUpdate() + { + FillDb(); + + using (ISession s = sessions.OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + var vs1 = s.Get<VerySimple>(1); + var vs2 = s.Get<VerySimple>(2); + vs1.Weight -= 10; + vs1.Weight -= 1; + sessions.Statistics.Clear(); + s.Update(vs1); + s.Update(vs2); + tx.Commit(); + } + + Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1)); + Cleanup(); + } + + [Test] + [Description("The batcher should run all DELETE queries in only one roundtrip.")] + public void OneRoundTripDelete() + { + FillDb(); + + using (ISession s = sessions.OpenSession()) + using (ITransaction tx = s.BeginTransaction()) + { + var vs1 = s.Get<VerySimple>(1); + var vs2 = s.Get<VerySimple>(2); + sessions.Statistics.Clear(); + s.Delete(vs1); + s.Delete(vs2); + tx.Commit(); + } + + Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1)); + Cleanup(); + } + + [Test] + [Description(@"Activating the SQL and turning off the batcher's log the log stream: +-should not contains any batch info +-should contain SQL's log info +-the batcher should work.")] + public void SqlLog() + { + using (new LogSpy(typeof(AbstractBatcher), true)) + { + using (var sl = new SqlLogSpy()) + { + sessions.Statistics.Clear(); + FillDb(); + string logs = sl.GetWholeLog(); + Assert.That(logs, Text.DoesNotContain("batch").IgnoreCase); + Assert.That(logs, Text.Contains("INSERT").IgnoreCase); + } + } + + Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1)); + Cleanup(); + } + + [Test] + [Description(@"Activating the AbstractBatcher's log the log stream: +-should not contains batch info +-should contain SQL log info only regarding batcher (SQL log should not be duplicated) +-the batcher should work.")] + public void AbstractBatcherLog() + { + using (new LogSpy(typeof(AbstractBatcher))) + { + using (var sl = new SqlLogSpy()) + { + sessions.Statistics.Clear(); + FillDb(); + string logs = sl.GetWholeLog(); + Assert.That(logs, Text.Contains("batch").IgnoreCase); + foreach (var loggingEvent in sl.Appender.GetEvents()) + { + string message = loggingEvent.RenderedMessage; + if(message.ToLowerInvariant().Contains("insert")) + { + Assert.That(message, Text.Contains("batch").IgnoreCase); + } + } + } + } + + Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1)); + Cleanup(); + } + + [Test] + [Description(@"Activating the AbstractBatcher's log the log stream: +-should contain well formatted SQL log info")] + public void AbstractBatcherLogFormattedSql() + { + using (new LogSpy(typeof(AbstractBatcher))) + { + using (var sl = new SqlLogSpy()) + { + sessions.Statistics.Clear(); + FillDb(); + foreach (var loggingEvent in sl.Appender.GetEvents()) + { + string message = loggingEvent.RenderedMessage; + if(message.StartsWith("Adding")) + { + // should be the line with the formatted SQL + var strings = message.Split(System.Environment.NewLine.ToCharArray()); + foreach (var sqlLine in strings) + { + if(sqlLine.Contains("p0")) + { + Assert.That(sqlLine, Text.Contains("p1")); + Assert.That(sqlLine, Text.Contains("p2")); + } + } + } + } + } + } + + Assert.That(sessions.Statistics.PrepareStatementCount, Is.EqualTo(1)); + Cleanup(); + } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/Ado/VerySimple.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Ado/VerySimple.cs (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/Ado/VerySimple.cs 2009-05-13 22:32:05 UTC (rev 4294) @@ -0,0 +1,9 @@ +namespace NHibernate.Test.Ado +{ + public class VerySimple + { + public virtual int Id { get; set; } + public virtual string Name { get; set; } + public virtual double Weight { get; set; } + } +} \ No newline at end of file Added: trunk/nhibernate/src/NHibernate.Test/Ado/VerySimple.hbm.xml =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Ado/VerySimple.hbm.xml (rev 0) +++ trunk/nhibernate/src/NHibernate.Test/Ado/VerySimple.hbm.xml 2009-05-13 22:32:05 UTC (rev 4294) @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8" ?> +<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" + assembly="NHibernate.Test" + namespace="NHibernate.Test.Ado"> + + <class name="VerySimple"> + <id name="Id"/> + <property name="Name"/> + <property name="Weight"/> + </class> + +</hibernate-mapping> \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/LogSpy.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/LogSpy.cs 2009-05-13 22:07:13 UTC (rev 4293) +++ trunk/nhibernate/src/NHibernate.Test/LogSpy.cs 2009-05-13 22:32:05 UTC (rev 4294) @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Text; using log4net; using log4net.Appender; using log4net.Core; @@ -12,7 +14,7 @@ private readonly Logger logger; private readonly Level prevLogLevel; - public LogSpy(ILog log) + public LogSpy(ILog log, bool disable) { logger = log.Logger as Logger; if (logger == null) @@ -22,22 +24,35 @@ // Change the log level to DEBUG and temporarily save the previous log level prevLogLevel = logger.Level; - logger.Level = Level.Debug; + logger.Level = disable ? Level.Off : Level.Debug; // Add a new MemoryAppender to the logger. appender = new MemoryAppender(); logger.AddAppender(appender); } - public LogSpy(System.Type loggerType) : this(LogManager.GetLogger(loggerType)) {} + public LogSpy(ILog log) : this(log, false) { } + public LogSpy(System.Type loggerType) : this(LogManager.GetLogger(loggerType), false) { } + public LogSpy(System.Type loggerType, bool disable) : this(LogManager.GetLogger(loggerType), disable) { } - public LogSpy(string loggerName) : this(LogManager.GetLogger(loggerName)) {} + public LogSpy(string loggerName) : this(LogManager.GetLogger(loggerName), false) { } + public LogSpy(string loggerName, bool disable) : this(LogManager.GetLogger(loggerName), disable) { } public MemoryAppender Appender { get { return appender; } } + public virtual string GetWholeLog() + { + var wholeMessage = new StringBuilder(); + foreach (LoggingEvent loggingEvent in Appender.GetEvents()) + { + wholeMessage.Append(loggingEvent.RenderedMessage); + } + return wholeMessage.ToString(); + } + #region IDisposable Members public void Dispose() Modified: trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj =================================================================== --- trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-05-13 22:07:13 UTC (rev 4293) +++ trunk/nhibernate/src/NHibernate.Test/NHibernate.Test.csproj 2009-05-13 22:32:05 UTC (rev 4294) @@ -74,6 +74,8 @@ <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> + <Compile Include="Ado\BatcherFixture.cs" /> + <Compile Include="Ado\VerySimple.cs" /> <Compile Include="Any\Address.cs" /> <Compile Include="Any\AnyTypeTest.cs" /> <Compile Include="Any\ComplexPropertyValue.cs" /> @@ -1769,6 +1771,7 @@ <EmbeddedResource Include="Cascade\JobBatch.hbm.xml" /> <EmbeddedResource Include="Deletetransient\Person.hbm.xml" /> <EmbeddedResource Include="BulkManipulation\SimpleClass.hbm.xml" /> + <EmbeddedResource Include="Ado\VerySimple.hbm.xml" /> <Content Include="DynamicEntity\package.html" /> <EmbeddedResource Include="NHSpecificTest\NH1760\Mappings.hbm.xml" /> <EmbeddedResource Include="MappingTest\Wicked.hbm.xml" /> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |