File: MySqlTransaction.xml

package info (click to toggle)
mysql-connector-net 6.4.3-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,160 kB
  • ctags: 8,552
  • sloc: cs: 63,689; xml: 7,505; sql: 345; makefile: 50; ansic: 40
file content (329 lines) | stat: -rw-r--r-- 12,022 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<docs>
  <Class>
    <summary>
      Represents a SQL transaction to be made in a MySQL database. This class cannot be inherited.
    </summary>

    <remarks>
      The application creates a <B>MySqlTransaction</B> object by calling <see cref="MySqlConnection.BeginTransaction()"/>
      on the <see cref="MySqlConnection"/> object. All subsequent operations associated with the
      transaction (for example, committing or aborting the transaction), are performed on the
      <B>MySqlTransaction</B> object.
    </remarks>

    <example>
      The following example creates a <see cref="MySqlConnection"/> and a <B>MySqlTransaction</B>.
      It also demonstrates how to use the <see cref="MySqlConnection.BeginTransaction()"/>,
      <see cref="MySqlTransaction.Commit"/>, and <see cref="MySqlTransaction.Rollback"/> methods.
      <code lang="vbnet">
        Public Sub RunTransaction(myConnString As String)
        Dim myConnection As New MySqlConnection(myConnString)
        myConnection.Open()

        Dim myCommand As MySqlCommand = myConnection.CreateCommand()
        Dim myTrans As MySqlTransaction

        ' Start a local transaction
        myTrans = myConnection.BeginTransaction()
        ' Must assign both transaction object and connection
        ' to Command object for a pending local transaction
        myCommand.Connection = myConnection
        myCommand.Transaction = myTrans

        Try
        myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')"
        myCommand.ExecuteNonQuery()
        myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"
        myCommand.ExecuteNonQuery()
        myTrans.Commit()
        Console.WriteLine("Both records are written to database.")
        Catch e As Exception
        Try
        myTrans.Rollback()
        Catch ex As MySqlException
        If Not myTrans.Connection Is Nothing Then
        Console.WriteLine("An exception of type " &amp; ex.GetType().ToString() &amp; _
        " was encountered while attempting to roll back the transaction.")
        End If
        End Try

        Console.WriteLine("An exception of type " &amp; e.GetType().ToString() &amp; _
        "was encountered while inserting the data.")
        Console.WriteLine("Neither record was written to database.")
        Finally
        myConnection.Close()
        End Try
        End Sub 'RunTransaction
      </code>
      <code lang="C#">
        public void RunTransaction(string myConnString)
        {
        MySqlConnection myConnection = new MySqlConnection(myConnString);
        myConnection.Open();

        MySqlCommand myCommand = myConnection.CreateCommand();
        MySqlTransaction myTrans;

        // Start a local transaction
        myTrans = myConnection.BeginTransaction();
        // Must assign both transaction object and connection
        // to Command object for a pending local transaction
        myCommand.Connection = myConnection;
        myCommand.Transaction = myTrans;

        try
        {
        myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
        myCommand.ExecuteNonQuery();
        myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";
        myCommand.ExecuteNonQuery();
        myTrans.Commit();
        Console.WriteLine("Both records are written to database.");
        }
        catch(Exception e)
        {
        try
        {
        myTrans.Rollback();
        }
        catch (MySqlException ex)
        {
        if (myTrans.Connection != null)
        {
        Console.WriteLine("An exception of type " + ex.GetType() +
        " was encountered while attempting to roll back the transaction.");
        }
        }

        Console.WriteLine("An exception of type " + e.GetType() +
        " was encountered while inserting the data.");
        Console.WriteLine("Neither record was written to database.");
        }
        finally
        {
        myConnection.Close();
        }
        }
      </code>
    </example>


  </Class>

  <Rollback>
    <summary>
      Rolls back a transaction from a pending state.
    </summary>
    <remarks>
      The Rollback method is equivalent to the MySQL statement ROLLBACK.
      The transaction can only be rolled back from a pending state
      (after BeginTransaction has been called, but before Commit is
      called).
    </remarks>
    <example>
      The following example creates a <see cref="MySqlConnection"/> and a
      <see cref="MySqlTransaction"/>. It also demonstrates how to use the
      <see cref="MySqlConnection.BeginTransaction()"/>, <see cref="Commit"/>, and <B>Rollback</B>
      methods.
      <code lang="vbnet">
        Public Sub RunSqlTransaction(myConnString As String)
        Dim myConnection As New MySqlConnection(myConnString)
        myConnection.Open()

        Dim myCommand As MySqlCommand = myConnection.CreateCommand()
        Dim myTrans As MySqlTransaction

        ' Start a local transaction
        myTrans = myConnection.BeginTransaction()

        ' Must assign both transaction object and connection
        ' to Command object for a pending local transaction
        myCommand.Connection = myConnection
        myCommand.Transaction = myTrans

        Try
        myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')"
        myCommand.ExecuteNonQuery()
        myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')"
        myCommand.ExecuteNonQuery()
        myTrans.Commit()
        Console.WriteLine("Success.")
        Catch e As Exception
        Try
        myTrans.Rollback()
        Catch ex As MySqlException
        If Not myTrans.Connection Is Nothing Then
        Console.WriteLine("An exception of type " &amp; ex.GetType().ToString() &amp; _
        " was encountered while attempting to roll back the transaction.")
        End If
        End Try

        Console.WriteLine("An exception of type " &amp; e.GetType().ToString() &amp; _
        "was encountered while inserting the data.")
        Console.WriteLine("Neither record was written to database.")
        Finally
        myConnection.Close()
        End Try
        End Sub
      </code>
      <code lang="C#">
        public void RunSqlTransaction(string myConnString)
        {
        MySqlConnection myConnection = new MySqlConnection(myConnString);
        myConnection.Open();

        MySqlCommand myCommand = myConnection.CreateCommand();
        MySqlTransaction myTrans;

        // Start a local transaction
        myTrans = myConnection.BeginTransaction();
        // Must assign both transaction object and connection
        // to Command object for a pending local transaction
        myCommand.Connection = myConnection;
        myCommand.Transaction = myTrans;

        try
        {
        myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')";
        myCommand.ExecuteNonQuery();
        myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')";
        myCommand.ExecuteNonQuery();
        myTrans.Commit();
        Console.WriteLine("Both records are written to database.");
        }
        catch(Exception e)
        {
        try
        {
        myTrans.Rollback();
        }
        catch (MySqlException ex)
        {
        if (myTrans.Connection != null)
        {
        Console.WriteLine("An exception of type " + ex.GetType() +
        " was encountered while attempting to roll back the transaction.");
        }
        }

        Console.WriteLine("An exception of type " + e.GetType() +
        " was encountered while inserting the data.");
        Console.WriteLine("Neither record was written to database.");
        }
        finally
        {
        myConnection.Close();
        }
        }
      </code>
    </example>
  </Rollback>

  <Commit>
    <summary>
      Commits the database transaction.
    </summary>
    <remarks>
      The <b>Commit</b> method is equivalent to the MySQL SQL statement
      COMMIT.
    </remarks>
    <example>
      The following example creates a <see cref="MySqlConnection"/> and a
      <see cref="MySqlTransaction"/>. It also demonstrates how to use the
      <see cref="MySqlConnection.BeginTransaction()"/>, <see cref="Commit"/>, and <B>Rollback</B>
      methods.
      <code lang="vbnet">
        Public Sub RunSqlTransaction(myConnString As String)
        Dim myConnection As New MySqlConnection(myConnString)
        myConnection.Open()

        Dim myCommand As MySqlCommand = myConnection.CreateCommand()
        Dim myTrans As MySqlTransaction

        ' Start a local transaction
        myTrans = myConnection.BeginTransaction()

        ' Must assign both transaction object and connection
        ' to Command object for a pending local transaction
        myCommand.Connection = myConnection
        myCommand.Transaction = myTrans

        Try
        myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')"
        myCommand.ExecuteNonQuery()
        myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')"
        myCommand.ExecuteNonQuery()
        myTrans.Commit()
        Console.WriteLine("Success.")
        Catch e As Exception
        Try
        myTrans.Rollback()
        Catch ex As MySqlException
        If Not myTrans.Connection Is Nothing Then
        Console.WriteLine("An exception of type " &amp; ex.GetType().ToString() &amp; _
        " was encountered while attempting to roll back the transaction.")
        End If
        End Try

        Console.WriteLine("An exception of type " &amp; e.GetType().ToString() &amp; _
        "was encountered while inserting the data.")
        Console.WriteLine("Neither record was written to database.")
        Finally
        myConnection.Close()
        End Try
        End Sub
      </code>
      <code lang="C#">
        public void RunSqlTransaction(string myConnString)
        {
        MySqlConnection myConnection = new MySqlConnection(myConnString);
        myConnection.Open();

        MySqlCommand myCommand = myConnection.CreateCommand();
        MySqlTransaction myTrans;

        // Start a local transaction
        myTrans = myConnection.BeginTransaction();
        // Must assign both transaction object and connection
        // to Command object for a pending local transaction
        myCommand.Connection = myConnection;
        myCommand.Transaction = myTrans;

        try
        {
        myCommand.CommandText = "Insert into mytable (id, desc) VALUES (100, 'Description')";
        myCommand.ExecuteNonQuery();
        myCommand.CommandText = "Insert into mytable (id, desc) VALUES (101, 'Description')";
        myCommand.ExecuteNonQuery();
        myTrans.Commit();
        Console.WriteLine("Both records are written to database.");
        }
        catch(Exception e)
        {
        try
        {
        myTrans.Rollback();
        }
        catch (MySqlException ex)
        {
        if (myTrans.Connection != null)
        {
        Console.WriteLine("An exception of type " + ex.GetType() +
        " was encountered while attempting to roll back the transaction.");
        }
        }

        Console.WriteLine("An exception of type " + e.GetType() +
        " was encountered while inserting the data.");
        Console.WriteLine("Neither record was written to database.");
        }
        finally
        {
        myConnection.Close();
        }
        }
      </code>
    </example>
  </Commit>

</docs>