SqlTransactionTest.cs 58 KB

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