SqlTransactionTest.cs 59 KB

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