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