SqlTransactionTest.cs 59 KB

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