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