1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.ActiveRecord
.Tests
18 using System
.Data
.SqlClient
;
20 using Castle
.ActiveRecord
.Framework
;
21 using Castle
.ActiveRecord
.Framework
.Scopes
;
22 using Castle
.ActiveRecord
.Tests
.Model
;
23 using Castle
.Core
.Configuration
;
27 using NUnit
.Framework
;
30 public class DifferentDatabaseScopeTestCase
: AbstractActiveRecordTest
37 ActiveRecordStarter
.Initialize( GetConfigSource(),
38 typeof(Blog
), typeof(Post
),
39 typeof(OtherDbBlog
), typeof(OtherDbPost
), typeof(Test2ARBase
) );
44 [Test
, Category("mssql")]
45 public void SimpleCase()
47 Blog blog
= new Blog();
48 blog
.Name
= "hammett's blog";
49 blog
.Author
= "hamilton verissimo";
52 SqlConnection conn
= CreateSqlConnection();
58 using(new DifferentDatabaseScope(conn
))
64 Blog
[] blogs
= Blog
.FindAll();
65 Assert
.IsNotNull( blogs
);
66 Assert
.AreEqual( 1, blogs
.Length
);
68 OtherDbBlog
[] blogs2
= OtherDbBlog
.FindAll();
69 Assert
.IsNotNull( blogs2
);
70 Assert
.AreEqual( 1, blogs2
.Length
);
73 [Test
, Category("mssql")]
74 public void UsingSessionScope()
76 ISession session1
, session2
;
78 using(new SessionScope())
80 Blog blog
= new Blog();
81 blog
.Name
= "hammett's blog";
82 blog
.Author
= "hamilton verissimo";
85 session1
= blog
.CurrentSession
;
86 Assert
.IsNotNull(session1
);
88 SqlConnection conn
= CreateSqlConnection();
94 using(new DifferentDatabaseScope(conn
))
98 session2
= blog
.CurrentSession
;
99 Assert
.IsNotNull(session2
);
101 Assert
.IsFalse( Object
.ReferenceEquals(session1
, session2
) );
106 Blog
[] blogs
= Blog
.FindAll();
107 Assert
.IsNotNull( blogs
);
108 Assert
.AreEqual( 1, blogs
.Length
);
110 OtherDbBlog
[] blogs2
= OtherDbBlog
.FindAll();
111 Assert
.IsNotNull( blogs2
);
112 Assert
.AreEqual( 1, blogs2
.Length
);
115 [Test
, Category("mssql")]
116 public void UsingTransactionScope()
118 SqlConnection conn
= CreateSqlConnection();
119 ISession session1
, session2
;
120 ITransaction trans1
, trans2
;
122 using(new TransactionScope())
124 Blog blog
= new Blog();
125 blog
.Name
= "hammett's blog";
126 blog
.Author
= "hamilton verissimo";
129 session1
= blog
.CurrentSession
;
130 trans1
= blog
.CurrentSession
.Transaction
;
131 Assert
.IsNotNull(session1
);
132 Assert
.IsNotNull(session1
.Transaction
);
133 Assert
.IsFalse(session1
.Transaction
.WasCommitted
);
134 Assert
.IsFalse(session1
.Transaction
.WasRolledBack
);
138 using(new DifferentDatabaseScope(conn
))
142 session2
= blog
.CurrentSession
;
143 trans2
= blog
.CurrentSession
.Transaction
;
144 Assert
.IsNotNull(session2
);
146 Assert
.IsFalse( Object
.ReferenceEquals(session1
, session2
) );
148 Assert
.IsNotNull(session2
.Transaction
);
149 Assert
.IsFalse(session2
.Transaction
.WasCommitted
);
150 Assert
.IsFalse(session2
.Transaction
.WasRolledBack
);
154 Assert
.IsFalse(session1
.IsOpen
);
155 Assert
.IsFalse(session2
.IsOpen
);
156 Assert
.IsTrue(trans1
.WasCommitted
);
157 Assert
.IsFalse(trans1
.WasRolledBack
);
158 Assert
.IsTrue(trans2
.WasCommitted
);
159 Assert
.IsFalse(trans2
.WasRolledBack
);
161 Blog
[] blogs
= Blog
.FindAll();
162 Assert
.IsNotNull( blogs
);
163 Assert
.AreEqual( 1, blogs
.Length
);
165 OtherDbBlog
[] blogs2
= OtherDbBlog
.FindAll();
166 Assert
.IsNotNull( blogs2
);
167 Assert
.AreEqual( 1, blogs2
.Length
);
170 [Test
, Category("mssql")]
171 public void UsingTransactionScopeWithRollback()
173 SqlConnection conn
= CreateSqlConnection();
174 ISession session1
, session2
;
175 ITransaction trans1
, trans2
;
177 using(TransactionScope scope
= new TransactionScope())
179 Blog blog
= new Blog();
180 blog
.Name
= "hammett's blog";
181 blog
.Author
= "hamilton verissimo";
184 session1
= blog
.CurrentSession
;
185 trans1
= blog
.CurrentSession
.Transaction
;
186 Assert
.IsNotNull(session1
);
187 Assert
.IsNotNull(session1
.Transaction
);
188 Assert
.IsFalse(session1
.Transaction
.WasCommitted
);
189 Assert
.IsFalse(session1
.Transaction
.WasRolledBack
);
193 using(new DifferentDatabaseScope(conn
))
197 session2
= blog
.CurrentSession
;
198 trans2
= blog
.CurrentSession
.Transaction
;
199 Assert
.IsNotNull(session2
);
201 Assert
.IsFalse( Object
.ReferenceEquals(session1
, session2
) );
203 Assert
.IsNotNull(session2
.Transaction
);
204 Assert
.IsFalse(session2
.Transaction
.WasCommitted
);
205 Assert
.IsFalse(session2
.Transaction
.WasRolledBack
);
207 scope
.VoteRollBack();
211 Assert
.IsFalse(session1
.IsOpen
);
212 Assert
.IsFalse(session2
.IsOpen
);
213 Assert
.IsFalse(trans1
.WasCommitted
);
214 Assert
.IsTrue(trans1
.WasRolledBack
);
215 Assert
.IsFalse(trans2
.WasCommitted
);
216 Assert
.IsTrue(trans2
.WasRolledBack
);
218 Blog
[] blogs
= Blog
.FindAll();
219 Assert
.IsNotNull( blogs
);
220 Assert
.AreEqual( 0, blogs
.Length
);
222 OtherDbBlog
[] blogs2
= OtherDbBlog
.FindAll();
223 Assert
.IsNotNull( blogs2
);
224 Assert
.AreEqual( 0, blogs2
.Length
);
227 [Test
, Category("mssql")]
228 public void MoreThanOneConnectionWithinTheSessionScope()
230 SqlConnection conn
= CreateSqlConnection();
231 SqlConnection conn2
= CreateSqlConnection2();
233 using(new SessionScope())
235 foreach(SqlConnection connection
in new SqlConnection
[] { conn, conn2 }
)
239 using(new DifferentDatabaseScope(connection
))
241 Blog blog
= new Blog();
242 blog
.Name
= "hammett's blog";
243 blog
.Author
= "hamilton verissimo";
249 OtherDbBlog
[] blogs2
= OtherDbBlog
.FindAll();
250 Assert
.IsNotNull( blogs2
);
251 Assert
.AreEqual( 1, blogs2
.Length
);
253 Blog
[] blogs
= Blog
.FindAll();
254 Assert
.IsNotNull( blogs
);
255 Assert
.AreEqual( 1, blogs
.Length
);
258 [Test
, Category("mssql")]
259 public void UsingSessionAndTransactionScope()
261 SqlConnection conn
= CreateSqlConnection();
262 ISession session1
, session2
;
264 ITransaction trans1
, trans2
;
265 using(new SessionScope())
267 using(TransactionScope scope
= new TransactionScope())
269 Blog blog
= new Blog();
270 blog
.Name
= "hammett's blog";
271 blog
.Author
= "hamilton verissimo";
274 session1
= blog
.CurrentSession
;
275 Assert
.IsNotNull(session1
);
276 trans1
= session1
.Transaction
;
277 Assert
.IsNotNull(trans1
);
278 Assert
.IsFalse(trans1
.WasCommitted
);
279 Assert
.IsFalse(trans1
.WasRolledBack
);
283 using(new DifferentDatabaseScope(conn
))
286 blog
.Name
= "hammett's blog";
287 blog
.Author
= "hamilton verissimo";
290 session2
= blog
.CurrentSession
;
291 Assert
.IsNotNull(session2
);
292 trans2
= session2
.Transaction
;
293 Assert
.IsFalse( Object
.ReferenceEquals(session1
, session2
) );
295 Assert
.IsNotNull(session2
.Transaction
);
296 Assert
.IsFalse(session2
.Transaction
.WasCommitted
);
297 Assert
.IsFalse(session2
.Transaction
.WasRolledBack
);
300 using(new DifferentDatabaseScope(conn
))
303 blog
.Name
= "hammett's blog";
304 blog
.Author
= "hamilton verissimo";
307 session2
= blog
.CurrentSession
;
308 Assert
.IsNotNull(session2
);
310 Assert
.IsFalse( Object
.ReferenceEquals(session1
, session2
) );
312 Assert
.IsNotNull(session2
.Transaction
);
313 Assert
.IsFalse(session2
.Transaction
.WasCommitted
);
314 Assert
.IsFalse(session2
.Transaction
.WasRolledBack
);
319 Assert
.IsFalse(session1
.IsOpen
);
320 Assert
.IsFalse(session2
.IsOpen
);
321 Assert
.IsTrue(trans1
.WasCommitted
);
322 Assert
.IsFalse(trans1
.WasRolledBack
);
323 Assert
.IsTrue(trans2
.WasCommitted
);
324 Assert
.IsFalse(session2
.Transaction
.WasRolledBack
);
326 Blog
[] blogs
= Blog
.FindAll();
327 Assert
.IsNotNull( blogs
);
328 Assert
.AreEqual( 1, blogs
.Length
);
330 OtherDbBlog
[] blogs2
= OtherDbBlog
.FindAll();
331 Assert
.IsNotNull( blogs2
);
332 Assert
.AreEqual( 2, blogs2
.Length
);
335 [Test
, Category("mssql")]
336 public void SequenceOfTransactions()
338 SqlConnection conn
= CreateSqlConnection();
339 ISession session1
, session2
;
341 ITransaction trans1
, trans2
;
342 using(new SessionScope())
344 using(TransactionScope scope
= new TransactionScope())
346 Blog blog
= new Blog();
347 blog
.Name
= "hammett's blog";
348 blog
.Author
= "hamilton verissimo";
351 session1
= blog
.CurrentSession
;
352 Assert
.IsNotNull(session1
);
353 trans1
= session1
.Transaction
;
354 Assert
.IsNotNull(trans1
);
355 Assert
.IsFalse(trans1
.WasCommitted
);
356 Assert
.IsFalse(trans1
.WasRolledBack
);
360 using(new DifferentDatabaseScope(conn
))
363 blog
.Name
= "hammett's blog";
364 blog
.Author
= "hamilton verissimo";
367 session2
= blog
.CurrentSession
;
368 Assert
.IsNotNull(session2
);
369 trans2
= session2
.Transaction
;
370 Assert
.IsFalse( Object
.ReferenceEquals(session1
, session2
) );
372 Assert
.IsNotNull(session2
.Transaction
);
373 Assert
.IsFalse(session2
.Transaction
.WasCommitted
);
374 Assert
.IsFalse(session2
.Transaction
.WasRolledBack
);
377 using(new DifferentDatabaseScope(conn
))
380 blog
.Name
= "hammett's blog";
381 blog
.Author
= "hamilton verissimo";
384 session2
= blog
.CurrentSession
;
385 Assert
.IsNotNull(session2
);
387 Assert
.IsFalse( Object
.ReferenceEquals(session1
, session2
) );
389 Assert
.IsNotNull(trans2
);
390 Assert
.IsFalse(trans2
.WasCommitted
);
391 Assert
.IsFalse(trans2
.WasRolledBack
);
397 using(TransactionScope scope
= new TransactionScope())
399 Blog blog
= new Blog();
400 blog
.Name
= "another blog";
401 blog
.Author
= "erico verissimo";
406 Assert
.IsFalse(session1
.IsOpen
);
407 Assert
.IsFalse(session2
.IsOpen
);
408 Assert
.IsTrue(trans1
.WasCommitted
);
409 Assert
.IsFalse(trans1
.WasRolledBack
);
410 Assert
.IsTrue(trans2
.WasCommitted
);
411 Assert
.IsFalse(trans2
.WasRolledBack
);
413 Blog
[] blogs
= Blog
.FindAll();
414 Assert
.IsNotNull( blogs
);
415 Assert
.AreEqual( 2, blogs
.Length
);
417 OtherDbBlog
[] blogs2
= OtherDbBlog
.FindAll();
418 Assert
.IsNotNull( blogs2
);
419 Assert
.AreEqual( 2, blogs2
.Length
);
422 private SqlConnection
CreateSqlConnection()
424 IConfigurationSource config
= GetConfigSource();
426 IConfiguration db2
= config
.GetConfiguration( typeof(Test2ARBase
) );
428 SqlConnection conn
= null;
430 foreach(IConfiguration child
in db2
.Children
)
432 if (child
.Name
== "hibernate.connection.connection_string")
434 conn
= new SqlConnection(child
.Value
);
441 private SqlConnection
CreateSqlConnection2()
443 IConfigurationSource config
= GetConfigSource();
445 IConfiguration db2
= config
.GetConfiguration( typeof(ActiveRecordBase
) );
447 SqlConnection conn
= null;
449 foreach(IConfiguration child
in db2
.Children
)
451 if (child
.Name
== "hibernate.connection.connection_string")
453 conn
= new SqlConnection(child
.Value
);