OdbcTransaction.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // System.Data.Odbc.OdbcTransaction
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. //
  10. // Copyright (C) 2004 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;
  32. using System.Data;
  33. #if NET_2_0
  34. using System.Data.Common;
  35. #endif // NET_2_0
  36. namespace System.Data.Odbc
  37. {
  38. #if NET_2_0
  39. public sealed class OdbcTransaction : DbTransaction
  40. #else
  41. public sealed class OdbcTransaction : MarshalByRefObject, IDbTransaction
  42. #endif // NET_2_0
  43. {
  44. private bool disposed = false;
  45. private OdbcConnection connection;
  46. private IsolationLevel isolationlevel;
  47. internal OdbcTransaction(OdbcConnection conn, IsolationLevel isolationlevel)
  48. {
  49. // Set Auto-commit (102) to false
  50. OdbcReturn ret=libodbc.SQLSetConnectAttr(conn.hDbc, OdbcConnectionAttribute.AutoCommit, IntPtr.Zero, 0);
  51. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  52. throw new OdbcException(new OdbcError("SQLSetConnectAttr",OdbcHandleType.Dbc,conn.hDbc));
  53. // Handle isolation level
  54. int lev=0;
  55. switch (isolationlevel)
  56. {
  57. case IsolationLevel.ReadUncommitted:
  58. lev=1;
  59. break;
  60. case IsolationLevel.ReadCommitted:
  61. lev=2;
  62. break;
  63. case IsolationLevel.RepeatableRead:
  64. lev=3;
  65. break;
  66. case IsolationLevel.Serializable:
  67. lev=4;
  68. break;
  69. case IsolationLevel.Unspecified:
  70. lev=0;
  71. break;
  72. default:
  73. throw new NotSupportedException();
  74. }
  75. libodbc.SQLSetConnectAttr(conn.hDbc, OdbcConnectionAttribute.TransactionIsolation, (IntPtr) lev, 0);
  76. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  77. throw new OdbcException(new OdbcError("SQLSetConnectAttr",OdbcHandleType.Dbc,conn.hDbc));
  78. this.isolationlevel=isolationlevel;
  79. connection=conn;
  80. }
  81. #region Implementation of IDisposable
  82. #if NET_2_0
  83. protected override
  84. #endif
  85. void Dispose(bool disposing) {
  86. if (!disposed) {
  87. if (disposing) {
  88. Rollback();
  89. }
  90. disposed = true;
  91. }
  92. }
  93. #if ONLY_1_1
  94. void IDisposable.Dispose() {
  95. Dispose(true);
  96. GC.SuppressFinalize(this);
  97. }
  98. #else
  99. public override void Dispose ()
  100. {
  101. Dispose (true);
  102. GC.SuppressFinalize (this);
  103. }
  104. #endif // ONLY_1_1
  105. #endregion Implementation of IDisposable
  106. #region Implementation of IDbTransaction
  107. public
  108. #if NET_2_0
  109. override
  110. #endif //NET_2_0
  111. void Commit()
  112. {
  113. if (connection.transaction==this)
  114. {
  115. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 0);
  116. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  117. throw new OdbcException(new OdbcError("SQLEndTran",OdbcHandleType.Dbc,connection.hDbc));
  118. connection.transaction=null;
  119. }
  120. else
  121. throw new InvalidOperationException();
  122. }
  123. public
  124. #if NET_2_0
  125. override
  126. #endif //NET_2_0
  127. void Rollback()
  128. {
  129. if (connection.transaction==this)
  130. {
  131. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 1);
  132. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  133. throw new OdbcException(new OdbcError("SQLEndTran",OdbcHandleType.Dbc,connection.hDbc));
  134. connection.transaction=null;
  135. }
  136. else
  137. throw new InvalidOperationException();
  138. }
  139. #if ONLY_1_1
  140. IDbConnection IDbTransaction.Connection
  141. {
  142. get
  143. {
  144. return Connection;
  145. }
  146. }
  147. #endif // ONLY_1_1
  148. #if NET_2_0
  149. protected override DbConnection DbConnection
  150. {
  151. get {return Connection;}
  152. }
  153. #endif // NET_2_0
  154. public
  155. #if NET_2_0
  156. override
  157. #endif //NET_2_0
  158. IsolationLevel IsolationLevel
  159. {
  160. get
  161. {
  162. return isolationlevel;
  163. }
  164. }
  165. #endregion Implementation of IDbTransaction
  166. #region Public Instance Properties
  167. #if ONLY_1_1
  168. public OdbcConnection Connection
  169. {
  170. get
  171. {
  172. return connection;
  173. }
  174. }
  175. #endif // ONLY_1_1
  176. #endregion Public Instance Properties
  177. }
  178. }