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.

130 lines
4.2 KiB

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