OdbcTransaction.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // System.Data.Odbc.OdbcTransaction
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Data;
  32. using System.Data.Common;
  33. using System.Globalization;
  34. namespace System.Data.Odbc
  35. {
  36. #if NET_2_0
  37. public sealed class OdbcTransaction : DbTransaction, IDisposable
  38. #else
  39. public sealed class OdbcTransaction : MarshalByRefObject, IDbTransaction
  40. #endif
  41. {
  42. private bool disposed;
  43. private OdbcConnection connection;
  44. private IsolationLevel isolationlevel;
  45. private bool isOpen;
  46. internal OdbcTransaction (OdbcConnection conn, IsolationLevel isolationlevel)
  47. {
  48. // Set Auto-commit (102) to false
  49. SetAutoCommit (conn, false);
  50. // Handle isolation level
  51. OdbcIsolationLevel lev = OdbcIsolationLevel.ReadCommitted;
  52. OdbcConnectionAttribute attr = OdbcConnectionAttribute.TransactionIsolation;
  53. switch (isolationlevel) {
  54. case IsolationLevel.ReadUncommitted:
  55. lev = OdbcIsolationLevel.ReadUncommitted;
  56. break;
  57. case IsolationLevel.ReadCommitted:
  58. lev = OdbcIsolationLevel.ReadCommitted;
  59. break;
  60. case IsolationLevel.RepeatableRead:
  61. lev = OdbcIsolationLevel.RepeatableRead;
  62. break;
  63. case IsolationLevel.Serializable:
  64. lev = OdbcIsolationLevel.Serializable;
  65. break;
  66. #if NET_2_0
  67. case IsolationLevel.Snapshot:
  68. // badly broken on MS:
  69. // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=305736
  70. lev = OdbcIsolationLevel.Snapshot;
  71. // SQL_ATTR_TXN_ISOLATION can be used to set all other isolation
  72. // levels except for SQL_TXN_SS_SNAPSHOT. If you want to use snapshot
  73. // isolation, you must set SQL_TXN_SS_SNAPSHOT through
  74. // SQL_COPT_SS_TXN_ISOLATION. However, you can retrieve the
  75. // isolation level by using either SQL_ATTR_TXN_ISOLATION or
  76. // SQL_COPT_SS_TXN_ISOLATION.
  77. // Source:
  78. // http://msdn2.microsoft.com/en-us/library/ms131709.aspx
  79. attr = OdbcConnectionAttribute.CoptTransactionIsolation;
  80. break;
  81. #endif
  82. case IsolationLevel.Unspecified:
  83. // when isolationlevel is not specified, then use
  84. // default isolation level of the driver and
  85. // lazy initialize it in the IsolationLevel property
  86. break;
  87. #if NET_2_0
  88. case IsolationLevel.Chaos:
  89. throw new ArgumentOutOfRangeException ("IsolationLevel",
  90. string.Format (CultureInfo.CurrentCulture,
  91. "The IsolationLevel enumeration " +
  92. "value, {0}, is not supported by " +
  93. "the .Net Framework Odbc Data " +
  94. "Provider.", (int) isolationlevel));
  95. #endif
  96. default:
  97. #if NET_2_0
  98. throw new ArgumentOutOfRangeException ("IsolationLevel",
  99. string.Format (CultureInfo.CurrentCulture,
  100. "The IsolationLevel enumeration value, {0}, is invalid.",
  101. (int) isolationlevel));
  102. #else
  103. throw new ArgumentException (string.Format (
  104. CultureInfo.InvariantCulture,
  105. "Not supported isolationlevel - {0}",
  106. isolationlevel));
  107. #endif
  108. }
  109. // only change isolation level if it was explictly set
  110. if (isolationlevel != IsolationLevel.Unspecified) {
  111. // mbd: Getting the return code of the second call to SQLSetConnectAttr is missing from original code!
  112. OdbcReturn ret = libodbc.SQLSetConnectAttr (conn.hDbc,
  113. attr, (IntPtr) lev, 0);
  114. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  115. throw new OdbcException (new OdbcError ("SQLSetConnectAttr", OdbcHandleType.Dbc, conn.hDbc));
  116. }
  117. this.isolationlevel = isolationlevel;
  118. connection = conn;
  119. isOpen = true;
  120. }
  121. // Set Auto-commit (102) connection attribute
  122. // [MonoTODO]: nice to have before svn: define libodbc.SQL_IS_UINTEGER = -5
  123. private static void SetAutoCommit (OdbcConnection conn, bool isAuto)
  124. {
  125. OdbcReturn ret = libodbc.SQLSetConnectAttr (conn.hDbc,
  126. OdbcConnectionAttribute.AutoCommit,
  127. (IntPtr) (isAuto ? 1 : 0), -5);
  128. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  129. throw new OdbcException (new OdbcError ("SQLSetConnectAttr", OdbcHandleType.Dbc, conn.hDbc));
  130. }
  131. private static IsolationLevel GetIsolationLevel (OdbcConnection conn)
  132. {
  133. int lev;
  134. int length;
  135. OdbcReturn ret = libodbc.SQLGetConnectAttr (conn.hDbc,
  136. OdbcConnectionAttribute.TransactionIsolation,
  137. out lev, 0, out length);
  138. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  139. throw new OdbcException (new OdbcError ("SQLGetConnectAttr",
  140. OdbcHandleType.Dbc, conn.hDbc));
  141. return MapOdbcIsolationLevel ((OdbcIsolationLevel) lev);
  142. }
  143. private static IsolationLevel MapOdbcIsolationLevel (OdbcIsolationLevel odbcLevel)
  144. {
  145. IsolationLevel isoLevel = IsolationLevel.Unspecified;
  146. switch (odbcLevel) {
  147. case OdbcIsolationLevel.ReadUncommitted:
  148. isoLevel = IsolationLevel.ReadUncommitted;
  149. break;
  150. case OdbcIsolationLevel.ReadCommitted:
  151. isoLevel = IsolationLevel.ReadCommitted;
  152. break;
  153. case OdbcIsolationLevel.RepeatableRead:
  154. isoLevel = IsolationLevel.RepeatableRead;
  155. break;
  156. case OdbcIsolationLevel.Serializable:
  157. isoLevel = IsolationLevel.Serializable;
  158. break;
  159. #if NET_2_0
  160. case OdbcIsolationLevel.Snapshot:
  161. isoLevel = IsolationLevel.Snapshot;
  162. break;
  163. #else
  164. default:
  165. throw new NotSupportedException (string.Format (
  166. CultureInfo.InvariantCulture,
  167. "Isolation level {0} is not supported.",
  168. odbcLevel));
  169. #endif
  170. }
  171. return isoLevel;
  172. }
  173. #region Implementation of IDisposable
  174. #if NET_2_0
  175. protected override
  176. #endif
  177. void Dispose (bool disposing)
  178. {
  179. if (!disposed) {
  180. if (disposing && isOpen)
  181. Rollback ();
  182. disposed = true;
  183. }
  184. }
  185. void IDisposable.Dispose ()
  186. {
  187. Dispose (true);
  188. GC.SuppressFinalize (this);
  189. }
  190. #endregion Implementation of IDisposable
  191. #region Implementation of IDbTransaction
  192. public
  193. #if NET_2_0
  194. override
  195. #endif //NET_2_0
  196. void Commit ()
  197. {
  198. if (!isOpen)
  199. throw ExceptionHelper.TransactionNotUsable (GetType ());
  200. if (connection.transaction == this) {
  201. OdbcReturn ret = libodbc.SQLEndTran ((short) OdbcHandleType.Dbc, connection.hDbc, 0);
  202. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  203. throw new OdbcException (new OdbcError ("SQLEndTran", OdbcHandleType.Dbc, connection.hDbc));
  204. SetAutoCommit (connection, true); // restore default auto-commit
  205. connection.transaction = null;
  206. connection = null;
  207. isOpen = false;
  208. } else
  209. throw new InvalidOperationException ();
  210. }
  211. public
  212. #if NET_2_0
  213. override
  214. #endif //NET_2_0
  215. void Rollback()
  216. {
  217. if (!isOpen)
  218. throw ExceptionHelper.TransactionNotUsable (GetType ());
  219. if (connection.transaction == this) {
  220. OdbcReturn ret = libodbc.SQLEndTran ((short) OdbcHandleType.Dbc, connection.hDbc, 1);
  221. if (ret!=OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  222. throw new OdbcException (new OdbcError ("SQLEndTran", OdbcHandleType.Dbc, connection.hDbc));
  223. SetAutoCommit (connection, true); // restore default auto-commit
  224. connection.transaction = null;
  225. connection = null;
  226. isOpen = false;
  227. } else
  228. throw new InvalidOperationException ();
  229. }
  230. #if NET_2_0
  231. protected override DbConnection DbConnection {
  232. get {
  233. return Connection;
  234. }
  235. }
  236. #else
  237. IDbConnection IDbTransaction.Connection {
  238. get {
  239. return Connection;
  240. }
  241. }
  242. #endif
  243. public
  244. #if NET_2_0
  245. override
  246. #endif
  247. IsolationLevel IsolationLevel {
  248. get {
  249. if (!isOpen)
  250. throw ExceptionHelper.TransactionNotUsable (GetType ());
  251. if (isolationlevel == IsolationLevel.Unspecified)
  252. isolationlevel = GetIsolationLevel (Connection);
  253. return isolationlevel;
  254. }
  255. }
  256. #endregion Implementation of IDbTransaction
  257. #region Public Instance Properties
  258. public new OdbcConnection Connection {
  259. get {
  260. return connection;
  261. }
  262. }
  263. #endregion Public Instance Properties
  264. }
  265. }