|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Data; |
| 4 | +using NHibernate.Cfg; |
| 5 | +using NHibernate.Util; |
| 6 | +using NUnit.Framework; |
| 7 | +using Environment=NHibernate.Cfg.Environment; |
| 8 | + |
| 9 | +namespace NHibernate.Test.ConnectionTest |
| 10 | +{ |
| 11 | + [TestFixture] |
| 12 | + public class AggressiveReleaseTest : ConnectionManagementTestCase |
| 13 | + { |
| 14 | + protected override void Configure(Configuration cfg) |
| 15 | + { |
| 16 | + base.Configure(cfg); |
| 17 | + cfg.SetProperty(Environment.ReleaseConnections, "after_transaction"); |
| 18 | + //cfg.SetProperty(Environment.ConnectionProvider, typeof(DummyConnectionProvider).AssemblyQualifiedName); |
| 19 | + //cfg.SetProperty(Environment.GenerateStatistics, "true"); |
| 20 | + cfg.SetProperty(Environment.BatchSize, "0"); |
| 21 | + } |
| 22 | + |
| 23 | + protected override ISession GetSessionUnderTest() |
| 24 | + { |
| 25 | + return OpenSession(); |
| 26 | + } |
| 27 | + |
| 28 | + protected void Reconnect(ISession session) |
| 29 | + { |
| 30 | + session.Reconnect(); |
| 31 | + } |
| 32 | + |
| 33 | + protected void Prepare() |
| 34 | + { |
| 35 | + //DummyTransactionManager.INSTANCE.Begin(); |
| 36 | + } |
| 37 | + |
| 38 | + protected void Done() |
| 39 | + { |
| 40 | + //DummyTransactionManager.INSTANCE.Commit(); |
| 41 | + } |
| 42 | + |
| 43 | + // Some additional tests specifically for the aggressive-Release functionality... |
| 44 | + |
| 45 | + [Test] |
| 46 | + public void SerializationOnAfterStatementAggressiveRelease() |
| 47 | + { |
| 48 | + Prepare(); |
| 49 | + ISession s = GetSessionUnderTest(); |
| 50 | + Silly silly = new Silly("silly"); |
| 51 | + s.Save(silly); |
| 52 | + |
| 53 | + // this should cause the CM to obtain a connection, and then Release it |
| 54 | + s.Flush(); |
| 55 | + |
| 56 | + // We should be able to serialize the session at this point... |
| 57 | + SerializationHelper.Serialize(s); |
| 58 | + |
| 59 | + s.Delete(silly); |
| 60 | + s.Flush(); |
| 61 | + |
| 62 | + Release(s); |
| 63 | + Done(); |
| 64 | + } |
| 65 | + |
| 66 | + [Test] |
| 67 | + public void SerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources() |
| 68 | + { |
| 69 | + Prepare(); |
| 70 | + ISession s = GetSessionUnderTest(); |
| 71 | + |
| 72 | + Silly silly = new Silly("silly"); |
| 73 | + s.Save(silly); |
| 74 | + |
| 75 | + // this should cause the CM to obtain a connection, and then Release it |
| 76 | + s.Flush(); |
| 77 | + |
| 78 | + // both scroll() and iterate() cause the batcher to hold on |
| 79 | + // to resources, which should make aggresive-Release not Release |
| 80 | + // the connection (and thus cause serialization to fail) |
| 81 | + IEnumerable en = s.CreateQuery("from Silly").Enumerable(); |
| 82 | + |
| 83 | + try |
| 84 | + { |
| 85 | + SerializationHelper.Serialize(s); |
| 86 | + Assert.Fail("Serialization allowed on connected session; or aggressive Release released connection with open resources"); |
| 87 | + } |
| 88 | + catch (InvalidOperationException e) |
| 89 | + { |
| 90 | + // expected behavior |
| 91 | + } |
| 92 | + |
| 93 | + // Closing the ScrollableResults does currently force the batcher to |
| 94 | + // aggressively Release the connection |
| 95 | + NHibernateUtil.Close(en); |
| 96 | + SerializationHelper.Serialize(s); |
| 97 | + |
| 98 | + s.Delete(silly); |
| 99 | + s.Flush(); |
| 100 | + |
| 101 | + Release(s); |
| 102 | + Done(); |
| 103 | + } |
| 104 | + |
| 105 | + [Test] |
| 106 | + public void QueryIteration() |
| 107 | + { |
| 108 | + Prepare(); |
| 109 | + ISession s = GetSessionUnderTest(); |
| 110 | + Silly silly = new Silly("silly"); |
| 111 | + s.Save(silly); |
| 112 | + s.Flush(); |
| 113 | + |
| 114 | + IEnumerable en = s.CreateQuery("from Silly").Enumerable(); |
| 115 | + IEnumerator itr = en.GetEnumerator(); |
| 116 | + Assert.IsTrue(itr.MoveNext()); |
| 117 | + Silly silly2 = (Silly) itr.Current; |
| 118 | + Assert.AreEqual(silly, silly2); |
| 119 | + NHibernateUtil.Close(itr); |
| 120 | + |
| 121 | + itr = s.CreateQuery("from Silly").Enumerable().GetEnumerator(); |
| 122 | + IEnumerator itr2 = s.CreateQuery("from Silly where name = 'silly'").Enumerable().GetEnumerator(); |
| 123 | + |
| 124 | + Assert.IsTrue(itr.MoveNext()); |
| 125 | + Assert.AreEqual(silly, itr.Current); |
| 126 | + Assert.IsTrue(itr2.MoveNext()); |
| 127 | + Assert.AreEqual(silly, itr2.Current); |
| 128 | + |
| 129 | + NHibernateUtil.Close(itr); |
| 130 | + NHibernateUtil.Close(itr2); |
| 131 | + |
| 132 | + s.Delete(silly); |
| 133 | + s.Flush(); |
| 134 | + |
| 135 | + Release(s); |
| 136 | + Done(); |
| 137 | + } |
| 138 | + |
| 139 | + //[Test] |
| 140 | + //public void QueryScrolling() |
| 141 | + //{ |
| 142 | + // Prepare(); |
| 143 | + // ISession s = GetSessionUnderTest(); |
| 144 | + // Silly silly = new Silly("silly"); |
| 145 | + // s.Save(silly); |
| 146 | + // s.Flush(); |
| 147 | + |
| 148 | + // ScrollableResults sr = s.CreateQuery("from Silly").scroll(); |
| 149 | + // Assert.IsTrue(sr.next()); |
| 150 | + // Silly silly2 = (Silly) sr.get(0); |
| 151 | + // Assert.AreEqual(silly, silly2); |
| 152 | + // sr.Close(); |
| 153 | + |
| 154 | + // sr = s.CreateQuery("from Silly").Scroll(); |
| 155 | + // ScrollableResults sr2 = s.CreateQuery("from Silly where name = 'silly'").Scroll(); |
| 156 | + |
| 157 | + // Assert.IsTrue(sr.next()); |
| 158 | + // Assert.AreEqual(silly, sr.get(0)); |
| 159 | + // Assert.IsTrue(sr2.next()); |
| 160 | + // Assert.AreEqual(silly, sr2.get(0)); |
| 161 | + |
| 162 | + // sr.Close(); |
| 163 | + // sr2.Close(); |
| 164 | + |
| 165 | + // s.Delete(silly); |
| 166 | + // s.Flush(); |
| 167 | + |
| 168 | + // Release(s); |
| 169 | + // Done(); |
| 170 | + //} |
| 171 | + |
| 172 | + [Test] |
| 173 | + public void SuppliedConnection() |
| 174 | + { |
| 175 | + Prepare(); |
| 176 | + |
| 177 | + IDbConnection originalConnection = sessions.ConnectionProvider.GetConnection(); |
| 178 | + ISession session = sessions.OpenSession(originalConnection); |
| 179 | + |
| 180 | + Silly silly = new Silly("silly"); |
| 181 | + session.Save(silly); |
| 182 | + |
| 183 | + // this will cause the connection manager to cycle through the aggressive Release logic; |
| 184 | + // it should not Release the connection since we explicitly suplied it ourselves. |
| 185 | + session.Flush(); |
| 186 | + |
| 187 | + Assert.IsTrue(originalConnection == session.Connection, "Different connections"); |
| 188 | + |
| 189 | + session.Delete(silly); |
| 190 | + session.Flush(); |
| 191 | + |
| 192 | + Release(session); |
| 193 | + originalConnection.Close(); |
| 194 | + Done(); |
| 195 | + } |
| 196 | + |
| 197 | + // TODO |
| 198 | + //[Test] |
| 199 | + //public void BorrowedConnections() |
| 200 | + //{ |
| 201 | + // Prepare(); |
| 202 | + // ISession s = GetSessionUnderTest(); |
| 203 | + |
| 204 | + // IDbConnection conn = s.Connection; |
| 205 | + // Assert.IsTrue(((SessionImpl) s).ConnectionManager.HasBorrowedConnection); |
| 206 | + // conn.Close(); |
| 207 | + // Assert.IsFalse(((SessionImpl) s).ConnectionManager.HasBorrowedConnection); |
| 208 | + |
| 209 | + // Release(s); |
| 210 | + // Done(); |
| 211 | + //} |
| 212 | + |
| 213 | + [Test] |
| 214 | + public void ConnectionMaintanenceDuringFlush() |
| 215 | + { |
| 216 | + Prepare(); |
| 217 | + ISession s = GetSessionUnderTest(); |
| 218 | + s.BeginTransaction(); |
| 219 | + |
| 220 | + IList entities = new ArrayList(); |
| 221 | + for (int i = 0; i < 10; i++) |
| 222 | + { |
| 223 | + Other other = new Other("other-" + i); |
| 224 | + Silly silly = new Silly("silly-" + i, other); |
| 225 | + entities.Add(silly); |
| 226 | + s.Save(silly); |
| 227 | + } |
| 228 | + s.Flush(); |
| 229 | + |
| 230 | + foreach (Silly silly in entities) |
| 231 | + { |
| 232 | + silly.Name = "new-" + silly.Name; |
| 233 | + silly.Other.Name = "new-" + silly.Other.Name; |
| 234 | + } |
| 235 | +// long initialCount = sessions.Statistics.getConnectCount(); |
| 236 | + s.Flush(); |
| 237 | +// Assert.AreEqual(initialCount + 1, sessions.Statistics.getConnectCount(), "connection not maintained through Flush"); |
| 238 | + |
| 239 | + s.Delete("from Silly"); |
| 240 | + s.Delete("from Other"); |
| 241 | + s.Transaction.Commit(); |
| 242 | + Release(s); |
| 243 | + Done(); |
| 244 | + } |
| 245 | + } |
| 246 | +} |
0 commit comments