[runtime] Overwrite stacktrace for exception on re-throw. Fixes #1856.
[mono.git] / mcs / class / System.Data / System.Data.OleDb / OleDbTransaction.cs
1 //
2 // System.Data.OleDb.OleDbTransaction
3 //
4 // Authors:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Rodrigo Moya, 2002
9 // Copyright (C) Tim Coleman, 2002
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Data.Common;
33
34 namespace System.Data.OleDb
35 {
36         public sealed class OleDbTransaction : DbTransaction, IDbTransaction
37         {
38                 #region Fields
39
40                 bool disposed;
41                 OleDbConnection connection;
42                 IntPtr gdaTransaction;
43                 int depth;
44                 bool isOpen;
45
46                 #endregion // Fields
47
48                 #region Constructors
49
50                 internal OleDbTransaction (OleDbConnection connection, int depth)
51                         : this (connection, depth, IsolationLevel.ReadCommitted)
52                 {
53                 }
54
55                 internal OleDbTransaction (OleDbConnection connection)
56                         : this (connection, 1)
57                 {
58                 }
59
60                 internal OleDbTransaction (OleDbConnection connection, int depth, IsolationLevel isolevel) 
61                 {
62                         this.connection = connection;
63
64                         gdaTransaction = libgda.gda_transaction_new (depth.ToString ());
65                         
66                         switch (isolevel) {
67                         case IsolationLevel.ReadCommitted :
68                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
69                                                                             GdaTransactionIsolation.ReadCommitted);
70                                 break;
71                         case IsolationLevel.ReadUncommitted :
72                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
73                                                                             GdaTransactionIsolation.ReadUncommitted);
74                                 break;
75                         case IsolationLevel.RepeatableRead :
76                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
77                                                                             GdaTransactionIsolation.RepeatableRead);
78                                 break;
79                         case IsolationLevel.Serializable :
80                                 libgda.gda_transaction_set_isolation_level (gdaTransaction,
81                                                                             GdaTransactionIsolation.Serializable);
82                                 break;
83                         }
84                         
85                         libgda.gda_connection_begin_transaction (connection.GdaConnection, gdaTransaction);
86                         isOpen = true;
87                 }
88
89                 internal OleDbTransaction (OleDbConnection connection, IsolationLevel isolevel) 
90                         : this (connection, 1, isolevel)
91                 {
92                 }
93
94
95                 #endregion // Constructors
96
97                 #region Properties
98
99                 public new OleDbConnection Connection {
100                         get {
101                                 return connection;
102                         }
103                 }
104
105                 protected override DbConnection DbConnection {
106                         get { return connection; }
107                 }
108                 
109                 public
110                 override
111                 IsolationLevel IsolationLevel {
112                         get {
113                                 if (!isOpen)
114                                         throw ExceptionHelper.TransactionNotUsable (GetType ());
115
116                                 switch (libgda.gda_transaction_get_isolation_level (gdaTransaction)) {
117                                 case GdaTransactionIsolation.ReadCommitted :
118                                         return IsolationLevel.ReadCommitted;
119                                 case GdaTransactionIsolation.ReadUncommitted :
120                                         return IsolationLevel.ReadUncommitted;
121                                 case GdaTransactionIsolation.RepeatableRead :
122                                         return IsolationLevel.RepeatableRead;
123                                 case GdaTransactionIsolation.Serializable :
124                                         return IsolationLevel.Serializable;
125                                 }
126                                 return IsolationLevel.Unspecified;
127                         }
128                 }
129
130                 #endregion // Properties
131
132                 #region Methods
133
134                 public OleDbTransaction Begin ()
135                 {
136                         if (!isOpen)
137                                 throw ExceptionHelper.TransactionNotUsable (GetType ());
138                         return new OleDbTransaction (connection, depth + 1);
139                 }
140
141                 public OleDbTransaction Begin (IsolationLevel isolevel) 
142                 {
143                         if (!isOpen)
144                                 throw ExceptionHelper.TransactionNotUsable (GetType ());
145                         return new OleDbTransaction (connection, depth + 1, isolevel);
146                 }
147
148                 public
149                 override
150                 void Commit ()
151                 {
152                         if (!isOpen)
153                                 throw ExceptionHelper.TransactionNotUsable (GetType ());
154
155                         if (!libgda.gda_connection_commit_transaction (connection.GdaConnection,
156                                 gdaTransaction))
157                                 throw new InvalidOperationException ();
158                         connection = null;
159                         isOpen = false;
160                 }
161
162
163                 protected override
164                 void Dispose (bool disposing)
165                 {
166                         if (!disposed) {
167                                 if (disposing && isOpen)
168                                         Rollback ();
169                                 disposed = true;
170                         }
171
172                         base.Dispose (disposing);
173                 }
174
175
176                 public
177                 override
178                 void Rollback ()
179                 {
180                         if (!isOpen)
181                                 throw ExceptionHelper.TransactionNotUsable (GetType ());
182
183                         if (!libgda.gda_connection_rollback_transaction (connection.GdaConnection,
184                                 gdaTransaction))
185                                 throw new InvalidOperationException ();
186                         connection = null;
187                         isOpen = false;
188                 }
189
190                 #endregion // Methods
191         }
192 }