SqlTransactionTest.cs 59 KB

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