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:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
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.
24 using System
.Runtime
.InteropServices
;
26 namespace IBM
.Data
.DB2
28 public sealed class DB2Transaction
: MarshalByRefObject
, IDbTransaction
30 private enum TransactionState
36 IsolationLevel isolationLevel
;
37 DB2Connection db2Conn
;
38 TransactionState state
;
42 internal DB2Transaction(DB2Connection con
, IsolationLevel isoL
)
48 isolationLevel
= isoL
;
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
;
57 case System
.Data
.IsolationLevel
.ReadUncommitted
: //SQL_TXN_READ_UNCOMMITTED
58 db2IsoL
= DB2Constants
.SQL_TXN_READ_UNCOMMITTED
;
60 case System
.Data
.IsolationLevel
.RepeatableRead
: //SQL_TXN_REPEATABLE_READ
61 db2IsoL
= DB2Constants
.SQL_TXN_REPEATABLE_READ
;
63 case System
.Data
.IsolationLevel
.Serializable
: //SQL_TXN_SERIALIZABLE_READ
64 db2IsoL
= DB2Constants
.SQL_TXN_SERIALIZABLE_READ
;
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
;
81 /// DB2Connection associated with this transaction
83 public IDbConnection Connection
91 /// IsolationLevel property
94 public IsolationLevel IsolationLevel
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.");
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;
121 public void Rollback()
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;
134 public void Dispose()
136 if (state
!= TransactionState
.Open
)