2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / IBM.Data.DB2 / IBM.Data.DB2 / DB2Transaction.cs
blob3c2b20ae9ed51b134ecaa63e59024afcc56aa86a
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 using System;
23 using System.Data;
24 using System.Runtime.InteropServices;
26 namespace IBM.Data.DB2
28 public sealed class DB2Transaction : MarshalByRefObject, IDbTransaction
30 private enum TransactionState
32 Open,
33 Committed,
34 Rolledback,
36 IsolationLevel isolationLevel;
37 DB2Connection db2Conn;
38 TransactionState state;
42 internal DB2Transaction(DB2Connection con, IsolationLevel isoL)
44 long db2IsoL;
45 db2Conn = con;
46 short sqlRet;
48 isolationLevel = isoL;
50 switch (isoL)
52 default:
53 case System.Data.IsolationLevel.Chaos: //No DB2equivalent, default to SQL_TXN_READ_COMMITTED
54 case System.Data.IsolationLevel.ReadCommitted: //SQL_TXN_READ_COMMITTED
55 db2IsoL = DB2Constants.SQL_TXN_READ_COMMITTED;
56 break;
57 case System.Data.IsolationLevel.ReadUncommitted: //SQL_TXN_READ_UNCOMMITTED
58 db2IsoL = DB2Constants.SQL_TXN_READ_UNCOMMITTED;
59 break;
60 case System.Data.IsolationLevel.RepeatableRead: //SQL_TXN_REPEATABLE_READ
61 db2IsoL = DB2Constants.SQL_TXN_REPEATABLE_READ;
62 break;
63 case System.Data.IsolationLevel.Serializable: //SQL_TXN_SERIALIZABLE_READ
64 db2IsoL = DB2Constants.SQL_TXN_SERIALIZABLE_READ;
65 break;
68 if(db2Conn.openConnection.autoCommit)
70 sqlRet = DB2CLIWrapper.SQLSetConnectAttr(db2Conn.DBHandle, DB2Constants.SQL_ATTR_AUTOCOMMIT, new IntPtr(DB2Constants.SQL_AUTOCOMMIT_OFF), 0);
71 DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, db2Conn.DBHandle, "Error setting AUTOCOMMIT OFF in transaction CTOR.", db2Conn);
72 db2Conn.openConnection.autoCommit = false;
74 sqlRet = DB2CLIWrapper.SQLSetConnectAttr(db2Conn.DBHandle, DB2Constants.SQL_ATTR_TXN_ISOLATION, new IntPtr(db2IsoL), 0);
75 DB2ClientUtils.DB2CheckReturn(sqlRet, DB2Constants.SQL_HANDLE_DBC, db2Conn.DBHandle, "Error setting isolation level.", db2Conn);
77 state = TransactionState.Open;
80 /// <summary>
81 /// DB2Connection associated with this transaction
82 /// </summary>
83 public IDbConnection Connection
85 get
87 return db2Conn;
90 /// <summary>
91 /// IsolationLevel property
92 /// </summary>
93 ///
94 public IsolationLevel IsolationLevel
96 get
98 CheckStateOpen();
99 return isolationLevel;
103 internal void CheckStateOpen()
105 if(state == TransactionState.Committed)
106 throw new InvalidOperationException("Transaction was already committed. It is no longer usable.");
107 if(state == TransactionState.Rolledback)
108 throw new InvalidOperationException("Transaction was already rolled back. It is no longer usable.");
111 public void Commit()
113 CheckStateOpen();
114 DB2CLIWrapper.SQLEndTran(DB2Constants.SQL_HANDLE_DBC, db2Conn.DBHandle, DB2Constants.SQL_COMMIT);
115 this.state = TransactionState.Committed;
116 this.db2Conn.openConnection.transactionOpen = false;
117 this.db2Conn.WeakRefTransaction = null;
118 this.db2Conn = null;
121 public void Rollback()
123 CheckStateOpen();
124 DB2CLIWrapper.SQLEndTran(DB2Constants.SQL_HANDLE_DBC, db2Conn.DBHandle, DB2Constants.SQL_ROLLBACK);
125 this.db2Conn.openConnection.transactionOpen = false;
126 this.state = TransactionState.Rolledback;
127 this.db2Conn.WeakRefTransaction = null;
128 this.db2Conn = null;
131 /// <summary>
132 /// Dispose method.
133 /// </summary>
134 public void Dispose()
136 if (state != TransactionState.Open)
137 return;
139 Rollback();