You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.0 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright ?2004, 2010, Oracle and/or its affiliates. All rights reserved.
  3. //
  4. // MySQL Connector/NET is licensed under the terms of the GPLv2
  5. // <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
  6. // MySQL Connectors. There are special exceptions to the terms and
  7. // conditions of the GPLv2 as it is applied to this software, see the
  8. // FLOSS License Exception
  9. // <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
  10. //
  11. // This program is free software; you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published
  13. // by the Free Software Foundation; version 2 of the License.
  14. //
  15. // This program is distributed in the hope that it will be useful, but
  16. // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  18. // for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License along
  21. // with this program; if not, write to the Free Software Foundation, Inc.,
  22. // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. using System;
  24. using System.Transactions;
  25. using System.Collections;
  26. using System.Collections.Generic;
  27. using System.Data;
  28. using System.Data.Common;
  29. using System.Threading;
  30. namespace Externals.MySql.Data.MySqlClient
  31. {
  32. /// <summary>
  33. /// Represents a single(not nested) TransactionScope
  34. /// </summary>
  35. internal class MySqlTransactionScope
  36. {
  37. public MySqlConnection connection;
  38. public Transaction baseTransaction;
  39. public MySqlTransaction simpleTransaction;
  40. public int rollbackThreadId;
  41. public MySqlTransactionScope(MySqlConnection con, Transaction trans,
  42. MySqlTransaction simpleTransaction)
  43. {
  44. connection = con;
  45. baseTransaction = trans;
  46. this.simpleTransaction = simpleTransaction;
  47. }
  48. public void Rollback(SinglePhaseEnlistment singlePhaseEnlistment)
  49. {
  50. // prevent commands in main thread to run concurrently
  51. Driver driver = connection.driver;
  52. lock (driver)
  53. {
  54. rollbackThreadId = Thread.CurrentThread.ManagedThreadId;
  55. while (connection.Reader != null)
  56. {
  57. // wait for reader to finish. Maybe we should not wait
  58. // forever and cancel it after some time?
  59. System.Threading.Thread.Sleep(100);
  60. }
  61. simpleTransaction.Rollback();
  62. singlePhaseEnlistment.Aborted();
  63. DriverTransactionManager.RemoveDriverInTransaction(baseTransaction);
  64. driver.currentTransaction = null;
  65. if (connection.State == ConnectionState.Closed)
  66. connection.CloseFully();
  67. rollbackThreadId = 0;
  68. }
  69. }
  70. public void SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment)
  71. {
  72. simpleTransaction.Commit();
  73. singlePhaseEnlistment.Committed();
  74. DriverTransactionManager.RemoveDriverInTransaction(baseTransaction);
  75. connection.driver.currentTransaction = null;
  76. if (connection.State == ConnectionState.Closed)
  77. connection.CloseFully();
  78. }
  79. }
  80. internal sealed class MySqlPromotableTransaction : IPromotableSinglePhaseNotification, ITransactionPromoter
  81. {
  82. // Per-thread stack to manage nested transaction scopes
  83. [ThreadStatic]
  84. static Stack<MySqlTransactionScope> globalScopeStack;
  85. MySqlConnection connection;
  86. Transaction baseTransaction;
  87. Stack<MySqlTransactionScope> scopeStack;
  88. public MySqlPromotableTransaction(MySqlConnection connection, Transaction baseTransaction)
  89. {
  90. this.connection = connection;
  91. this.baseTransaction = baseTransaction;
  92. }
  93. public Transaction BaseTransaction
  94. {
  95. get
  96. {
  97. if (scopeStack.Count > 0)
  98. return scopeStack.Peek().baseTransaction;
  99. else
  100. return null;
  101. }
  102. }
  103. public bool InRollback
  104. {
  105. get
  106. {
  107. if (scopeStack.Count > 0)
  108. {
  109. MySqlTransactionScope currentScope = scopeStack.Peek();
  110. if (currentScope.rollbackThreadId == Thread.CurrentThread.ManagedThreadId)
  111. {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. }
  118. void IPromotableSinglePhaseNotification.Initialize()
  119. {
  120. string valueName = Enum.GetName(
  121. typeof(System.Transactions.IsolationLevel), baseTransaction.IsolationLevel);
  122. System.Data.IsolationLevel dataLevel = (System.Data.IsolationLevel)Enum.Parse(
  123. typeof(System.Data.IsolationLevel), valueName);
  124. MySqlTransaction simpleTransaction = connection.BeginTransaction(dataLevel);
  125. // We need to save the per-thread scope stack locally.
  126. // We cannot always use thread static variable in rollback: when scope
  127. // times out, rollback is issued by another thread.
  128. if (globalScopeStack == null)
  129. {
  130. globalScopeStack = new Stack<MySqlTransactionScope>();
  131. }
  132. scopeStack = globalScopeStack;
  133. scopeStack.Push(new MySqlTransactionScope(connection, baseTransaction,
  134. simpleTransaction));
  135. }
  136. void IPromotableSinglePhaseNotification.Rollback(SinglePhaseEnlistment singlePhaseEnlistment)
  137. {
  138. MySqlTransactionScope current = scopeStack.Peek();
  139. current.Rollback(singlePhaseEnlistment);
  140. scopeStack.Pop();
  141. }
  142. void IPromotableSinglePhaseNotification.SinglePhaseCommit(SinglePhaseEnlistment singlePhaseEnlistment)
  143. {
  144. scopeStack.Pop().SinglePhaseCommit(singlePhaseEnlistment);
  145. }
  146. byte[] ITransactionPromoter.Promote()
  147. {
  148. throw new NotSupportedException();
  149. }
  150. }
  151. internal class DriverTransactionManager
  152. {
  153. private static Hashtable driversInUse = new Hashtable();
  154. public static Driver GetDriverInTransaction(Transaction transaction)
  155. {
  156. lock (driversInUse.SyncRoot)
  157. {
  158. Driver d = (Driver)driversInUse[transaction.GetHashCode()];
  159. return d;
  160. }
  161. }
  162. public static void SetDriverInTransaction(Driver driver)
  163. {
  164. lock (driversInUse.SyncRoot)
  165. {
  166. driversInUse[driver.currentTransaction.BaseTransaction.GetHashCode()] = driver;
  167. }
  168. }
  169. public static void RemoveDriverInTransaction(Transaction transaction)
  170. {
  171. lock (driversInUse.SyncRoot)
  172. {
  173. driversInUse.Remove(transaction.GetHashCode());
  174. }
  175. }
  176. }
  177. }
  178. #endif