OleDbTransaction_Commit.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // Copyright (c) 2006 Mainsoft Co.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Data;
  25. using System.Data.OleDb;
  26. using MonoTests.System.Data.Utils;
  27. using NUnit.Framework;
  28. namespace MonoTests.System.Data.OleDb
  29. {
  30. [TestFixture]
  31. public class OleDbTransaction_Commit : ADONetTesterClass
  32. {
  33. public static void Main()
  34. {
  35. OleDbTransaction_Commit tc = new OleDbTransaction_Commit();
  36. Exception exp = null;
  37. try
  38. {
  39. tc.BeginTest("OleDbTransaction_Commit");
  40. tc.run();
  41. }
  42. catch(Exception ex){exp = ex;}
  43. finally {tc.EndTest(exp);}
  44. }
  45. [Test]
  46. public void run()
  47. {
  48. Exception exp = null;
  49. // ORACLE does not support transactions with savepoints
  50. // http://support.microsoft.com/kb/187289/EN-US/
  51. if (ConnectedDataProvider.GetDbType(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString) == DataBaseServer.Oracle) return;
  52. if (ConnectedDataProvider.GetDbType(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString) == DataBaseServer.PostgreSQL) return;
  53. //prepare data
  54. base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  55. OleDbConnection con = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  56. con.Open();
  57. //prepare command for checking database
  58. OleDbConnection conSelect = new OleDbConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  59. conSelect.Open();
  60. OleDbCommand cmdSelect = new OleDbCommand("Select Title From Employees where EmployeeID in (100,200)", conSelect);
  61. OleDbTransaction txn = con.BeginTransaction();
  62. //prepare first transaction
  63. OleDbCommand cmd1 = new OleDbCommand("Update Employees Set Title = 'New Value1' Where EmployeeID = 100",con);
  64. cmd1.Transaction = txn;
  65. //prepare a second transaction
  66. OleDbCommand cmd2 = new OleDbCommand("Update Employees Set Title = 'New Value2' Where EmployeeID = 200",con);
  67. cmd2.Transaction = txn;
  68. try
  69. {
  70. BeginCase("one transaction - After commiting transaction");
  71. cmd1.ExecuteNonQuery();
  72. txn.Commit();
  73. string Result = cmdSelect.ExecuteScalar().ToString();
  74. Compare(Result,"New Value1" );
  75. }
  76. catch(Exception ex){exp = ex;}
  77. finally{EndCase(exp); exp = null;}
  78. //prepare data
  79. base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  80. txn = con.BeginTransaction();
  81. cmd1.Transaction = txn;
  82. cmd2.Transaction = txn;
  83. cmd1.ExecuteNonQuery();
  84. cmd2.ExecuteNonQuery();
  85. txn.Commit();
  86. OleDbDataReader rdr = cmdSelect.ExecuteReader();
  87. try
  88. {
  89. BeginCase("two transaction - check first transaction");
  90. rdr.Read();
  91. Compare(rdr.GetString(0),"New Value1" );
  92. }
  93. catch(Exception ex){exp = ex;}
  94. finally{EndCase(exp); exp = null;}
  95. try
  96. {
  97. BeginCase("two transaction - check second transaction");
  98. rdr.Read();
  99. Compare(rdr.GetString(0),"New Value2" );
  100. }
  101. catch(Exception ex){exp = ex;}
  102. finally{EndCase(exp); exp = null;}
  103. if (con.State == ConnectionState.Open) con.Close();
  104. if (conSelect.State == ConnectionState.Open) conSelect.Close();
  105. }
  106. }
  107. }