OracleTransaction_Commit.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.OracleClient;
  26. using MonoTests.System.Data.Utils;
  27. using NUnit.Framework;
  28. namespace MonoTests.System.Data.OracleClient
  29. {
  30. [TestFixture]
  31. public class OracleTransaction_Commit : ADONetTesterClass
  32. {
  33. public static void Main()
  34. {
  35. OracleTransaction_Commit tc = new OracleTransaction_Commit();
  36. Exception exp = null;
  37. try
  38. {
  39. tc.BeginTest("OracleTransaction_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. OracleConnection con = new OracleConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  56. con.Open();
  57. //prepare command for checking database
  58. OracleConnection conSelect = new OracleConnection(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  59. conSelect.Open();
  60. OracleCommand cmdSelect = new OracleCommand("Select Title From Employees where EmployeeID in (100,200)", conSelect);
  61. OracleTransaction txn = con.BeginTransaction();
  62. //prepare first transaction
  63. OracleCommand cmd1 = new OracleCommand("Update Employees Set Title = 'New Value1' Where EmployeeID = 100",con);
  64. cmd1.Transaction = txn;
  65. //prepare a second transaction
  66. OracleCommand cmd2 = new OracleCommand("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. conSelect.Close();
  79. //prepare data
  80. base.PrepareDataForTesting(MonoTests.System.Data.Utils.ConnectedDataProvider.ConnectionString);
  81. conSelect.Open();
  82. txn = con.BeginTransaction();
  83. cmd1.Transaction = txn;
  84. cmd2.Transaction = txn;
  85. cmd1.ExecuteNonQuery();
  86. cmd2.ExecuteNonQuery();
  87. txn.Commit();
  88. OracleDataReader rdr = cmdSelect.ExecuteReader();
  89. try
  90. {
  91. BeginCase("two transaction - check first transaction");
  92. rdr.Read();
  93. Compare(rdr.GetString(0),"New Value1" );
  94. }
  95. catch(Exception ex){exp = ex;}
  96. finally{EndCase(exp); exp = null;}
  97. try
  98. {
  99. BeginCase("two transaction - check second transaction");
  100. rdr.Read();
  101. Compare(rdr.GetString(0),"New Value2" );
  102. }
  103. catch(Exception ex){exp = ex;}
  104. finally{EndCase(exp); exp = null;}
  105. if (con.State == ConnectionState.Open) con.Close();
  106. if (conSelect.State == ConnectionState.Open) conSelect.Close();
  107. }
  108. }
  109. }