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.

133 lines
4.5 KiB

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