5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
31 using System.Data.Linq;
\r
34 using DbLinq.Data.Linq.Sql;
\r
36 namespace DbLinq.Data.Linq.Database.Implementation
\r
39 /// Transactional command
\r
44 class TransactionalCommand : ITransactionalCommand
\r
46 private readonly IDisposable _connection;
\r
48 /// Ambient transaction
\r
50 private readonly IDatabaseTransaction _transaction;
\r
52 private readonly IDbCommand _command;
\r
54 private readonly bool haveHigherTransaction;
\r
57 /// Gets the command.
\r
59 /// <value>The command.</value>
\r
60 public IDbCommand Command
\r
70 /// Commits current transaction.
\r
72 public virtual void Dispose()
\r
75 if (_transaction != null)
\r
76 _transaction.Dispose();
\r
77 _connection.Dispose();
\r
81 /// Commits the current transaction.
\r
82 /// throws NRE if _transaction is null. Behavior is intentional.
\r
84 public void Commit()
\r
86 // TODO: do not commit if participating in a higher transaction
\r
87 if (!haveHigherTransaction)
\r
88 _transaction.Commit();
\r
91 public TransactionalCommand(string commandText, bool createTransaction, DataContext dataContext)
\r
93 // TODO: check if all this stuff is necessary
\r
94 // the OpenConnection() checks that the connection is already open
\r
95 // TODO: see if we can move this here (in theory the final DataContext shouldn't use)
\r
96 _connection = dataContext.DatabaseContext.OpenConnection();
\r
98 _command = dataContext.DatabaseContext.CreateCommand();
\r
99 haveHigherTransaction = dataContext.Transaction != null;
\r
100 // the transaction is optional
\r
101 if (createTransaction && !haveHigherTransaction)
\r
103 _transaction = dataContext.DatabaseContext.Transaction();
\r
104 _command.Transaction = _transaction.Transaction;
\r
107 _command.Transaction = dataContext.Transaction;
\r
108 Command.CommandText = commandText;
\r