SqlTransactionTest.cs 59 KB

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