OdbcTransaction.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 conn.CreateOdbcException (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 conn.CreateOdbcException (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 conn.CreateOdbcException (OdbcHandleType.Dbc, conn.hDbc);
  140. return MapOdbcIsolationLevel ((OdbcIsolationLevel) lev);
  141. }
  142. private static IsolationLevel MapOdbcIsolationLevel (OdbcIsolationLevel odbcLevel)
  143. {
  144. IsolationLevel isoLevel = IsolationLevel.Unspecified;
  145. switch (odbcLevel) {
  146. case OdbcIsolationLevel.ReadUncommitted:
  147. isoLevel = IsolationLevel.ReadUncommitted;
  148. break;
  149. case OdbcIsolationLevel.ReadCommitted:
  150. isoLevel = IsolationLevel.ReadCommitted;
  151. break;
  152. case OdbcIsolationLevel.RepeatableRead:
  153. isoLevel = IsolationLevel.RepeatableRead;
  154. break;
  155. case OdbcIsolationLevel.Serializable:
  156. isoLevel = IsolationLevel.Serializable;
  157. break;
  158. #if NET_2_0
  159. case OdbcIsolationLevel.Snapshot:
  160. isoLevel = IsolationLevel.Snapshot;
  161. break;
  162. #else
  163. default:
  164. throw new NotSupportedException (string.Format (
  165. CultureInfo.InvariantCulture,
  166. "Isolation level {0} is not supported.",
  167. odbcLevel));
  168. #endif
  169. }
  170. return isoLevel;
  171. }
  172. #region Implementation of IDisposable
  173. #if NET_2_0
  174. protected override
  175. #endif
  176. void Dispose (bool disposing)
  177. {
  178. if (!disposed) {
  179. if (disposing && isOpen)
  180. Rollback ();
  181. disposed = true;
  182. }
  183. }
  184. void IDisposable.Dispose ()
  185. {
  186. Dispose (true);
  187. GC.SuppressFinalize (this);
  188. }
  189. #endregion Implementation of IDisposable
  190. #region Implementation of IDbTransaction
  191. public
  192. #if NET_2_0
  193. override
  194. #endif //NET_2_0
  195. void Commit ()
  196. {
  197. if (!isOpen)
  198. throw ExceptionHelper.TransactionNotUsable (GetType ());
  199. if (connection.transaction == this) {
  200. OdbcReturn ret = libodbc.SQLEndTran ((short) OdbcHandleType.Dbc, connection.hDbc, 0);
  201. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  202. throw connection.CreateOdbcException (OdbcHandleType.Dbc, connection.hDbc);
  203. SetAutoCommit (connection, true); // restore default auto-commit
  204. connection.transaction = null;
  205. connection = null;
  206. isOpen = false;
  207. } else
  208. throw new InvalidOperationException ();
  209. }
  210. public
  211. #if NET_2_0
  212. override
  213. #endif //NET_2_0
  214. void Rollback()
  215. {
  216. if (!isOpen)
  217. throw ExceptionHelper.TransactionNotUsable (GetType ());
  218. if (connection.transaction == this) {
  219. OdbcReturn ret = libodbc.SQLEndTran ((short) OdbcHandleType.Dbc, connection.hDbc, 1);
  220. if (ret != OdbcReturn.Success && ret != OdbcReturn.SuccessWithInfo)
  221. throw connection.CreateOdbcException (OdbcHandleType.Dbc, connection.hDbc);
  222. SetAutoCommit (connection, true); // restore default auto-commit
  223. connection.transaction = null;
  224. connection = null;
  225. isOpen = false;
  226. } else
  227. throw new InvalidOperationException ();
  228. }
  229. #if NET_2_0
  230. protected override DbConnection DbConnection {
  231. get {
  232. return Connection;
  233. }
  234. }
  235. #else
  236. IDbConnection IDbTransaction.Connection {
  237. get {
  238. return Connection;
  239. }
  240. }
  241. #endif
  242. public
  243. #if NET_2_0
  244. override
  245. #endif
  246. IsolationLevel IsolationLevel {
  247. get {
  248. if (!isOpen)
  249. throw ExceptionHelper.TransactionNotUsable (GetType ());
  250. if (isolationlevel == IsolationLevel.Unspecified)
  251. isolationlevel = GetIsolationLevel (Connection);
  252. return isolationlevel;
  253. }
  254. }
  255. #endregion Implementation of IDbTransaction
  256. #region Public Instance Properties
  257. public new OdbcConnection Connection {
  258. get {
  259. return connection;
  260. }
  261. }
  262. #endregion Public Instance Properties
  263. }
  264. }