2
0

OdbcTransaction.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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, IDisposable
  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. SetAutoCommit(conn, false);
  51. // Handle isolation level
  52. int lev=0;
  53. switch (isolationlevel)
  54. {
  55. case IsolationLevel.ReadUncommitted:
  56. lev=1;
  57. break;
  58. case IsolationLevel.ReadCommitted:
  59. lev=2;
  60. break;
  61. case IsolationLevel.RepeatableRead:
  62. lev=3;
  63. break;
  64. case IsolationLevel.Serializable:
  65. lev=4;
  66. break;
  67. case IsolationLevel.Unspecified:
  68. lev=0;
  69. break;
  70. default:
  71. throw new NotSupportedException();
  72. }
  73. // mbd: Getting the return code of the second call to SQLSetConnectAttr is missing from original code!
  74. OdbcReturn ret = libodbc.SQLSetConnectAttr(conn.hDbc, OdbcConnectionAttribute.TransactionIsolation, (IntPtr) lev, 0);
  75. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  76. throw new OdbcException(new OdbcError("SQLSetConnectAttr",OdbcHandleType.Dbc,conn.hDbc));
  77. this.isolationlevel=isolationlevel;
  78. connection=conn;
  79. }
  80. // Set Auto-commit (102) connection attribute
  81. // [MonoTODO]: nice to have before svn: define libodbc.SQL_IS_UINTEGER = -5
  82. private static void SetAutoCommit(OdbcConnection conn, bool isAuto)
  83. {
  84. OdbcReturn ret=libodbc.SQLSetConnectAttr(
  85. conn.hDbc, OdbcConnectionAttribute.AutoCommit, (IntPtr)(isAuto ? 1 : 0), -5
  86. );
  87. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  88. throw new OdbcException(new OdbcError("SQLSetConnectAttr",OdbcHandleType.Dbc,conn.hDbc));
  89. }
  90. #region Implementation of IDisposable
  91. #if NET_2_0
  92. protected override
  93. #endif
  94. void Dispose(bool disposing)
  95. {
  96. if (!disposed) {
  97. if (disposing) {
  98. Rollback();
  99. }
  100. disposed = true;
  101. }
  102. }
  103. void IDisposable.Dispose()
  104. {
  105. Dispose(true);
  106. GC.SuppressFinalize(this);
  107. }
  108. #endregion Implementation of IDisposable
  109. #region Implementation of IDbTransaction
  110. public
  111. #if NET_2_0
  112. override
  113. #endif //NET_2_0
  114. void Commit()
  115. {
  116. if (connection.transaction==this)
  117. {
  118. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 0);
  119. if ((ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  120. throw new OdbcException (new OdbcError ("SQLEndTran", OdbcHandleType.Dbc, connection.hDbc));
  121. SetAutoCommit(connection, true); // restore default auto-commit
  122. connection.transaction=null;
  123. }
  124. else
  125. throw new InvalidOperationException();
  126. }
  127. public
  128. #if NET_2_0
  129. override
  130. #endif //NET_2_0
  131. void Rollback()
  132. {
  133. if (connection.transaction==this)
  134. {
  135. OdbcReturn ret=libodbc.SQLEndTran((short) OdbcHandleType.Dbc, connection.hDbc, 1);
  136. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  137. throw new OdbcException(new OdbcError("SQLEndTran",OdbcHandleType.Dbc,connection.hDbc));
  138. SetAutoCommit(connection, true); // restore default auto-commit
  139. connection.transaction=null;
  140. }
  141. else
  142. throw new InvalidOperationException();
  143. }
  144. #if ONLY_1_1
  145. IDbConnection IDbTransaction.Connection {
  146. get {
  147. return Connection;
  148. }
  149. }
  150. #endif // ONLY_1_1
  151. #if NET_2_0
  152. protected override DbConnection DbConnection {
  153. get {
  154. return Connection;
  155. }
  156. }
  157. #endif // NET_2_0
  158. public
  159. #if NET_2_0
  160. override
  161. #endif //NET_2_0
  162. IsolationLevel IsolationLevel {
  163. get {
  164. return isolationlevel;
  165. }
  166. }
  167. #endregion Implementation of IDbTransaction
  168. #region Public Instance Properties
  169. public new OdbcConnection Connection {
  170. get {
  171. return connection;
  172. }
  173. }
  174. #endregion Public Instance Properties
  175. }
  176. }