(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Npgsql / Npgsql / NpgsqlTransaction.cs
blob2f837293aa4c21ed1b51e26fa9207c3b30bce8d2
1 // created on 17/11/2002 at 19:04
3 // Npgsql.NpgsqlTransaction.cs
4 //
5 // Author:
6 // Francisco Jr. (fxjrlists@yahoo.com.br)
7 //
8 // Copyright (C) 2002 The Npgsql Development Team
9 // npgsql-general@gborg.postgresql.org
10 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 using System;
28 using System.Text;
29 using System.Resources;
30 using System.Data;
33 namespace Npgsql
35 /// <summary>
36 /// Represents a transaction to be made in a PostgreSQL database. This class cannot be inherited.
37 /// </summary>
38 public sealed class NpgsqlTransaction : MarshalByRefObject, IDbTransaction
40 private static readonly String CLASSNAME = "NpgsqlTransaction";
41 private static ResourceManager resman = new ResourceManager(typeof(NpgsqlTransaction));
43 private NpgsqlConnection _conn = null;
44 private IsolationLevel _isolation = IsolationLevel.ReadCommitted;
45 private bool _disposing = false;
47 internal NpgsqlTransaction(NpgsqlConnection conn) : this(conn, IsolationLevel.ReadCommitted)
50 internal NpgsqlTransaction(NpgsqlConnection conn, IsolationLevel isolation)
52 resman = new System.Resources.ResourceManager(this.GetType());
54 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, CLASSNAME);
55 if ((isolation != IsolationLevel.ReadCommitted) &&
56 (isolation != IsolationLevel.Serializable))
57 throw new ArgumentOutOfRangeException(resman.GetString("Exception_UnsopportedIsolationLevel"), "isolation");
59 _conn = conn;
60 _isolation = isolation;
62 StringBuilder commandText = new StringBuilder("SET TRANSACTION ISOLATION LEVEL ");
64 if (isolation == IsolationLevel.ReadCommitted)
65 commandText.Append("READ COMMITTED");
66 else
67 commandText.Append("SERIALIZABLE");
69 commandText.Append("; BEGIN");
71 NpgsqlCommand command = new NpgsqlCommand(commandText.ToString(), conn.Connector);
72 command.ExecuteNonQuery();
73 _conn.Connector.Transaction = this;
76 /// <summary>
77 /// Gets the <see cref="Npgsql.NpgsqlConnection">NpgsqlConnection</see>
78 /// object associated with the transaction, or a null reference if the
79 /// transaction is no longer valid.
80 /// </summary>
81 /// <value>The <see cref="Npgsql.NpgsqlConnection">NpgsqlConnection</see>
82 /// object associated with the transaction.</value>
83 public NpgsqlConnection Connection
85 get
87 return _conn;
92 IDbConnection IDbTransaction.Connection
94 get
96 return Connection;
100 /// <summary>
101 /// Specifies the <see cref="System.Data.IsolationLevel">IsolationLevel</see> for this transaction.
102 /// </summary>
103 /// <value>The <see cref="System.Data.IsolationLevel">IsolationLevel</see> for this transaction.
104 /// The default is <b>ReadCommitted</b>.</value>
105 public IsolationLevel IsolationLevel
109 if (_conn == null) {
110 throw new InvalidOperationException(resman.GetString("Exception_NoTransaction"));
113 return _isolation;
117 /// <summary>
118 /// Releases the unmanaged resources used by the
119 /// <see cref="Npgsql.NpgsqlTransaction">NpgsqlTransaction</see>
120 /// and optionally releases the managed resources.
121 /// </summary>
122 public void Dispose()
124 this.Dispose(true);
127 private void Dispose(bool disposing)
129 if(disposing && this._conn != null)
131 this._disposing = true;
132 if (_conn.Connector.Transaction != null)
133 this.Rollback();
137 /// <summary>
138 /// Commits the database transaction.
139 /// </summary>
140 public void Commit()
142 if (_conn == null) {
143 throw new InvalidOperationException(resman.GetString("Exception_NoTransaction"));
146 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Commit");
148 NpgsqlCommand command = new NpgsqlCommand("COMMIT", _conn.Connector);
149 command.ExecuteNonQuery();
150 _conn.Connector.Transaction = null;
151 _conn = null;
154 /// <summary>
155 /// Rolls back a transaction from a pending state.
156 /// </summary>
157 public void Rollback()
159 if (_conn == null) {
160 throw new InvalidOperationException(resman.GetString("Exception_NoTransaction"));
163 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Rollback");
165 NpgsqlCommand command = new NpgsqlCommand("ROLLBACK", _conn.Connector);
166 command.ExecuteNonQuery();
167 _conn.Connector.Transaction = null;
168 _conn = null;
171 /// <summary>
172 /// Cancel the transaction without telling the backend about it. This is
173 /// used to make the transaction go away when closing a connection.
174 /// </summary>
175 internal void Cancel()
177 if (_conn != null) {
178 _conn.Connector.Transaction = null;
179 _conn = null;
183 internal bool Disposing{
186 return _disposing;