OleDbTransaction.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // System.Data.OleDb.OleDbTransaction
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Rodrigo Moya, 2002
  9. // Copyright (C) Tim Coleman, 2002
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.Data;
  34. using System.Data.Common;
  35. namespace System.Data.OleDb
  36. {
  37. public sealed class OleDbTransaction : MarshalByRefObject, IDbTransaction, IDisposable
  38. {
  39. #region Fields
  40. OleDbConnection connection;
  41. IntPtr gdaTransaction;
  42. int depth;
  43. #endregion // Fields
  44. #region Constructors
  45. internal OleDbTransaction (OleDbConnection connection, int depth)
  46. : this (connection, depth, IsolationLevel.ReadCommitted)
  47. {
  48. }
  49. internal OleDbTransaction (OleDbConnection connection)
  50. : this (connection, 1)
  51. {
  52. }
  53. internal OleDbTransaction (OleDbConnection connection, int depth, IsolationLevel isolevel)
  54. {
  55. this.connection = connection;
  56. gdaTransaction = libgda.gda_transaction_new (depth.ToString ());
  57. switch (isolevel) {
  58. case IsolationLevel.ReadCommitted :
  59. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  60. GdaTransactionIsolation.ReadCommitted);
  61. break;
  62. case IsolationLevel.ReadUncommitted :
  63. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  64. GdaTransactionIsolation.ReadUncommitted);
  65. break;
  66. case IsolationLevel.RepeatableRead :
  67. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  68. GdaTransactionIsolation.RepeatableRead);
  69. break;
  70. case IsolationLevel.Serializable :
  71. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  72. GdaTransactionIsolation.Serializable);
  73. break;
  74. }
  75. libgda.gda_connection_begin_transaction (connection.GdaConnection, gdaTransaction);
  76. }
  77. internal OleDbTransaction (OleDbConnection connection, IsolationLevel isolevel)
  78. : this (connection, 1, isolevel)
  79. {
  80. }
  81. #endregion // Constructors
  82. #region Properties
  83. public OleDbConnection Connection {
  84. get {
  85. return connection;
  86. }
  87. }
  88. IDbConnection IDbTransaction.Connection {
  89. get {
  90. return connection;
  91. }
  92. }
  93. public IsolationLevel IsolationLevel {
  94. get {
  95. switch (libgda.gda_transaction_get_isolation_level (gdaTransaction)) {
  96. case GdaTransactionIsolation.ReadCommitted :
  97. return IsolationLevel.ReadCommitted;
  98. case GdaTransactionIsolation.ReadUncommitted :
  99. return IsolationLevel.ReadUncommitted;
  100. case GdaTransactionIsolation.RepeatableRead :
  101. return IsolationLevel.RepeatableRead;
  102. case GdaTransactionIsolation.Serializable :
  103. return IsolationLevel.Serializable;
  104. }
  105. return IsolationLevel.Unspecified;
  106. }
  107. }
  108. #endregion // Properties
  109. #region Methods
  110. public OleDbTransaction Begin ()
  111. {
  112. return new OleDbTransaction (connection, depth + 1);
  113. }
  114. public OleDbTransaction Begin (IsolationLevel isolevel)
  115. {
  116. return new OleDbTransaction (connection, depth + 1, isolevel);
  117. }
  118. public void Commit ()
  119. {
  120. if (!libgda.gda_connection_commit_transaction (connection.GdaConnection,
  121. gdaTransaction))
  122. throw new InvalidOperationException ();
  123. }
  124. [MonoTODO]
  125. ~OleDbTransaction ()
  126. {
  127. libgda.FreeObject (gdaTransaction);
  128. gdaTransaction = IntPtr.Zero;
  129. }
  130. [MonoTODO]
  131. void IDisposable.Dispose ()
  132. {
  133. throw new NotImplementedException ();
  134. }
  135. public void Rollback ()
  136. {
  137. if (!libgda.gda_connection_rollback_transaction (connection.GdaConnection,
  138. gdaTransaction))
  139. throw new InvalidOperationException ();
  140. }
  141. #endregion // Methods
  142. }
  143. }