[bcl] Enable tests for the monodroid profile.
[mono.git] / mcs / class / System.Data / Test / ProviderTests / System.Data.SqlClient / SqlTransactionTest.cs
1 //
2 // SqlTransactionTest.cs - NUnit Test Cases for testing the
3 //                          SqlTransaction class
4 // Author:
5 //      Umadevi S (sumadevi@novell.com)
6 //      Sureshkumar T (tsureshkumar@novell.com)
7 //
8 // Copyright (c) 2004 Novell Inc., and the individuals listed
9 // on the ChangeLog entries.
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System;
32 using System.Data;
33 using System.Data.Common;
34 using System.Data.SqlClient;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Data.SqlClient
39 {
40         [TestFixture]
41         [Category ("sqlserver")]
42         public class SqlTransactionTest
43         {
44                 SqlConnection conn;
45                 SqlTransaction trans;
46                 String connectionString;
47                 EngineConfig engine;
48
49                 [SetUp]
50                 public void SetUp ()
51                 {
52                         connectionString = ConnectionManager.Singleton.ConnectionString;
53                         engine = ConnectionManager.Singleton.Engine;
54                 }
55
56                 [TearDown]
57                 public void TearDown ()
58                 {
59                         if (conn != null)
60                                 conn.Dispose ();
61                         if (trans != null)
62                                 trans.Dispose ();
63                 }
64
65                 [Test]
66                 public void Commit ()
67                 {
68                         if (RunningOnMono)
69                                 Assert.Ignore ("NotWorking");
70
71                         SqlConnection connA = null;
72                         SqlConnection connB = null;
73
74                         try {
75                                 connA = new SqlConnection (connectionString);
76                                 connA.Open ();
77
78                                 connB = new SqlConnection (connectionString);
79                                 connB.Open ();
80
81                                 using (trans = connA.BeginTransaction ()) {
82                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
83                                         SqlCommand cmd = new SqlCommand (sql, connA, trans);
84                                         cmd.ExecuteNonQuery ();
85                                         cmd.Dispose ();
86
87                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connA, trans);
88                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
89                                                 Assert.IsTrue (reader.Read (), "#A1");
90                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#A2");
91                                                 Assert.IsFalse (reader.Read (), "#A3");
92                                         }
93
94                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connB);
95                                         cmd.CommandTimeout = 2;
96                                         try {
97                                                 cmd.ExecuteReader ();
98                                                 Assert.Fail ("#B1");
99                                         } catch (SqlException ex) {
100                                                 // Timeout expired.  The timeout period
101                                                 // elapsed prior to completion of the
102                                                 // operation or the server is not responding
103                                                 Assert.AreEqual (typeof (SqlException), ex.GetType (), "#B2");
104                                                 Assert.AreEqual ((byte) 11, ex.Class, "#B3");
105                                                 Assert.IsNull (ex.InnerException, "#B4");
106                                                 Assert.IsNotNull (ex.Message, "#B5");
107                                                 Assert.AreEqual (-2, ex.Number, "#B6");
108                                                 Assert.AreEqual ((byte) 0, ex.State, "#B7");
109                                         }
110
111                                         trans.Commit ();
112
113                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connB);
114                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
115                                                 Assert.IsTrue (reader.Read (), "#C1");
116                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
117                                                 Assert.IsFalse (reader.Read (), "#C3");
118                                         }
119
120                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connA);
121                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
122                                                 Assert.IsTrue (reader.Read (), "#D1");
123                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#D2");
124                                                 Assert.IsFalse (reader.Read (), "#D3");
125                                         }
126                                 }
127                         } finally {
128                                 if (connA != null)
129                                         connA.Close ();
130                                 if (connB != null)
131                                         connB.Close ();
132
133                                 conn = new SqlConnection (connectionString);
134                                 conn.Open ();
135                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
136                         }
137                 }
138
139                 [Test]
140                 public void Commit_Connection_Closed ()
141                 {
142                         try {
143                                 conn = new SqlConnection (connectionString);
144                                 conn.Open ();
145
146                                 trans = conn.BeginTransaction ();
147
148                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
149                                 SqlCommand cmd = new SqlCommand (sql, conn, trans);
150                                 cmd.ExecuteNonQuery ();
151                                 cmd.Dispose ();
152
153                                 conn.Close ();
154
155                                 try {
156                                         trans.Commit ();
157                                         Assert.Fail ("#A1");
158                                 } catch (InvalidOperationException ex) {
159                                         // This SqlTransaction has completed; it is no
160                                         // longer usable
161                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
162                                         Assert.IsNull (ex.InnerException, "#A3");
163                                         Assert.IsNotNull (ex.Message, "#A4");
164                                 }
165
166                                 conn = new SqlConnection (connectionString);
167                                 conn.Open ();
168
169                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
170                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
171                                         Assert.IsFalse (reader.Read (), "#B");
172                                 }
173                         } finally {
174                                 if (trans != null)
175                                         trans.Dispose ();
176                                 if (conn != null)
177                                         conn.Close ();
178
179                                 conn = new SqlConnection (connectionString);
180                                 conn.Open ();
181                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
182                         }
183                 }
184
185                 [Test]
186                 public void Commit_Reader_Open ()
187                 {
188                         if (RunningOnMono)
189                                 Assert.Ignore ("NotWorking");
190
191                         SqlCommand cmd;
192
193                         conn = new SqlConnection (connectionString);
194                         conn.Open ();
195
196                         trans = conn.BeginTransaction ();
197
198                         try {
199                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
200                                 cmd = new SqlCommand (sql, conn, trans);
201                                 cmd.ExecuteNonQuery ();
202                                 cmd.Dispose ();
203
204                                 cmd = new SqlCommand ("select @@version", conn, trans);
205                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
206                                         try {
207                                                 trans.Commit ();
208                                                 Assert.Fail ("#A1");
209                                         } catch (InvalidOperationException ex) {
210                                                 // There is already an open DataReader
211                                                 // associated with this Command which
212                                                 // must be closed first
213                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
214                                                 Assert.IsNull (ex.InnerException, "#A3");
215                                                 Assert.IsNotNull (ex.Message, "#A4");
216                                         }
217                                 }
218
219                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn, trans);
220                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
221                                         Assert.IsTrue (reader.Read (), "#B1");
222                                 }
223
224                                 trans.Dispose ();
225                                 conn.Close ();
226
227                                 conn.Open ();
228
229                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
230                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
231                                         Assert.IsFalse (reader.Read (), "#C1");
232                                 }
233                         } finally {
234                                 if (trans != null)
235                                         trans.Dispose ();
236                                 if (conn != null)
237                                         conn.Close ();
238
239                                 conn = new SqlConnection (connectionString);
240                                 conn.Open ();
241                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
242                         }
243                 }
244
245                 [Test]
246                 public void Commit_Transaction_Committed ()
247                 {
248                         try {
249                                 conn = new SqlConnection (connectionString);
250                                 conn.Open ();
251
252                                 using (trans = conn.BeginTransaction ()) {
253                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
254                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
255                                         cmd.ExecuteNonQuery ();
256                                         cmd.Dispose ();
257
258                                         trans.Commit ();
259
260                                         try {
261                                                 trans.Commit ();
262                                                 Assert.Fail ("#A1");
263                                         } catch (InvalidOperationException ex) {
264                                                 // This SqlTransaction has completed; it is no
265                                                 // longer usable
266                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
267                                                 Assert.IsNull (ex.InnerException, "#A3");
268                                                 Assert.IsNotNull (ex.Message, "#A4");
269                                         }
270
271                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
272                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
273                                                 Assert.IsTrue (reader.Read (), "#B1");
274                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
275                                                 Assert.IsFalse (reader.Read (), "#B3");
276                                         }
277
278                                         conn.Close ();
279                                         conn = new SqlConnection (connectionString);
280                                         conn.Open ();
281
282                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
283                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
284                                                 Assert.IsTrue (reader.Read (), "#C1");
285                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
286                                                 Assert.IsFalse (reader.Read (), "#C3");
287                                         }
288                                 }
289                         } finally {
290                                 if (conn != null)
291                                         conn.Close ();
292
293                                 conn = new SqlConnection (connectionString);
294                                 conn.Open ();
295                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
296                         }
297                 }
298
299                 [Test]
300                 public void Commit_Transaction_Disposed ()
301                 {
302                         try {
303                                 conn = new SqlConnection (connectionString);
304                                 conn.Open ();
305
306                                 using (trans = conn.BeginTransaction ()) {
307                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
308                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
309                                         cmd.ExecuteNonQuery ();
310                                         cmd.Dispose ();
311
312                                         trans.Dispose ();
313
314                                         try {
315                                                 trans.Commit ();
316                                                 Assert.Fail ("#A1");
317                                         } catch (InvalidOperationException ex) {
318                                                 // This SqlTransaction has completed; it is no
319                                                 // longer usable
320                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
321                                                 Assert.IsNull (ex.InnerException, "#A3");
322                                                 Assert.IsNotNull (ex.Message, "#A4");
323                                         }
324
325                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
326                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
327                                                 Assert.IsFalse (reader.Read (), "#B1");
328                                         }
329                                 }
330                         } finally {
331                                 if (conn != null)
332                                         conn.Close ();
333
334                                 conn = new SqlConnection (connectionString);
335                                 conn.Open ();
336                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
337                         }
338                 }
339
340                 [Test]
341                 public void Commit_Transaction_Rolledback ()
342                 {
343                         if (RunningOnMono)
344                                 Assert.Ignore ("NotWorking");
345
346                         try {
347                                 conn = new SqlConnection (connectionString);
348                                 conn.Open ();
349
350                                 using (trans = conn.BeginTransaction ()) {
351                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
352                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
353                                         cmd.ExecuteNonQuery ();
354                                         cmd.Dispose ();
355
356                                         trans.Rollback ();
357
358                                         try {
359                                                 trans.Commit ();
360                                                 Assert.Fail ("#A1");
361                                         } catch (InvalidOperationException ex) {
362                                                 // This SqlTransaction has completed; it is no
363                                                 // longer usable
364                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
365                                                 Assert.IsNull (ex.InnerException, "#A3");
366                                                 Assert.IsNotNull (ex.Message, "#A4");
367                                         }
368
369                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
370                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
371                                                 Assert.IsFalse (reader.Read (), "#B1");
372                                         }
373                                 }
374
375                                 using (trans = conn.BeginTransaction ()) {
376                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
377                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
378                                         cmd.ExecuteNonQuery ();
379                                         cmd.Dispose ();
380
381                                         trans.Save ("SAVE1");
382
383                                         sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
384                                         cmd = new SqlCommand (sql, conn, trans);
385                                         cmd.ExecuteNonQuery ();
386                                         cmd.Dispose ();
387
388                                         trans.Rollback ("SAVE1");
389                                         trans.Commit ();
390
391                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
392                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
393                                                 Assert.IsTrue (reader.Read (), "#D1");
394                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#D2");
395                                                 Assert.IsFalse (reader.Read (), "#D3");
396                                         }
397
398                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
399                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
400                                                 Assert.IsFalse (reader.Read (), "#E1");
401                                         }
402                                 }
403                         } finally {
404                                 if (conn != null)
405                                         conn.Close ();
406
407                                 conn = new SqlConnection (connectionString);
408                                 conn.Open ();
409                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
410                         }
411                 }
412
413                 [Test]
414                 public void Connection_Transaction_Committed ()
415                 {
416                         conn = new SqlConnection (connectionString);
417                         conn.Open ();
418
419                         trans = conn.BeginTransaction ();
420                         trans.Commit ();
421
422                         Assert.IsNull (trans.Connection);
423                 }
424
425                 [Test]
426                 public void Connection_Transaction_Disposed ()
427                 {
428                         conn = new SqlConnection (connectionString);
429                         conn.Open ();
430
431                         trans = conn.BeginTransaction ();
432                         trans.Dispose ();
433
434                         Assert.IsNull (trans.Connection);
435                 }
436
437                 [Test]
438                 public void Connection_Transaction_Open ()
439                 {
440                         conn = new SqlConnection (connectionString);
441                         conn.Open ();
442
443                         trans = conn.BeginTransaction ();
444
445                         Assert.AreSame (conn, trans.Connection);
446                 }
447
448                 [Test]
449                 public void Connection_Transaction_Rolledback ()
450                 {
451                         if (RunningOnMono)
452                                 Assert.Ignore ("NotWorking");
453
454                         conn = new SqlConnection (connectionString);
455                         conn.Open ();
456
457                         trans = conn.BeginTransaction ();
458                         try {
459                                 trans.Rollback ();
460                                 Assert.IsNull (trans.Connection);
461                         } finally {
462                                 trans.Dispose ();
463                         }
464
465                         trans = conn.BeginTransaction ();
466                         trans.Save ("SAVE1");
467                         try {
468                                 trans.Rollback ("SAVE1");
469                                 Assert.AreSame (conn, trans.Connection);
470                         } finally {
471                                 trans.Dispose ();
472                         }
473                 }
474
475                 [Test]
476                 public void Connection_Transaction_Saved ()
477                 {
478                         conn = new SqlConnection (connectionString);
479                         conn.Open ();
480
481                         trans = conn.BeginTransaction ();
482                         trans.Save ("SAVE1");
483
484                         Assert.AreSame (conn, trans.Connection);
485                 }
486
487                 [Test]
488                 public void Dispose ()
489                 {
490                         string sql;
491                         SqlCommand cmd = null;
492
493                         try {
494                                 conn = new SqlConnection (connectionString);
495                                 conn.Open ();
496
497                                 trans = conn.BeginTransaction ();
498
499                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
500                                 cmd = new SqlCommand (sql, conn, trans);
501                                 cmd.ExecuteNonQuery ();
502                                 cmd.Dispose ();
503
504                                 trans.Save ("SAVE1");
505
506                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
507                                 cmd = new SqlCommand (sql, conn, trans);
508                                 cmd.ExecuteNonQuery ();
509                                 cmd.Dispose ();
510
511                                 trans.Dispose ();
512                                 trans.Dispose ();
513
514                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
515                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
516                                         Assert.IsFalse (reader.Read (), "#1");
517                                 }
518
519                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
520                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
521                                         Assert.IsFalse (reader.Read (), "#2");
522                                 }
523                         } finally {
524                                 if (cmd != null)
525                                         cmd.Dispose ();
526                                 if (trans != null)
527                                         trans.Dispose ();
528                                 if (conn != null)
529                                         conn.Close ();
530
531                                 conn = new SqlConnection (connectionString);
532                                 conn.Open ();
533                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
534                         }
535                 }
536
537                 [Test]
538                 public void Dispose_Connection_Closed ()
539                 {
540                         try {
541                                 conn = new SqlConnection (connectionString);
542                                 conn.Open ();
543
544                                 trans = conn.BeginTransaction ();
545
546                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
547                                 SqlCommand cmd = new SqlCommand (sql, conn, trans);
548                                 cmd.ExecuteNonQuery ();
549                                 cmd.Dispose ();
550
551                                 conn.Close ();
552
553                                 trans.Dispose ();
554
555                                 conn = new SqlConnection (connectionString);
556                                 conn.Open ();
557
558                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
559                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
560                                         Assert.IsFalse (reader.Read ());
561                                 }
562                         } finally {
563                                 if (trans != null)
564                                         trans.Dispose ();
565                                 if (conn != null)
566                                         conn.Close ();
567
568                                 conn = new SqlConnection (connectionString);
569                                 conn.Open ();
570                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
571                         }
572                 }
573
574                 [Test]
575                 public void Dispose_Reader_Open ()
576                 {
577                         if (RunningOnMono)
578                                 Assert.Ignore ("NotWorking");
579
580                         try {
581                                 conn = new SqlConnection (connectionString);
582                                 conn.Open ();
583
584                                 trans = conn.BeginTransaction ();
585
586                                 SqlCommand cmd = new SqlCommand ("select * from employee", conn, trans);
587                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
588                                         try {
589                                                 trans.Dispose ();
590                                                 Assert.Fail ("#A1");
591                                         } catch (InvalidOperationException ex) {
592                                                 // There is already an open DataReader
593                                                 // associated with this Connection
594                                                 // which must be closed first
595                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
596                                                 Assert.IsNull (ex.InnerException, "#A3");
597                                                 Assert.IsNotNull (ex.Message, "#A4");
598                                         }
599
600                                         try {
601                                                 trans.Dispose ();
602                                                 Assert.Fail ("#B1");
603                                         } catch (InvalidOperationException ex) {
604                                                 // There is already an open DataReader
605                                                 // associated with this Connection
606                                                 // which must be closed first
607                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
608                                                 Assert.IsNull (ex.InnerException, "#B3");
609                                                 Assert.IsNotNull (ex.Message, "#B4");
610                                         }
611                                 }
612
613                                 trans.Dispose ();
614                         } finally {
615                                 if (trans != null)
616                                         trans.Dispose ();
617                                 if (conn != null)
618                                         conn.Close ();
619                         }
620                 }
621
622                 [Test]
623                 public void IsolationLevel_Reader_Open ()
624                 {
625                         conn = new SqlConnection (connectionString);
626                         conn.Open ();
627
628                         trans = conn.BeginTransaction ();
629
630                         try {
631                                 SqlCommand cmd = new SqlCommand ("select @@version", conn, trans);
632                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
633                                         Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel);
634                                 }
635                         } finally {
636                                 if (trans != null)
637                                         trans.Dispose ();
638                         }
639                 }
640
641                 [Test]
642                 public void IsolationLevel_Transaction_Committed ()
643                 {
644                         conn = new SqlConnection (connectionString);
645                         conn.Open ();
646
647                         trans = conn.BeginTransaction ();
648                         trans.Commit ();
649
650                         try {
651                                 IsolationLevel iso = trans.IsolationLevel;
652                                 Assert.Fail ("#1:" + iso);
653                         } catch (InvalidOperationException ex) {
654                                 // This SqlTransaction has completed; it is no
655                                 // longer usable
656                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
657                                 Assert.IsNull (ex.InnerException, "#3");
658                                 Assert.IsNotNull (ex.Message, "#4");
659                         }
660                 }
661
662                 [Test]
663                 public void IsolationLevel_Transaction_Disposed ()
664                 {
665                         conn = new SqlConnection (connectionString);
666                         conn.Open ();
667
668                         trans = conn.BeginTransaction ();
669                         trans.Dispose ();
670
671                         try {
672                                 IsolationLevel iso = trans.IsolationLevel;
673                                 Assert.Fail ("#1:" + iso);
674                         } catch (InvalidOperationException ex) {
675                                 // This SqlTransaction has completed; it is no
676                                 // longer usable
677                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
678                                 Assert.IsNull (ex.InnerException, "#3");
679                                 Assert.IsNotNull (ex.Message, "#4");
680                         }
681                 }
682
683                 [Test]
684                 public void IsolationLevel_Transaction_Open ()
685                 {
686                         conn = new SqlConnection (connectionString);
687                         conn.Open ();
688
689                         trans = conn.BeginTransaction ();
690                         Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel);
691                 }
692
693                 [Test]
694                 public void IsolationLevel_Transaction_Rolledback ()
695                 {
696                         if (RunningOnMono)
697                                 Assert.Ignore ("NotWorking");
698
699                         conn = new SqlConnection (connectionString);
700                         conn.Open ();
701
702                         trans = conn.BeginTransaction ();
703                         trans.Rollback ();
704
705                         try {
706                                 IsolationLevel iso = trans.IsolationLevel;
707                                 Assert.Fail ("#A1:" + iso);
708                         } catch (InvalidOperationException ex) {
709                                 // This SqlTransaction has completed; it is no
710                                 // longer usable
711                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
712                                 Assert.IsNull (ex.InnerException, "#A3");
713                                 Assert.IsNotNull (ex.Message, "#A4");
714                         } finally {
715                                 trans.Dispose ();
716                         }
717
718                         trans = conn.BeginTransaction ();
719                         trans.Save ("SAVE1");
720                         trans.Rollback ("SAVE1");
721
722                         Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel, "#B1");
723                 }
724
725                 [Test]
726                 public void IsolationLevel_Transaction_Saved ()
727                 {
728                         conn = new SqlConnection (connectionString);
729                         conn.Open ();
730
731                         trans = conn.BeginTransaction ();
732                         trans.Save ("SAVE1");
733                         Assert.AreEqual (IsolationLevel.ReadCommitted, trans.IsolationLevel);
734                 }
735
736                 [Test] // Rollback ()
737                 public void Rollback1 ()
738                 {
739                         string sql;
740                         SqlCommand cmd = null;
741                         SqlConnection connA = null;
742                         SqlConnection connB = null;
743
744                         try {
745                                 connA = new SqlConnection (connectionString);
746                                 connA.Open ();
747
748                                 connB = new SqlConnection (connectionString);
749                                 connB.Open ();
750
751                                 using (trans = connA.BeginTransaction ()) {
752                                         sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
753                                         cmd = new SqlCommand (sql, connA, trans);
754                                         cmd.ExecuteNonQuery ();
755                                         cmd.Dispose ();
756
757                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connA, trans);
758                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
759                                                 Assert.IsTrue (reader.Read (), "#A1");
760                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#A2");
761                                                 Assert.IsFalse (reader.Read (), "#A3");
762                                         }
763
764                                         trans.Rollback ();
765
766                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connA);
767                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
768                                                 Assert.IsFalse (reader.Read (), "#B1");
769                                         }
770
771                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", connB);
772                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
773                                                 Assert.IsFalse (reader.Read (), "#C1");
774                                         }
775                                 }
776                         } finally {
777                                 if (cmd != null)
778                                         cmd.Dispose ();
779                                 if (connA != null)
780                                         connA.Close ();
781                                 if (connB != null)
782                                         connB.Close ();
783
784                                 conn = new SqlConnection (connectionString);
785                                 conn.Open ();
786                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
787                         }
788
789                         try {
790                                 conn = new SqlConnection (connectionString);
791                                 conn.Open ();
792
793                                 trans = conn.BeginTransaction ();
794
795                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
796                                 cmd = new SqlCommand (sql, conn, trans);
797                                 cmd.ExecuteNonQuery ();
798                                 cmd.Dispose ();
799
800                                 trans.Save ("SAVE1");
801
802                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
803                                 cmd = new SqlCommand (sql, conn, trans);
804                                 cmd.ExecuteNonQuery ();
805                                 cmd.Dispose ();
806
807                                 trans.Save ("SAVE2");
808
809                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
810                                 cmd = new SqlCommand (sql, conn, trans);
811                                 cmd.ExecuteNonQuery ();
812                                 cmd.Dispose ();
813
814                                 trans.Rollback ();
815                                 conn.Close ();
816
817                                 conn = new SqlConnection (connectionString);
818                                 conn.Open ();
819
820                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
821                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
822                                         Assert.IsFalse (reader.Read (), "#D1");
823                                 }
824
825                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
826                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
827                                         Assert.IsFalse (reader.Read (), "#E1");
828                                 }
829
830                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
831                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
832                                         Assert.IsFalse (reader.Read (), "#F1");
833                                 }
834                         } finally {
835                                 if (cmd != null)
836                                         cmd.Dispose ();
837                                 if (trans != null)
838                                         trans.Dispose ();
839                                 if (conn != null)
840                                         conn.Close ();
841
842                                 conn = new SqlConnection (connectionString);
843                                 conn.Open ();
844                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
845                         }
846
847                         try {
848                                 conn = new SqlConnection (connectionString);
849                                 conn.Open ();
850
851                                 trans = conn.BeginTransaction ();
852                                 trans.Rollback ();
853                         } finally {
854                                 if (trans != null)
855                                         trans.Dispose ();
856                         }
857                 }
858
859                 [Test] // Rollback ()
860                 public void Rollback1_Connection_Closed ()
861                 {
862                         try {
863                                 conn = new SqlConnection (connectionString);
864                                 conn.Open ();
865
866                                 trans = conn.BeginTransaction ();
867
868                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
869                                 SqlCommand cmd = new SqlCommand (sql, conn, trans);
870                                 cmd.ExecuteNonQuery ();
871                                 cmd.Dispose ();
872
873                                 conn.Close ();
874
875                                 try {
876                                         trans.Rollback ();
877                                         Assert.Fail ("#A1");
878                                 } catch (InvalidOperationException ex) {
879                                         // This SqlTransaction has completed; it is no
880                                         // longer usable
881                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
882                                         Assert.IsNull (ex.InnerException, "#A3");
883                                         Assert.IsNotNull (ex.Message, "#A4");
884                                 }
885
886                                 conn = new SqlConnection (connectionString);
887                                 conn.Open ();
888
889                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
890                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
891                                         Assert.IsFalse (reader.Read (), "#B1");
892                                 }
893                         } finally {
894                                 if (conn != null)
895                                         conn.Close ();
896
897                                 conn = new SqlConnection (connectionString);
898                                 conn.Open ();
899                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
900                         }
901                 }
902
903                 [Test] // Rollback ()
904                 public void Rollback1_Reader_Open ()
905                 {
906                         if (RunningOnMono)
907                                 Assert.Ignore ("NotWorking");
908
909                         SqlCommand cmd;
910
911                         conn = new SqlConnection (connectionString);
912                         conn.Open ();
913
914                         trans = conn.BeginTransaction ();
915
916                         try {
917                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
918                                 cmd = new SqlCommand (sql, conn, trans);
919                                 cmd.ExecuteNonQuery ();
920                                 cmd.Dispose ();
921
922                                 cmd = new SqlCommand ("select @@version", conn, trans);
923                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
924                                         try {
925                                                 trans.Rollback ();
926                                                 Assert.Fail ("#1");
927                                         } catch (InvalidOperationException ex) {
928                                                 // There is already an open DataReader
929                                                 // associated with this Command which
930                                                 // must be closed first
931                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
932                                                 Assert.IsNull (ex.InnerException, "#3");
933                                                 Assert.IsNotNull (ex.Message, "#4");
934                                         }
935                                 }
936                         } finally {
937                                 if (trans != null)
938                                         trans.Dispose ();
939                         }
940                 }
941
942                 [Test] // Rollback ()
943                 public void Rollback1_Transaction_Committed ()
944                 {
945                         try {
946                                 conn = new SqlConnection (connectionString);
947                                 conn.Open ();
948
949                                 using (trans = conn.BeginTransaction ()) {
950                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
951                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
952                                         cmd.ExecuteNonQuery ();
953                                         cmd.Dispose ();
954
955                                         trans.Commit ();
956
957                                         try {
958                                                 trans.Rollback ();
959                                                 Assert.Fail ("#A1");
960                                         } catch (InvalidOperationException ex) {
961                                                 // This SqlTransaction has completed; it is no
962                                                 // longer usable
963                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
964                                                 Assert.IsNull (ex.InnerException, "#A3");
965                                                 Assert.IsNotNull (ex.Message, "#A4");
966                                         }
967
968                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
969                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
970                                                 Assert.IsTrue (reader.Read (), "#B1");
971                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
972                                                 Assert.IsFalse (reader.Read (), "#B3");
973                                         }
974
975                                         conn.Close ();
976                                         conn = new SqlConnection (connectionString);
977                                         conn.Open ();
978
979                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
980                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
981                                                 Assert.IsTrue (reader.Read (), "#C1");
982                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
983                                                 Assert.IsFalse (reader.Read (), "#C3");
984                                         }
985                                 }
986                         } finally {
987                                 if (conn != null)
988                                         conn.Close ();
989
990                                 conn = new SqlConnection (connectionString);
991                                 conn.Open ();
992                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
993                         }
994                 }
995
996                 [Test] // Rollback ()
997                 public void Rollback1_Transaction_Disposed ()
998                 {
999                         try {
1000                                 conn = new SqlConnection (connectionString);
1001                                 conn.Open ();
1002
1003                                 using (trans = conn.BeginTransaction ()) {
1004                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1005                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1006                                         cmd.ExecuteNonQuery ();
1007                                         cmd.Dispose ();
1008
1009                                         trans.Dispose ();
1010                                         trans.Rollback ();
1011
1012                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1013                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1014                                                 Assert.IsFalse (reader.Read (), "#B1");
1015                                         }
1016                                 }
1017                         } finally {
1018                                 if (conn != null)
1019                                         conn.Close ();
1020
1021                                 conn = new SqlConnection (connectionString);
1022                                 conn.Open ();
1023                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1024                         }
1025                 }
1026
1027                 [Test] // Rollback ()
1028                 public void Rollback1_Transaction_Rolledback ()
1029                 {
1030                         if (RunningOnMono)
1031                                 Assert.Ignore ("NotWorking");
1032
1033                         try {
1034                                 conn = new SqlConnection (connectionString);
1035                                 conn.Open ();
1036
1037                                 using (trans = conn.BeginTransaction ()) {
1038                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1039                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1040                                         cmd.ExecuteNonQuery ();
1041                                         cmd.Dispose ();
1042
1043                                         trans.Rollback ();
1044
1045                                         try {
1046                                                 trans.Rollback ();
1047                                                 Assert.Fail ("#A1");
1048                                         } catch (InvalidOperationException ex) {
1049                                                 // This SqlTransaction has completed; it is no
1050                                                 // longer usable
1051                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1052                                                 Assert.IsNull (ex.InnerException, "#A3");
1053                                                 Assert.IsNotNull (ex.Message, "#A4");
1054                                         }
1055
1056                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1057                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1058                                                 Assert.IsFalse (reader.Read (), "#B1");
1059                                         }
1060                                 }
1061
1062                                 using (trans = conn.BeginTransaction ()) {
1063                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1064                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1065                                         cmd.ExecuteNonQuery ();
1066                                         cmd.Dispose ();
1067
1068                                         trans.Save ("SAVE1");
1069
1070                                         sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
1071                                         cmd = new SqlCommand (sql, conn, trans);
1072                                         cmd.ExecuteNonQuery ();
1073                                         cmd.Dispose ();
1074
1075                                         trans.Rollback ("SAVE1");
1076
1077                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn, trans);
1078                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1079                                                 Assert.IsTrue (reader.Read (), "#C1");
1080                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
1081                                                 Assert.IsFalse (reader.Read (), "#C3");
1082                                         }
1083
1084                                         trans.Rollback ();
1085
1086                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1087                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1088                                                 Assert.IsFalse (reader.Read (), "#D1");
1089                                         }
1090                                 }
1091                         } finally {
1092                                 if (conn != null)
1093                                         conn.Close ();
1094
1095                                 conn = new SqlConnection (connectionString);
1096                                 conn.Open ();
1097                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1098                         }
1099                 }
1100
1101                 [Test] // Rollback (String)
1102                 public void Rollback2 ()
1103                 {
1104                         if (RunningOnMono)
1105                                 Assert.Ignore ("NotWorking");
1106
1107                         string sql;
1108                         SqlCommand cmd = null;
1109
1110                         try {
1111                                 conn = new SqlConnection (connectionString);
1112                                 conn.Open ();
1113
1114                                 trans = conn.BeginTransaction ();
1115
1116                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1117                                 cmd = new SqlCommand (sql, conn, trans);
1118                                 cmd.ExecuteNonQuery ();
1119                                 cmd.Dispose ();
1120
1121                                 trans.Save ("SAVE1");
1122
1123                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
1124                                 cmd = new SqlCommand (sql, conn, trans);
1125                                 cmd.ExecuteNonQuery ();
1126                                 cmd.Dispose ();
1127
1128                                 trans.Save ("SAVE2");
1129
1130                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
1131                                 cmd = new SqlCommand (sql, conn, trans);
1132                                 cmd.ExecuteNonQuery ();
1133                                 cmd.Dispose ();
1134
1135                                 trans.Save ("SAVE3");
1136
1137                                 trans.Rollback ("SAVE1");
1138                                 trans.Commit ();
1139                                 conn.Close ();
1140
1141                                 conn = new SqlConnection (connectionString);
1142                                 conn.Open ();
1143
1144                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1145                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1146                                         Assert.IsTrue (reader.Read (), "#A1");
1147                                         Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#A2");
1148                                         Assert.IsFalse (reader.Read (), "#A3");
1149                                 }
1150
1151                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
1152                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1153                                         Assert.IsFalse (reader.Read (), "#B1");
1154                                 }
1155
1156                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
1157                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1158                                         Assert.IsFalse (reader.Read (), "#C1");
1159                                 }
1160                         } finally {
1161                                 if (cmd != null)
1162                                         cmd.Dispose ();
1163                                 if (trans != null)
1164                                         trans.Dispose ();
1165                                 if (conn != null)
1166                                         conn.Close ();
1167
1168                                 conn = new SqlConnection (connectionString);
1169                                 conn.Open ();
1170                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1171                         }
1172
1173                         try {
1174                                 conn = new SqlConnection (connectionString);
1175                                 conn.Open ();
1176
1177                                 trans = conn.BeginTransaction ();
1178
1179                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1180                                 cmd = new SqlCommand (sql, conn, trans);
1181                                 cmd.ExecuteNonQuery ();
1182                                 cmd.Dispose ();
1183
1184                                 trans.Save ("SAVE1");
1185
1186                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
1187                                 cmd = new SqlCommand (sql, conn, trans);
1188                                 cmd.ExecuteNonQuery ();
1189                                 cmd.Dispose ();
1190
1191                                 trans.Save ("SAVE2");
1192
1193                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
1194                                 cmd = new SqlCommand (sql, conn, trans);
1195                                 cmd.ExecuteNonQuery ();
1196                                 cmd.Dispose ();
1197
1198                                 trans.Save ("SAVE1");
1199
1200                                 trans.Rollback ("SAVE1");
1201                                 trans.Commit ();
1202                                 conn.Close ();
1203
1204                                 conn = new SqlConnection (connectionString);
1205                                 conn.Open ();
1206
1207                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1208                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1209                                         Assert.IsTrue (reader.Read (), "#D1");
1210                                         Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#D2");
1211                                         Assert.IsFalse (reader.Read (), "#D3");
1212                                 }
1213
1214                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
1215                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1216                                         Assert.IsTrue (reader.Read (), "#E1");
1217                                         Assert.AreEqual ("BangaloreNovell", reader.GetString (0), "#E2");
1218                                         Assert.IsFalse (reader.Read (), "#E3");
1219                                 }
1220
1221                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
1222                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1223                                         Assert.IsTrue (reader.Read (), "#F1");
1224                                         Assert.AreEqual ("Novell", reader.GetString (0), "#F2");
1225                                         Assert.IsFalse (reader.Read (), "#F3");
1226                                 }
1227                         } finally {
1228                                 if (cmd != null)
1229                                         cmd.Dispose ();
1230                                 if (trans != null)
1231                                         trans.Dispose ();
1232                                 if (conn != null)
1233                                         conn.Close ();
1234
1235                                 conn = new SqlConnection (connectionString);
1236                                 conn.Open ();
1237                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1238                         }
1239                 }
1240
1241                 [Test] // Rollback (String)
1242                 public void Rollback2_Connection_Closed ()
1243                 {
1244                         try {
1245                                 conn = new SqlConnection (connectionString);
1246                                 conn.Open ();
1247
1248                                 trans = conn.BeginTransaction ();
1249
1250                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1251                                 SqlCommand cmd = new SqlCommand (sql, conn, trans);
1252                                 cmd.ExecuteNonQuery ();
1253                                 cmd.Dispose ();
1254
1255                                 conn.Close ();
1256
1257                                 try {
1258                                         trans.Rollback ("SAVE1");
1259                                         Assert.Fail ("#A1");
1260                                 } catch (InvalidOperationException ex) {
1261                                         // This SqlTransaction has completed; it is no
1262                                         // longer usable
1263                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1264                                         Assert.IsNull (ex.InnerException, "#A3");
1265                                         Assert.IsNotNull (ex.Message, "#A4");
1266                                 }
1267
1268                                 conn = new SqlConnection (connectionString);
1269                                 conn.Open ();
1270
1271                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1272                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1273                                         Assert.IsFalse (reader.Read (), "#B");
1274                                 }
1275                         } finally {
1276                                 if (trans != null)
1277                                         trans.Dispose ();
1278                                 if (conn != null)
1279                                         conn.Close ();
1280
1281                                 conn = new SqlConnection (connectionString);
1282                                 conn.Open ();
1283                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1284                         }
1285                 }
1286
1287                 [Test] // Rollback (String)
1288                 public void Rollback2_Reader_Open ()
1289                 {
1290                         if (RunningOnMono)
1291                                 Assert.Ignore ("NotWorking");
1292
1293                         SqlCommand cmd;
1294
1295                         conn = new SqlConnection (connectionString);
1296                         conn.Open ();
1297
1298                         trans = conn.BeginTransaction ();
1299
1300                         try {
1301                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1302                                 cmd = new SqlCommand (sql, conn, trans);
1303                                 cmd.ExecuteNonQuery ();
1304                                 cmd.Dispose ();
1305
1306                                 trans.Save ("SAVE1");
1307
1308                                 cmd = new SqlCommand ("select @@version", conn, trans);
1309                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1310                                         try {
1311                                                 trans.Rollback ("SAVE1");
1312                                                 Assert.Fail ("#1");
1313                                         } catch (InvalidOperationException ex) {
1314                                                 // There is already an open DataReader
1315                                                 // associated with this Command which
1316                                                 // must be closed first
1317                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1318                                                 Assert.IsNull (ex.InnerException, "#3");
1319                                                 Assert.IsNotNull (ex.Message, "#4");
1320                                         }
1321                                 }
1322                         } finally {
1323                                 if (trans != null)
1324                                         trans.Dispose ();
1325                         }
1326                 }
1327
1328                 [Test] // Rollback (String)
1329                 public void Rollback2_Transaction_Committed ()
1330                 {
1331                         try {
1332                                 conn = new SqlConnection (connectionString);
1333                                 conn.Open ();
1334
1335                                 using (trans = conn.BeginTransaction ()) {
1336                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1337                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1338                                         cmd.ExecuteNonQuery ();
1339                                         cmd.Dispose ();
1340
1341                                         trans.Commit ();
1342
1343                                         try {
1344                                                 trans.Rollback ("SAVE1");
1345                                                 Assert.Fail ("#A1");
1346                                         } catch (InvalidOperationException ex) {
1347                                                 // This SqlTransaction has completed; it is no
1348                                                 // longer usable
1349                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1350                                                 Assert.IsNull (ex.InnerException, "#A3");
1351                                                 Assert.IsNotNull (ex.Message, "#A4");
1352                                         }
1353
1354                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1355                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1356                                                 Assert.IsTrue (reader.Read (), "#B1");
1357                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
1358                                                 Assert.IsFalse (reader.Read (), "#B3");
1359                                         }
1360
1361                                         conn.Close ();
1362                                         conn = new SqlConnection (connectionString);
1363                                         conn.Open ();
1364
1365                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1366                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1367                                                 Assert.IsTrue (reader.Read (), "#C1");
1368                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
1369                                                 Assert.IsFalse (reader.Read (), "#C3");
1370                                         }
1371                                 }
1372                         } finally {
1373                                 if (conn != null)
1374                                         conn.Close ();
1375
1376                                 conn = new SqlConnection (connectionString);
1377                                 conn.Open ();
1378                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1379                         }
1380                 }
1381
1382                 [Test] // Rollback (String)
1383                 public void Rollback2_Transaction_Disposed ()
1384                 {
1385                         if (RunningOnMono)
1386                                 Assert.Ignore ("NotWorking");
1387
1388                         try {
1389                                 conn = new SqlConnection (connectionString);
1390                                 conn.Open ();
1391
1392                                 using (trans = conn.BeginTransaction ()) {
1393                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1394                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1395                                         cmd.ExecuteNonQuery ();
1396                                         cmd.Dispose ();
1397
1398                                         trans.Dispose ();
1399
1400                                         try {
1401                                                 trans.Rollback ("SAVE1");
1402                                                 Assert.Fail ("#A1");
1403                                         } catch (InvalidOperationException ex) {
1404                                                 // This SqlTransaction has completed; it is no
1405                                                 // longer usable
1406                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1407                                                 Assert.IsNull (ex.InnerException, "#A3");
1408                                                 Assert.IsNotNull (ex.Message, "#A4");
1409                                         }
1410
1411                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1412                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1413                                                 Assert.IsFalse (reader.Read (), "#B1");
1414                                         }
1415                                 }
1416                         } finally {
1417                                 if (conn != null)
1418                                         conn.Close ();
1419
1420                                 conn = new SqlConnection (connectionString);
1421                                 conn.Open ();
1422                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1423                         }
1424                 }
1425
1426                 [Test] // Rollback (String)
1427                 public void Rollback2_Transaction_Rolledback ()
1428                 {
1429                         if (RunningOnMono)
1430                                 Assert.Ignore ("NotWorking");
1431
1432                         try {
1433                                 conn = new SqlConnection (connectionString);
1434                                 conn.Open ();
1435
1436                                 using (trans = conn.BeginTransaction ()) {
1437                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1438                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1439                                         cmd.ExecuteNonQuery ();
1440                                         cmd.Dispose ();
1441
1442                                         trans.Rollback ();
1443
1444                                         try {
1445                                                 trans.Rollback ("SAVE1");
1446                                                 Assert.Fail ("#A1");
1447                                         } catch (InvalidOperationException ex) {
1448                                                 // This SqlTransaction has completed; it is no
1449                                                 // longer usable
1450                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1451                                                 Assert.IsNull (ex.InnerException, "#A3");
1452                                                 Assert.IsNotNull (ex.Message, "#A4");
1453                                         }
1454
1455                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1456                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1457                                                 Assert.IsFalse (reader.Read (), "#B1");
1458                                         }
1459                                 }
1460
1461                                 using (trans = conn.BeginTransaction ()) {
1462                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1463                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1464                                         cmd.ExecuteNonQuery ();
1465                                         cmd.Dispose ();
1466
1467                                         trans.Save ("SAVE1");
1468                                         trans.Rollback ("SAVE1");
1469
1470                                         try {
1471                                                 trans.Rollback ("SAVE1");
1472                                                 Assert.Fail ("#C1");
1473                                         } catch (SqlException ex) {
1474                                                 // Cannot roll back SAVE1. No transaction
1475                                                 // or savepoint of that name was found
1476                                                 Assert.AreEqual (typeof (SqlException), ex.GetType (), "#C2");
1477                                                 Assert.AreEqual ((byte) 16, ex.Class, "#C3");
1478                                                 Assert.IsNull (ex.InnerException, "#C4");
1479                                                 Assert.IsNotNull (ex.Message, "#C5");
1480                                                 Assert.IsTrue (ex.Message.IndexOf ("SAVE1") != -1, "#C6");
1481                                                 Assert.AreEqual (6401, ex.Number, "#C7");
1482                                                 Assert.AreEqual ((byte) 1, ex.State, "#C8");
1483                                         }
1484
1485                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn, trans);
1486                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1487                                                 Assert.IsTrue (reader.Read (), "#D1");
1488                                         }
1489                                 }
1490                         } finally {
1491                                 if (conn != null)
1492                                         conn.Close ();
1493
1494                                 conn = new SqlConnection (connectionString);
1495                                 conn.Open ();
1496                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1497                         }
1498                 }
1499
1500                 [Test] // Rollback (String)
1501                 public void Rollback2_TransactionName_DoesNotExist ()
1502                 {
1503                         conn = new SqlConnection (connectionString);
1504                         conn.Open ();
1505
1506                         using (trans = conn.BeginTransaction ()) {
1507                                 try {
1508                                         trans.Rollback ("SAVE1");
1509                                         Assert.Fail ("#1");
1510                                 } catch (SqlException ex) {
1511                                         // Cannot roll back SAVE1. No transaction
1512                                         // or savepoint of that name was found
1513                                         Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
1514                                         Assert.AreEqual ((byte) 16, ex.Class, "#3");
1515                                         Assert.IsNull (ex.InnerException, "#4");
1516                                         Assert.IsNotNull (ex.Message, "#5");
1517                                         Assert.IsTrue (ex.Message.IndexOf ("SAVE1") != -1, "#6");
1518                                         Assert.AreEqual (6401, ex.Number, "#7");
1519                                         if (ClientVersion == 7)
1520                                                 Assert.AreEqual ((byte) 2, ex.State, "#8");
1521                                         else
1522                                                 Assert.AreEqual ((byte) 1, ex.State, "#8");
1523                                 }
1524
1525                                 trans.Commit ();
1526                         }
1527                 }
1528
1529                 [Test] // Rollback (String)
1530                 public void Rollback2_TransactionName_Empty ()
1531                 {
1532                         if (RunningOnMono)
1533                                 Assert.Ignore ("NotWorking");
1534
1535                         conn = new SqlConnection (connectionString);
1536                         conn.Open ();
1537
1538                         using (trans = conn.BeginTransaction ()) {
1539                                 try {
1540                                         trans.Rollback (string.Empty);
1541                                         Assert.Fail ("#1");
1542                                 } catch (ArgumentException ex) {
1543                                         // Invalid transaction or invalid name
1544                                         // for a point at which to save within
1545                                         // the transaction
1546                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1547                                         Assert.IsNull (ex.InnerException, "#3");
1548                                         Assert.IsNotNull (ex.Message, "#4");
1549                                         Assert.IsNull (ex.ParamName, "#5");
1550                                 }
1551
1552                                 trans.Commit ();
1553                         }
1554                 }
1555
1556                 [Test] // Rollback (String)
1557                 public void Rollback2_TransactionName_Null ()
1558                 {
1559                         if (RunningOnMono)
1560                                 Assert.Ignore ("NotWorking");
1561
1562                         conn = new SqlConnection (connectionString);
1563                         conn.Open ();
1564
1565                         using (trans = conn.BeginTransaction ()) {
1566                                 try {
1567                                         trans.Rollback ((string) null);
1568                                         Assert.Fail ("#1");
1569                                 } catch (ArgumentException ex) {
1570                                         // Invalid transaction or invalid name
1571                                         // for a point at which to save within
1572                                         // the transaction
1573                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
1574                                         Assert.IsNull (ex.InnerException, "#3");
1575                                         Assert.IsNotNull (ex.Message, "#4");
1576                                         Assert.IsNull (ex.ParamName, "#5");
1577                                 }
1578
1579                                 trans.Commit ();
1580                         }
1581                 }
1582
1583                 [Test]
1584                 public void Save_Connection_Closed ()
1585                 {
1586                         try {
1587                                 conn = new SqlConnection (connectionString);
1588                                 conn.Open ();
1589
1590                                 trans = conn.BeginTransaction ();
1591
1592                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1593                                 SqlCommand cmd = new SqlCommand (sql, conn, trans);
1594                                 cmd.ExecuteNonQuery ();
1595                                 cmd.Dispose ();
1596
1597                                 conn.Close ();
1598
1599                                 try {
1600                                         trans.Save ("SAVE1");
1601                                         Assert.Fail ("#A1");
1602                                 } catch (InvalidOperationException ex) {
1603                                         // This SqlTransaction has completed; it is no
1604                                         // longer usable
1605                                         Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1606                                         Assert.IsNull (ex.InnerException, "#A3");
1607                                         Assert.IsNotNull (ex.Message, "#A4");
1608                                 }
1609
1610                                 conn = new SqlConnection (connectionString);
1611                                 conn.Open ();
1612
1613                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1614                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1615                                         Assert.IsFalse (reader.Read (), "#B1");
1616                                 }
1617                         } finally {
1618                                 if (trans != null)
1619                                         trans.Dispose ();
1620                                 if (conn != null)
1621                                         conn.Close ();
1622
1623                                 conn = new SqlConnection (connectionString);
1624                                 conn.Open ();
1625                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1626                         }
1627                 }
1628
1629                 [Test]
1630                 public void Save ()
1631                 {
1632                         string sql;
1633                         SqlCommand cmd = null;
1634
1635                         try {
1636                                 conn = new SqlConnection (connectionString);
1637                                 conn.Open ();
1638
1639                                 trans = conn.BeginTransaction ();
1640
1641                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1642                                 cmd = new SqlCommand (sql, conn, trans);
1643                                 cmd.ExecuteNonQuery ();
1644                                 cmd.Dispose ();
1645
1646                                 trans.Save ("SAVE1");
1647
1648                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
1649                                 cmd = new SqlCommand (sql, conn, trans);
1650                                 cmd.ExecuteNonQuery ();
1651                                 cmd.Dispose ();
1652
1653                                 trans.Save ("SAVE2");
1654
1655                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
1656                                 cmd = new SqlCommand (sql, conn, trans);
1657                                 cmd.ExecuteNonQuery ();
1658                                 cmd.Dispose ();
1659
1660                                 conn.Close ();
1661
1662                                 conn = new SqlConnection (connectionString);
1663                                 conn.Open ();
1664
1665                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1666                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1667                                         Assert.IsFalse (reader.Read (), "#A1");
1668                                 }
1669
1670                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
1671                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1672                                         Assert.IsFalse (reader.Read (), "#B1");
1673                                 }
1674
1675                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
1676                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1677                                         Assert.IsFalse (reader.Read (), "#C1");
1678                                 }
1679                         } finally {
1680                                 if (cmd != null)
1681                                         cmd.Dispose ();
1682                                 if (trans != null)
1683                                         trans.Dispose ();
1684                                 if (conn != null)
1685                                         conn.Close ();
1686
1687                                 conn = new SqlConnection (connectionString);
1688                                 conn.Open ();
1689                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1690                         }
1691
1692                         try {
1693                                 conn = new SqlConnection (connectionString);
1694                                 conn.Open ();
1695
1696                                 trans = conn.BeginTransaction ();
1697
1698                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1699                                 cmd = new SqlCommand (sql, conn, trans);
1700                                 cmd.ExecuteNonQuery ();
1701                                 cmd.Dispose ();
1702
1703                                 trans.Save ("SAVE1");
1704
1705                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6667, 'BangaloreNovell', '1999-03-10', '2006-08-23')";
1706                                 cmd = new SqlCommand (sql, conn, trans);
1707                                 cmd.ExecuteNonQuery ();
1708                                 cmd.Dispose ();
1709
1710                                 trans.Save ("SAVE2");
1711
1712                                 sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6668, 'Novell', '1997-04-07', '2003-06-25')";
1713                                 cmd = new SqlCommand (sql, conn, trans);
1714                                 cmd.ExecuteNonQuery ();
1715                                 cmd.Dispose ();
1716
1717                                 trans.Commit ();
1718
1719                                 conn = new SqlConnection (connectionString);
1720                                 conn.Open ();
1721
1722                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1723                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1724                                         Assert.IsTrue (reader.Read (), "#D1");
1725                                         Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#D2");
1726                                         Assert.IsFalse (reader.Read (), "#D3");
1727                                 }
1728
1729                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6667", conn);
1730                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1731                                         Assert.IsTrue (reader.Read (), "#E1");
1732                                         Assert.AreEqual ("BangaloreNovell", reader.GetString (0), "#E2");
1733                                         Assert.IsFalse (reader.Read (), "#E3");
1734                                 }
1735
1736                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6668", conn);
1737                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1738                                         Assert.IsTrue (reader.Read (), "#F1");
1739                                         Assert.AreEqual ("Novell", reader.GetString (0), "#F2");
1740                                         Assert.IsFalse (reader.Read (), "#F3");
1741                                 }
1742                         } finally {
1743                                 if (cmd != null)
1744                                         cmd.Dispose ();
1745                                 if (trans != null)
1746                                         trans.Dispose ();
1747                                 if (conn != null)
1748                                         conn.Close ();
1749
1750                                 conn = new SqlConnection (connectionString);
1751                                 conn.Open ();
1752                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1753                         }
1754                 }
1755
1756                 [Test]
1757                 public void Save_Reader_Open ()
1758                 {
1759                         if (RunningOnMono)
1760                                 Assert.Ignore ("NotWorking");
1761
1762                         SqlCommand cmd;
1763
1764                         conn = new SqlConnection (connectionString);
1765                         conn.Open ();
1766
1767                         trans = conn.BeginTransaction ();
1768
1769                         try {
1770                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1771                                 cmd = new SqlCommand (sql, conn, trans);
1772                                 cmd.ExecuteNonQuery ();
1773                                 cmd.Dispose ();
1774
1775                                 cmd = new SqlCommand ("select @@version", conn, trans);
1776                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1777                                         try {
1778                                                 trans.Save ("SAVE1");
1779                                                 Assert.Fail ("#1");
1780                                         } catch (InvalidOperationException ex) {
1781                                                 // There is already an open DataReader
1782                                                 // associated with this Command which
1783                                                 // must be closed first
1784                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
1785                                                 Assert.IsNull (ex.InnerException, "#3");
1786                                                 Assert.IsNotNull (ex.Message, "#4");
1787                                         }
1788                                 }
1789                         } finally {
1790                                 if (trans != null)
1791                                         trans.Dispose ();
1792                         }
1793                 }
1794
1795                 [Test]
1796                 public void Save_Transaction_Committed ()
1797                 {
1798                         try {
1799                                 conn = new SqlConnection (connectionString);
1800                                 conn.Open ();
1801
1802                                 using (trans = conn.BeginTransaction ()) {
1803                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1804                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1805                                         cmd.ExecuteNonQuery ();
1806                                         cmd.Dispose ();
1807
1808                                         trans.Commit ();
1809
1810                                         try {
1811                                                 trans.Save ("SAVE1");
1812                                                 Assert.Fail ("#A1");
1813                                         } catch (InvalidOperationException ex) {
1814                                                 // This SqlTransaction has completed; it is no
1815                                                 // longer usable
1816                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1817                                                 Assert.IsNull (ex.InnerException, "#A3");
1818                                                 Assert.IsNotNull (ex.Message, "#A4");
1819                                         }
1820
1821                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1822                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1823                                                 Assert.IsTrue (reader.Read (), "#B1");
1824                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
1825                                                 Assert.IsFalse (reader.Read (), "#B3");
1826                                         }
1827
1828                                         conn.Close ();
1829                                         conn = new SqlConnection (connectionString);
1830                                         conn.Open ();
1831
1832                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1833                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1834                                                 Assert.IsTrue (reader.Read (), "#C1");
1835                                                 Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#C2");
1836                                                 Assert.IsFalse (reader.Read (), "#C3");
1837                                         }
1838                                 }
1839                         } finally {
1840                                 if (conn != null)
1841                                         conn.Close ();
1842
1843                                 conn = new SqlConnection (connectionString);
1844                                 conn.Open ();
1845                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1846                         }
1847                 }
1848
1849                 [Test]
1850                 public void Save_Transaction_Disposed ()
1851                 {
1852                         if (RunningOnMono)
1853                                 Assert.Ignore ("NotWorking");
1854
1855                         try {
1856                                 conn = new SqlConnection (connectionString);
1857                                 conn.Open ();
1858
1859                                 using (trans = conn.BeginTransaction ()) {
1860                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1861                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1862                                         cmd.ExecuteNonQuery ();
1863                                         cmd.Dispose ();
1864
1865                                         trans.Dispose ();
1866
1867                                         try {
1868                                                 trans.Save ("SAVE1");
1869                                                 Assert.Fail ("#A1");
1870                                         } catch (InvalidOperationException ex) {
1871                                                 // This SqlTransaction has completed; it is no
1872                                                 // longer usable
1873                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1874                                                 Assert.IsNull (ex.InnerException, "#A3");
1875                                                 Assert.IsNotNull (ex.Message, "#A4");
1876                                         }
1877
1878                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1879                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1880                                                 Assert.IsFalse (reader.Read (), "#B1");
1881                                         }
1882                                 }
1883                         } finally {
1884                                 if (conn != null)
1885                                         conn.Close ();
1886
1887                                 conn = new SqlConnection (connectionString);
1888                                 conn.Open ();
1889                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1890                         }
1891                 }
1892
1893                 [Test]
1894                 public void Save_Transaction_Rolledback ()
1895                 {
1896                         if (RunningOnMono)
1897                                 Assert.Ignore ("NotWorking");
1898
1899                         try {
1900                                 conn = new SqlConnection (connectionString);
1901                                 conn.Open ();
1902
1903                                 using (trans = conn.BeginTransaction ()) {
1904                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1905                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1906                                         cmd.ExecuteNonQuery ();
1907                                         cmd.Dispose ();
1908
1909                                         trans.Rollback ();
1910
1911                                         try {
1912                                                 trans.Save ("SAVE1");
1913                                                 Assert.Fail ("#A1");
1914                                         } catch (InvalidOperationException ex) {
1915                                                 // This SqlTransaction has completed; it is no
1916                                                 // longer usable
1917                                                 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
1918                                                 Assert.IsNull (ex.InnerException, "#A3");
1919                                                 Assert.IsNotNull (ex.Message, "#A4");
1920                                         }
1921
1922                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1923                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1924                                                 Assert.IsFalse (reader.Read (), "#B1");
1925                                         }
1926                                 }
1927
1928                                 using (trans = conn.BeginTransaction ()) {
1929                                         string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1930                                         SqlCommand cmd = new SqlCommand (sql, conn, trans);
1931                                         cmd.ExecuteNonQuery ();
1932                                         cmd.Dispose ();
1933
1934                                         trans.Save ("SAVE1");
1935                                         trans.Rollback ("SAVE1");
1936                                         trans.Save ("SAVE1");
1937
1938                                         cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn, trans);
1939                                         using (SqlDataReader reader = cmd.ExecuteReader ()) {
1940                                                 Assert.IsTrue (reader.Read (), "#D1");
1941                                         }
1942                                 }
1943                         } finally {
1944                                 if (conn != null)
1945                                         conn.Close ();
1946
1947                                 conn = new SqlConnection (connectionString);
1948                                 conn.Open ();
1949                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1950                         }
1951                 }
1952
1953                 [Test]
1954                 public void Save_TransactionName_Empty ()
1955                 {
1956                         if (RunningOnMono)
1957                                 Assert.Ignore ("NotWorking");
1958
1959                         try {
1960                                 conn = new SqlConnection (connectionString);
1961                                 conn.Open ();
1962
1963                                 trans = conn.BeginTransaction ();
1964
1965                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
1966                                 SqlCommand cmd = new SqlCommand (sql, conn, trans);
1967                                 cmd.ExecuteNonQuery ();
1968                                 cmd.Dispose ();
1969
1970                                 try {
1971                                         trans.Save (string.Empty);
1972                                         Assert.Fail ("#A1");
1973                                 } catch (ArgumentException ex) {
1974                                         // Invalid transaction or invalid name
1975                                         // for a point at which to save within
1976                                         // the transaction
1977                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
1978                                         Assert.IsNull (ex.InnerException, "#A3");
1979                                         Assert.IsNotNull (ex.Message, "#A4");
1980                                         Assert.IsNull (ex.ParamName, "#A5");
1981                                 }
1982
1983                                 trans.Rollback ();
1984                                 conn.Close ();
1985
1986                                 conn = new SqlConnection (connectionString);
1987                                 conn.Open ();
1988
1989                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
1990                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
1991                                         Assert.IsFalse (reader.Read (), "#B1");
1992                                 }
1993                         } finally {
1994                                 if (trans != null)
1995                                         trans.Dispose ();
1996                                 if (conn != null)
1997                                         conn.Close ();
1998
1999                                 conn = new SqlConnection (connectionString);
2000                                 conn.Open ();
2001                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
2002                         }
2003                 }
2004
2005                 [Test]
2006                 public void Save_TransactionName_Null ()
2007                 {
2008                         if (RunningOnMono)
2009                                 Assert.Ignore ("NotWorking");
2010
2011                         try {
2012                                 conn = new SqlConnection (connectionString);
2013                                 conn.Open ();
2014
2015                                 trans = conn.BeginTransaction ();
2016
2017                                 string sql = "INSERT INTO employee (id, fname, dob, doj) VALUES (6666, 'NovellBangalore', '1989-02-11', '2005-07-22')";
2018                                 SqlCommand cmd = new SqlCommand (sql, conn, trans);
2019                                 cmd.ExecuteNonQuery ();
2020                                 cmd.Dispose ();
2021
2022                                 try {
2023                                         trans.Save ((string) null);
2024                                         Assert.Fail ("#A1");
2025                                 } catch (ArgumentException ex) {
2026                                         // Invalid transaction or invalid name
2027                                         // for a point at which to save within
2028                                         // the transaction
2029                                         Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
2030                                         Assert.IsNull (ex.InnerException, "#A3");
2031                                         Assert.IsNotNull (ex.Message, "#A4");
2032                                         Assert.IsNull (ex.ParamName, "#A5");
2033                                 }
2034
2035                                 trans.Commit ();
2036                                 conn.Close ();
2037
2038                                 conn = new SqlConnection (connectionString);
2039                                 conn.Open ();
2040
2041                                 cmd = new SqlCommand ("SELECT fname FROM employee WHERE id=6666", conn);
2042                                 using (SqlDataReader reader = cmd.ExecuteReader ()) {
2043                                         Assert.IsTrue (reader.Read (), "#B1");
2044                                         Assert.AreEqual ("NovellBangalore", reader.GetString (0), "#B2");
2045                                         Assert.IsFalse (reader.Read (), "#B3");
2046                                 }
2047                         } finally {
2048                                 if (trans != null)
2049                                         trans.Dispose ();
2050                                 if (conn != null)
2051                                         conn.Close ();
2052
2053                                 conn = new SqlConnection (connectionString);
2054                                 conn.Open ();
2055                                 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
2056                         }
2057                 }
2058
2059                 int ClientVersion {
2060                         get {
2061                                 return (engine.ClientVersion);
2062                         }
2063                 }
2064
2065                 static bool RunningOnMono {
2066                         get
2067                         {
2068                                 return (Type.GetType ("System.MonoType", false) != null);
2069                         }
2070                 }
2071         }
2072 }