forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITransaction.cs
114 lines (103 loc) · 4.06 KB
/
ITransaction.cs
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
using System;
using System.Data;
using System.Data.Common;
using NHibernate.Transaction;
namespace NHibernate
{
/// <summary>
/// Allows the application to define units of work, while maintaining abstraction from the
/// underlying transaction implementation
/// </summary>
/// <remarks>
/// A transaction is associated with a <c>ISession</c> and is usually instantiated by a call to
/// <c>ISession.BeginTransaction()</c>. A single session might span multiple transactions since
/// the notion of a session (a conversation between the application and the datastore) is of
/// coarser granularity than the notion of a transaction. However, it is intended that there be
/// at most one uncommitted <c>ITransaction</c> associated with a particular <c>ISession</c>
/// at a time. Implementors are not intended to be threadsafe.
/// </remarks>
public partial interface ITransaction : IDisposable
{
/// <summary>
/// Begin the transaction with the default isolation level.
/// </summary>
void Begin();
/// <summary>
/// Begin the transaction with the specified isolation level.
/// </summary>
/// <param name="isolationLevel">Isolation level of the transaction</param>
void Begin(IsolationLevel isolationLevel);
/// <summary>
/// Flush the associated <c>ISession</c> and end the unit of work.
/// </summary>
/// <remarks>
/// This method will commit the underlying transaction if and only if the transaction
/// was initiated by this object.
/// </remarks>
void Commit();
/// <summary>
/// Force the underlying transaction to roll back.
/// </summary>
void Rollback();
/// <summary>
/// Is the transaction in progress
/// </summary>
bool IsActive { get; }
/// <summary>
/// Was the transaction rolled back or set to rollback only?
/// </summary>
bool WasRolledBack { get; }
/// <summary>
/// Was the transaction successfully committed?
/// </summary>
/// <remarks>
/// This method could return <see langword="false" /> even after successful invocation of <c>Commit()</c>
/// </remarks>
bool WasCommitted { get; }
/// <summary>
/// Enlist the <see cref="DbCommand"/> in the current Transaction.
/// </summary>
/// <param name="command">The <see cref="DbCommand"/> to enlist.</param>
/// <remarks>
/// It is okay for this to be a no op implementation.
/// </remarks>
void Enlist(DbCommand command);
// Obsolete since 5.2
/// <summary>
/// Register a user synchronization callback for this transaction.
/// </summary>
/// <param name="synchronization">The <see cref="ISynchronization"/> callback to register.</param>
[Obsolete("Use RegisterSynchronization(ITransactionCompletionSynchronization) extension method instead. " +
"If implementing ITransaction, implement a 'public void " +
"RegisterSynchronization(ITransactionCompletionSynchronization)': the TransactionExtensions extension " +
"method will call it.")]
void RegisterSynchronization(ISynchronization synchronization);
}
// 6.0 TODO: merge into ITransaction
public static class TransactionExtensions
{
/// <summary>
/// Register an user synchronization callback for this transaction.
/// </summary>
/// <param name="transaction">The transaction.</param>
/// <param name="synchronization">The <see cref="ISynchronization"/> callback to register.</param>
public static void RegisterSynchronization(
this ITransaction transaction,
ITransactionCompletionSynchronization synchronization)
{
if (transaction is AdoTransaction adoTransaction)
{
adoTransaction.RegisterSynchronization(synchronization);
return;
}
// Use reflection for supporting custom transaction factories and transaction implementations.
var registerMethod = transaction.GetType().GetMethod(
nameof(AdoTransaction.RegisterSynchronization),
new[] { typeof(ITransactionCompletionSynchronization) });
if (registerMethod == null)
throw new NotSupportedException(
$"{transaction.GetType()} does not support {nameof(ITransactionCompletionSynchronization)}");
registerMethod.Invoke(transaction, new object[] { synchronization });
}
}
}