New test.
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlTransaction.cs
1 //
2 // System.Data.SqlClient.SqlTransaction.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) Tim Coleman, 2002
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Data;
35 using System.Data.Common;
36
37 namespace System.Data.SqlClient {
38 #if NET_2_0
39         public sealed class SqlTransaction : DbTransaction, IDbTransaction, IDisposable
40 #else
41         public sealed class SqlTransaction : MarshalByRefObject, IDbTransaction, IDisposable
42 #endif // NET_2_0
43         {
44                 #region Fields
45
46                 bool disposed = false;
47                 SqlConnection connection;
48                 IsolationLevel isolationLevel;
49                 bool isOpen;
50                 bool isRolledBack = false;
51
52                 #endregion
53
54                 #region Constructors
55
56                 internal SqlTransaction (SqlConnection connection, IsolationLevel isolevel)
57                 {
58                         this.connection = connection;
59                         this.isolationLevel = isolevel;
60                         isOpen = true;
61                         isRolledBack = false;
62                 }
63
64                 #endregion // Constructors
65
66                 #region Properties
67
68                 public 
69 #if NET_2_0
70                 new
71 #endif // NET_2_0
72          SqlConnection Connection {
73                         get { return connection; }
74                 }
75
76                 internal bool IsOpen {
77                         get { return isOpen; }
78                 }
79
80                 public 
81 #if NET_2_0
82                 override
83 #endif // NET_2_0
84          IsolationLevel IsolationLevel {
85                         get { return isolationLevel; }
86                 }
87                 
88                 IDbConnection IDbTransaction.Connection {
89                         get { return Connection; }
90                 }
91
92                 #endregion // Properties
93                
94                 #region Methods
95
96                 public 
97 #if NET_2_0
98                 override
99 #endif // NET_2_0
100          void Commit ()
101                 {
102                         if (!isOpen)
103                                 throw new InvalidOperationException ("The Transaction was not open.");
104                         connection.Tds.Execute ("COMMIT TRANSACTION");
105                         connection.Transaction = null;
106                         isOpen = false;
107                 }               
108 #if NET_2_0
109                 protected override
110 #endif
111                 void Dispose (bool disposing)
112                 {
113                         if (!disposed)  {
114                                 if (disposing) {
115                                         if (isOpen) // in case it is called in the dispose of the class, then the isOpen is already false 
116                                                 Rollback ();
117                                 }
118                                 disposed = true;
119                         }
120                 }
121
122 #if !NET_2_0
123                 public 
124 #endif
125                 void Dispose ()
126                 {
127                         Dispose (true);
128                         GC.SuppressFinalize (this);
129                 }
130
131                 public 
132 #if NET_2_0
133                 override
134 #endif // NET_2_0
135          void Rollback ()
136                 {
137                         Rollback (String.Empty);
138                 }
139
140                 public void Rollback (string transactionName)
141                 {
142                         if (!isRolledBack) {
143                                 if (!isOpen)
144                                         throw new InvalidOperationException ("The Transaction was not open.");
145                                 connection.Tds.Execute (String.Format ("ROLLBACK TRANSACTION {0}", transactionName));
146                                 isRolledBack = true;
147                                 isOpen = false;
148                                 connection.Transaction = null;
149                                 connection = null;
150                         }
151                                 
152                 }
153
154                 public void Save (string savePointName)
155                 {
156                         if (!isOpen)
157                                 throw new InvalidOperationException ("The Transaction was not open.");
158                         connection.Tds.Execute (String.Format ("SAVE TRANSACTION {0}", savePointName));
159                 }
160 #if NET_2_0
161                 protected override DbConnection DbConnection
162                 {
163                         get {return (DbConnection) Connection;}
164                 }
165 #endif // NET_2_0
166                 #endregion // Methods
167         }
168 }