**** Merged from MCS ****
[mono-project.git] / mcs / class / ByteFX.Data / mysqlclient / transaction.cs
blobbc034ab9fa1e19ab31712b285e54f360a9585785
1 // ByteFX.Data data access components for .Net
2 // Copyright (C) 2002-2003 ByteFX, Inc.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 using System;
19 using System.Data;
21 namespace ByteFX.Data.MySqlClient
23 /// <summary>
24 /// Represents a SQL transaction to be made in a MySQL database. This class cannot be inherited.
25 /// </summary>
26 /// <include file='docs/MySqlTransaction.xml' path='MyDocs/MyMembers[@name="Class"]/*'/>
27 public sealed class MySqlTransaction : IDbTransaction
29 private IsolationLevel _level;
30 private MySqlConnection _conn;
31 private bool _open;
33 internal MySqlTransaction()
35 _open = true;
38 #region Properties
40 /// <summary>
41 /// 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.
42 /// </summary>
43 public IDbConnection Connection
45 get { return _conn; }
46 set { _conn = (MySqlConnection)value; }
49 /// <summary>
50 /// Specifies the <see cref="IsolationLevel"/> for this transaction.
51 /// </summary>
52 public IsolationLevel IsolationLevel
54 get { return _level; }
55 set { _level = value; }
58 #endregion
60 void System.IDisposable.Dispose()
64 /// <summary>
65 /// Commits the database transaction.
66 /// </summary>
67 public void Commit()
69 if (_conn == null || _conn.State != ConnectionState.Open)
70 throw new InvalidOperationException("Connection must be valid and open to commit transaction");
71 if (!_open)
72 throw new InvalidOperationException("Transaction has already been committed or is not pending");
73 Driver d = _conn.InternalConnection.Driver;
74 try
76 d.Send(DBCmd.QUERY, "COMMIT");
77 _open = false;
79 catch (MySqlException ex)
81 throw ex;
85 /// <summary>
86 /// Overloaded. Rolls back a transaction from a pending state.
87 /// </summary>
88 public void Rollback()
90 if (_conn == null || _conn.State != ConnectionState.Open)
91 throw new InvalidOperationException("Connection must be valid and open to commit transaction");
92 if (!_open)
93 throw new InvalidOperationException("Transaction has already been rolled back or is not pending");
94 Driver d = _conn.InternalConnection.Driver;
95 try
97 d.Send(DBCmd.QUERY, "ROLLBACK");
98 _open = false;
100 catch (MySqlException ex)
102 throw ex;