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.

121 lines
4.5 KiB

4 years ago
  1. #if MYSQL_6_10
  2. // Copyright (c) 2004, 2016 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.Data;
  25. using System.Data.Common;
  26. namespace Externals.MySql.Data.MySqlClient
  27. {
  28. internal sealed class MySqlTransaction : DbTransaction
  29. {
  30. private bool open;
  31. private bool disposed = false;
  32. internal MySqlTransaction(MySqlConnection c, IsolationLevel il)
  33. {
  34. Connection = c;
  35. IsolationLevel = il;
  36. open = true;
  37. }
  38. #region Destructor
  39. ~MySqlTransaction()
  40. {
  41. Dispose(false);
  42. }
  43. #endregion
  44. #region Properties
  45. /// <summary>
  46. /// Gets the <see cref="MySqlConnection"/> object associated with the transaction, or a null reference (Nothing in Visual Basic) if the transaction is no longer valid.
  47. /// </summary>
  48. /// <value>The <see cref="MySqlConnection"/> object associated with this transaction.</value>
  49. /// <remarks>
  50. /// A single application may have multiple database connections, each
  51. /// with zero or more transactions. This property enables you to
  52. /// determine the connection object associated with a particular
  53. /// transaction created by <see cref="MySqlConnection.BeginTransaction()"/>.
  54. /// </remarks>
  55. public new MySqlConnection Connection { get; }
  56. /// <summary>
  57. /// Specifies the <see cref="IsolationLevel"/> for this transaction.
  58. /// </summary>
  59. /// <value>
  60. /// The <see cref="IsolationLevel"/> for this transaction. The default is <b>ReadCommitted</b>.
  61. /// </value>
  62. /// <remarks>
  63. /// Parallel transactions are not supported. Therefore, the IsolationLevel
  64. /// applies to the entire transaction.
  65. /// </remarks>
  66. public override IsolationLevel IsolationLevel { get; }
  67. protected override DbConnection DbConnection
  68. {
  69. get { return Connection; }
  70. }
  71. #endregion
  72. protected override void Dispose(bool disposing)
  73. {
  74. if (disposed) return;
  75. base.Dispose(disposing);
  76. if (disposing)
  77. {
  78. if ((Connection != null && Connection.State == ConnectionState.Open || Connection.SoftClosed) && open)
  79. Rollback();
  80. }
  81. disposed = true;
  82. }
  83. public override void Commit()
  84. {
  85. if (Connection == null || (Connection.State != ConnectionState.Open && !Connection.SoftClosed))
  86. throw new InvalidOperationException("Connection must be valid and open to commit transaction");
  87. if (!open)
  88. throw new InvalidOperationException("Transaction has already been committed or is not pending");
  89. MySqlCommand cmd = new MySqlCommand("COMMIT", Connection);
  90. cmd.ExecuteNonQuery();
  91. open = false;
  92. }
  93. public override void Rollback()
  94. {
  95. if (Connection == null || (Connection.State != ConnectionState.Open && !Connection.SoftClosed))
  96. throw new InvalidOperationException("Connection must be valid and open to rollback transaction");
  97. if (!open)
  98. throw new InvalidOperationException("Transaction has already been rolled back or is not pending");
  99. MySqlCommand cmd = new MySqlCommand("ROLLBACK", Connection);
  100. cmd.ExecuteNonQuery();
  101. open = false;
  102. }
  103. }
  104. }
  105. #endif