2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient.Oci / OciTransactionHandle.cs
1 // 
2 // OciTransactionHandle.cs 
3 //  
4 // Part of managed C#/.NET library System.Data.OracleClient.dll
5 //
6 // Part of the Mono class libraries at
7 // mcs/class/System.Data.OracleClient/System.Data.OracleClient.Oci
8 //
9 // Assembly: System.Data.OracleClient.dll
10 // Namespace: System.Data.OracleClient.Oci
11 // 
12 // Author: 
13 //     Tim Coleman <tim@timcoleman.com>
14 //         
15 // Copyright (C) Tim Coleman, 2003
16 // 
17
18 using System;
19 using System.Runtime.InteropServices;
20
21 namespace System.Data.OracleClient.Oci {
22         internal sealed class OciTransactionHandle : OciHandle, IDisposable
23         {
24                 #region Fields
25
26                 bool disposed = false;
27                 OciErrorHandle errorHandle;
28                 OciServiceHandle serviceHandle;
29
30                 #endregion // Fields
31
32                 #region Constructors
33
34                 public OciTransactionHandle (OciHandle parent, IntPtr handle)
35                         : base (OciHandleType.Transaction, parent, handle)
36                 {
37                 }
38
39                 #endregion // Constructors
40
41                 #region Properties
42
43                 public OciErrorHandle ErrorHandle {
44                         get { return errorHandle; }
45                         set { errorHandle = value; }
46                 }
47
48                 public OciServiceHandle Service {
49                         get { return serviceHandle; }
50                         set { serviceHandle = value; }
51                 }
52
53                 #endregion // Properties
54
55                 #region Methods
56
57                 public void AttachToServiceContext ()
58                 {
59                         int status = 0;
60                         status = OciCalls.OCIAttrSet (Service,
61                                                         OciHandleType.Service,
62                                                         this,
63                                                         0,
64                                                         OciAttributeType.Transaction,
65                                                         ErrorHandle);
66                         if (status != 0) {
67                                 OciErrorInfo info = ErrorHandle.HandleError ();
68                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
69                         }
70                 }
71
72                 public void DetachFromServiceContext ()
73                 {
74                         int status = 0;
75                         status = OciCalls.OCIAttrSet (Service,
76                                 OciHandleType.Service,
77                                 IntPtr.Zero,
78                                 0,
79                                 OciAttributeType.Transaction,
80                                 ErrorHandle);
81                         if (status != 0) \r
82                         {
83                                 OciErrorInfo info = ErrorHandle.HandleError ();
84                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
85                         }
86                 }
87
88                 public void Begin ()
89                 {
90                         int status = 0;
91
92                         AttachToServiceContext ();
93
94                         status = OciCalls.OCITransStart (Service,
95                                                 ErrorHandle,
96                                                 60,
97                                                 OciTransactionFlags.New);
98
99                         if (status != 0) {
100                                 OciErrorInfo info = ErrorHandle.HandleError ();
101                                 throw new OracleException (info.ErrorCode, info.ErrorMessage);
102                         }
103                 }
104
105                 public void Commit ()
106                 {
107                         int status = 0;
108                         AttachToServiceContext ();
109                         try {
110                                 status = OciCalls.OCITransCommit (Service, ErrorHandle, 0);
111
112                                 if (status != 0) \r
113                                 {
114                                         OciErrorInfo info = ErrorHandle.HandleError ();
115                                         throw new OracleException (info.ErrorCode, info.ErrorMessage);
116                                 }
117                         }
118                         finally {
119                                 DetachFromServiceContext ();
120                         }
121                 }
122
123                 protected override void Dispose (bool disposing)
124                 {
125                         if (!disposed) {
126                                 disposed = true;
127                                 base.Dispose (disposing);
128                         }
129                 }
130
131                 public void Rollback ()
132                 {
133                         try {
134                                 int status = 0;
135                                 AttachToServiceContext ();
136                                 status = OciCalls.OCITransRollback (Service, ErrorHandle, 0);
137
138                                 if (status != 0) {
139                                         OciErrorInfo info = ErrorHandle.HandleError ();
140                                         throw new OracleException (info.ErrorCode, info.ErrorMessage);
141                                 }
142                         }
143                         finally {
144                                 DetachFromServiceContext ();
145                         }
146                 }
147
148                 #endregion // Methods
149         }
150 }