|
| 1 | +using System.Collections; |
| 2 | +using NHibernate.AdoNet; |
| 3 | +using NHibernate.Cfg; |
| 4 | +using NUnit.Framework; |
| 5 | + |
| 6 | +namespace NHibernate.Test.NHSpecificTest.NH3771 |
| 7 | +{ |
| 8 | + [TestFixture] |
| 9 | + public class Fixture : BugTestCase |
| 10 | + { |
| 11 | + protected override void Configure(Configuration configuration) |
| 12 | + { |
| 13 | + configuration.SetProperty(Environment.BatchVersionedData, "true"); |
| 14 | + configuration.SetProperty(Environment.FormatSql, "false"); |
| 15 | + configuration.SetProperty(Environment.GenerateStatistics, "true"); |
| 16 | + configuration.SetProperty(Environment.BatchSize, "10"); |
| 17 | + } |
| 18 | + |
| 19 | + protected override bool AppliesTo(Engine.ISessionFactoryImplementor factory) |
| 20 | + { |
| 21 | + return !(factory.Settings.BatcherFactory is NonBatchingBatcherFactory); |
| 22 | + } |
| 23 | + |
| 24 | + [Test] |
| 25 | + [Description("Should be two batchs with two sentences each.")] |
| 26 | + public void InsertAndUpdateWithBatch() |
| 27 | + { |
| 28 | + sessions.Statistics.Clear(); |
| 29 | + |
| 30 | + using (var sqlLog = new SqlLogSpy()) |
| 31 | + using (ISession s = sessions.OpenSession()) |
| 32 | + using (ITransaction tx = s.BeginTransaction()) |
| 33 | + { |
| 34 | + Singer vs1 = new Singer(); |
| 35 | + vs1.Id = 1; |
| 36 | + vs1.Name = "Fabrizio De Andre"; |
| 37 | + s.Save(vs1); |
| 38 | + |
| 39 | + Singer vs2 = new Singer(); |
| 40 | + vs2.Id = 2; |
| 41 | + vs2.Name = "Vinicio Capossela"; |
| 42 | + s.Save(vs2); |
| 43 | + |
| 44 | + s.Flush(); |
| 45 | + |
| 46 | + vs1.Name = "De Andre, Fabrizio"; |
| 47 | + vs2.Name = "Capossela, Vinicio"; |
| 48 | + |
| 49 | + s.Flush(); |
| 50 | + |
| 51 | + string log = sqlLog.GetWholeLog(); |
| 52 | + |
| 53 | + string[] separator = { System.Environment.NewLine }; |
| 54 | + string[] lines = log.Split(separator, System.StringSplitOptions.RemoveEmptyEntries); |
| 55 | + |
| 56 | + int batchs = 0; |
| 57 | + int sqls = 0; |
| 58 | + int batchCommands = 0; |
| 59 | + foreach (string line in lines) |
| 60 | + { |
| 61 | + if (line.StartsWith("NHibernate.SQL") && !line.StartsWith("NHibernate.SQL Batch commands:")) |
| 62 | + sqls++; |
| 63 | + |
| 64 | + if (line.StartsWith("NHibernate.SQL Batch commands:")) |
| 65 | + batchs++; |
| 66 | + |
| 67 | + if (line.StartsWith("command")) |
| 68 | + batchCommands++; |
| 69 | + } |
| 70 | + |
| 71 | + Assert.AreEqual(2, batchs); |
| 72 | + Assert.AreEqual(0, sqls); |
| 73 | + Assert.AreEqual(4, batchCommands); |
| 74 | + Assert.AreEqual(2, sessions.Statistics.PrepareStatementCount); |
| 75 | + |
| 76 | + tx.Rollback(); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments