DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DeveelDbTransaction.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Data;
19 using System.Data.Common;
20 
21 
22 namespace Deveel.Data.Client {
23  public sealed class DeveelDbTransaction : DbTransaction {
26  private bool finished;
27 
28  internal DeveelDbTransaction(DeveelDbConnection connection, IsolationLevel isolationLevel, int commitId) {
29  if (connection == null)
30  throw new ArgumentNullException("connection");
31 
32  this.connection = connection;
33  CommitId = commitId;
34  this.isolationLevel = isolationLevel;
35  connection.Transaction = this;
36  }
37 
38  private void AssertNotFinished() {
39  if (finished)
40  throw new InvalidOperationException("The transaction was already finished.");
41  }
42 
43 
44  public override void Commit() {
45  if (connection == null ||
46  (connection.State != ConnectionState.Open &&
47  connection.State != ConnectionState.Executing))
48  throw new InvalidOperationException("The underlying connection must be opened.");
49 
50  AssertNotFinished();
51 
52  try {
53  Connection.CommitTransaction(CommitId);
54  } finally {
55  Connection.Transaction = null;
56  finished = true;
57  }
58  }
59 
60  public override void Rollback() {
61  if (connection == null ||
62  (connection.State != ConnectionState.Open &&
63  connection.State != ConnectionState.Executing))
64  throw new InvalidOperationException("The underlying connection must be opened.");
65 
66  AssertNotFinished();
67 
68  try {
69  Connection.RollbackTransaction(CommitId);
70  } finally {
71  Connection.Transaction = null;
72  finished = true;
73  }
74  }
75 
76  public new DeveelDbConnection Connection {
77  get { return connection; }
78  }
79 
80  protected override DbConnection DbConnection {
81  get { return Connection; }
82  }
83 
84  public override IsolationLevel IsolationLevel {
85  get { return isolationLevel; }
86  }
87 
88  internal int CommitId { get; private set; }
89 
90  protected override void Dispose(bool disposing) {
91  if (disposing) {
92  if (!finished)
93  Rollback();
94  }
95 
96  connection = null;
97 
98  base.Dispose(disposing);
99  }
100  }
101 }
override void Dispose(bool disposing)
override void Dispose(bool disposing)
DeveelDbTransaction(DeveelDbConnection connection, IsolationLevel isolationLevel, int commitId)