OleDbTransaction.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.Data.Common;
  32. namespace System.Data.OleDb
  33. {
  34. #if NET_2_0
  35. public sealed class OleDbTransaction : DbTransaction, IDbTransaction
  36. #else
  37. public sealed class OleDbTransaction : MarshalByRefObject, IDbTransaction, IDisposable
  38. #endif
  39. {
  40. #region Fields
  41. bool disposed;
  42. OleDbConnection connection;
  43. IntPtr gdaTransaction;
  44. int depth;
  45. bool isOpen;
  46. #endregion // Fields
  47. #region Constructors
  48. internal OleDbTransaction (OleDbConnection connection, int depth)
  49. : this (connection, depth, IsolationLevel.ReadCommitted)
  50. {
  51. }
  52. internal OleDbTransaction (OleDbConnection connection)
  53. : this (connection, 1)
  54. {
  55. }
  56. internal OleDbTransaction (OleDbConnection connection, int depth, IsolationLevel isolevel)
  57. {
  58. this.connection = connection;
  59. gdaTransaction = libgda.gda_transaction_new (depth.ToString ());
  60. switch (isolevel) {
  61. case IsolationLevel.ReadCommitted :
  62. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  63. GdaTransactionIsolation.ReadCommitted);
  64. break;
  65. case IsolationLevel.ReadUncommitted :
  66. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  67. GdaTransactionIsolation.ReadUncommitted);
  68. break;
  69. case IsolationLevel.RepeatableRead :
  70. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  71. GdaTransactionIsolation.RepeatableRead);
  72. break;
  73. case IsolationLevel.Serializable :
  74. libgda.gda_transaction_set_isolation_level (gdaTransaction,
  75. GdaTransactionIsolation.Serializable);
  76. break;
  77. }
  78. libgda.gda_connection_begin_transaction (connection.GdaConnection, gdaTransaction);
  79. isOpen = true;
  80. }
  81. internal OleDbTransaction (OleDbConnection connection, IsolationLevel isolevel)
  82. : this (connection, 1, isolevel)
  83. {
  84. }
  85. #endregion // Constructors
  86. #region Properties
  87. public new OleDbConnection Connection {
  88. get {
  89. return connection;
  90. }
  91. }
  92. #if NET_2_0
  93. protected override DbConnection DbConnection {
  94. get { return connection; }
  95. }
  96. #endif
  97. IDbConnection IDbTransaction.Connection {
  98. get {
  99. return connection;
  100. }
  101. }
  102. public
  103. #if NET_2_0
  104. override
  105. #endif
  106. IsolationLevel IsolationLevel {
  107. get {
  108. if (!isOpen)
  109. throw ExceptionHelper.TransactionNotUsable (GetType ());
  110. switch (libgda.gda_transaction_get_isolation_level (gdaTransaction)) {
  111. case GdaTransactionIsolation.ReadCommitted :
  112. return IsolationLevel.ReadCommitted;
  113. case GdaTransactionIsolation.ReadUncommitted :
  114. return IsolationLevel.ReadUncommitted;
  115. case GdaTransactionIsolation.RepeatableRead :
  116. return IsolationLevel.RepeatableRead;
  117. case GdaTransactionIsolation.Serializable :
  118. return IsolationLevel.Serializable;
  119. }
  120. return IsolationLevel.Unspecified;
  121. }
  122. }
  123. #endregion // Properties
  124. #region Methods
  125. public OleDbTransaction Begin ()
  126. {
  127. if (!isOpen)
  128. throw ExceptionHelper.TransactionNotUsable (GetType ());
  129. return new OleDbTransaction (connection, depth + 1);
  130. }
  131. public OleDbTransaction Begin (IsolationLevel isolevel)
  132. {
  133. if (!isOpen)
  134. throw ExceptionHelper.TransactionNotUsable (GetType ());
  135. return new OleDbTransaction (connection, depth + 1, isolevel);
  136. }
  137. public
  138. #if NET_2_0
  139. override
  140. #endif
  141. void Commit ()
  142. {
  143. if (!isOpen)
  144. throw ExceptionHelper.TransactionNotUsable (GetType ());
  145. if (!libgda.gda_connection_commit_transaction (connection.GdaConnection,
  146. gdaTransaction))
  147. throw new InvalidOperationException ();
  148. connection = null;
  149. isOpen = false;
  150. }
  151. #if ONLY_1_1
  152. ~OleDbTransaction ()
  153. {
  154. libgda.FreeObject (gdaTransaction);
  155. gdaTransaction = IntPtr.Zero;
  156. }
  157. #endif
  158. #if NET_2_0
  159. protected override
  160. #endif
  161. void Dispose (bool disposing)
  162. {
  163. if (!disposed) {
  164. if (disposing && isOpen)
  165. Rollback ();
  166. disposed = true;
  167. }
  168. #if NET_2_0
  169. base.Dispose (disposing);
  170. #endif
  171. }
  172. void IDisposable.Dispose ()
  173. {
  174. Dispose (true);
  175. }
  176. public
  177. #if NET_2_0
  178. override
  179. #endif
  180. void Rollback ()
  181. {
  182. if (!isOpen)
  183. throw ExceptionHelper.TransactionNotUsable (GetType ());
  184. if (!libgda.gda_connection_rollback_transaction (connection.GdaConnection,
  185. gdaTransaction))
  186. throw new InvalidOperationException ();
  187. connection = null;
  188. isOpen = false;
  189. }
  190. #endregion // Methods
  191. }
  192. }