55
66namespace Ydb . Sdk . Ado ;
77
8+ /// <summary>
9+ /// Represents a YDB transaction. This class cannot be inherited.
10+ /// </summary>
11+ /// <remarks>
12+ /// YdbTransaction represents a database transaction in YDB. It provides methods to commit or rollback
13+ /// changes made within the transaction. The transaction mode determines the isolation level and
14+ /// consistency guarantees.
15+ ///
16+ /// For more information about YDB transactions, see:
17+ /// <see href="https://ydb.tech/docs/en/concepts/transactions">YDB Transactions Documentation</see>.
18+ /// </remarks>
819public sealed class YdbTransaction : DbTransaction
920{
1021 private readonly TransactionMode _transactionMode ;
@@ -13,9 +24,32 @@ public sealed class YdbTransaction : DbTransaction
1324 private YdbConnection ? _ydbConnection ;
1425 private bool _isDisposed ;
1526
27+ /// <summary>
28+ /// Gets or sets the transaction identifier.
29+ /// </summary>
30+ /// <remarks>
31+ /// The transaction ID is assigned by YDB when the transaction is started.
32+ /// This property is used internally for transaction management.
33+ /// </remarks>
1634 internal string ? TxId { get ; set ; }
35+
36+ /// <summary>
37+ /// Gets a value indicating whether the transaction has been completed.
38+ /// </summary>
39+ /// <remarks>
40+ /// A transaction is considered completed when it has been committed, rolled back, or failed.
41+ /// Once completed, the transaction cannot be used for further operations.
42+ /// </remarks>
1743 internal bool Completed { get ; private set ; }
1844
45+ /// <summary>
46+ /// Gets or sets a value indicating whether the transaction has failed.
47+ /// </summary>
48+ /// <remarks>
49+ /// When true, indicates that the transaction has been rolled back by the server.
50+ /// A failed transaction cannot be committed. The <see cref="Rollback"/> method
51+ /// can be called to mark the transaction as completed.
52+ /// </remarks>
1953 internal bool Failed
2054 {
2155 private get => _failed ;
@@ -26,6 +60,13 @@ internal bool Failed
2660 }
2761 }
2862
63+ /// <summary>
64+ /// Gets the transaction control for YDB operations.
65+ /// </summary>
66+ /// <remarks>
67+ /// Returns null if the transaction is completed, otherwise returns the appropriate
68+ /// transaction control based on whether the transaction has been started.
69+ /// </remarks>
2970 internal TransactionControl ? TransactionControl => Completed
3071 ? null
3172 : TxId == null
@@ -38,13 +79,65 @@ internal YdbTransaction(YdbConnection ydbConnection, TransactionMode transaction
3879 _transactionMode = transactionMode ;
3980 }
4081
82+ /// <summary>
83+ /// Commits the database transaction.
84+ /// </summary>
85+ /// <exception cref="InvalidOperationException">
86+ /// Thrown when the transaction has already been completed or the connection is closed.
87+ /// </exception>
88+ /// <exception cref="YdbOperationInProgressException">
89+ /// Thrown when another operation is in progress on the connection.
90+ /// </exception>
91+ /// <exception cref="YdbException">
92+ /// Thrown when the commit operation fails.
93+ /// </exception>
4194 public override void Commit ( ) => CommitAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
4295
96+ /// <summary>
97+ /// Asynchronously commits the database transaction.
98+ /// </summary>
99+ /// <param name="cancellationToken">A token to cancel the operation.</param>
100+ /// <returns>A task representing the asynchronous operation.</returns>
101+ /// <exception cref="InvalidOperationException">
102+ /// Thrown when the transaction has already been completed or the connection is closed.
103+ /// </exception>
104+ /// <exception cref="YdbOperationInProgressException">
105+ /// Thrown when another operation is in progress on the connection.
106+ /// </exception>
107+ /// <exception cref="YdbException">
108+ /// Thrown when the commit operation fails.
109+ /// </exception>
43110 public override async Task CommitAsync ( CancellationToken cancellationToken = new ( ) ) =>
44111 await FinishTransaction ( txId => DbConnection ! . Session . CommitTransaction ( txId , cancellationToken ) ) ;
45112
113+ /// <summary>
114+ /// Rolls back the database transaction.
115+ /// </summary>
116+ /// <exception cref="InvalidOperationException">
117+ /// Thrown when the transaction has already been completed or the connection is closed.
118+ /// </exception>
119+ /// <exception cref="YdbOperationInProgressException">
120+ /// Thrown when another operation is in progress on the connection.
121+ /// </exception>
122+ /// <exception cref="YdbException">
123+ /// Thrown when the rollback operation fails.
124+ /// </exception>
46125 public override void Rollback ( ) => RollbackAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
47126
127+ /// <summary>
128+ /// Asynchronously rolls back the database transaction.
129+ /// </summary>
130+ /// <param name="cancellationToken">A token to cancel the operation.</param>
131+ /// <returns>A task representing the asynchronous operation.</returns>
132+ /// <exception cref="InvalidOperationException">
133+ /// Thrown when the transaction has already been completed or the connection is closed.
134+ /// </exception>
135+ /// <exception cref="YdbOperationInProgressException">
136+ /// Thrown when another operation is in progress on the connection.
137+ /// </exception>
138+ /// <exception cref="YdbException">
139+ /// Thrown when the rollback operation fails.
140+ /// </exception>
48141 public override async Task RollbackAsync ( CancellationToken cancellationToken = new ( ) )
49142 {
50143 if ( Failed )
@@ -57,6 +150,13 @@ internal YdbTransaction(YdbConnection ydbConnection, TransactionMode transaction
57150 await FinishTransaction ( txId => DbConnection ! . Session . RollbackTransaction ( txId , cancellationToken ) ) ;
58151 }
59152
153+ /// <summary>
154+ /// Gets the database connection associated with this transaction.
155+ /// </summary>
156+ /// <returns>The YdbConnection associated with this transaction, or null if disposed.</returns>
157+ /// <exception cref="ObjectDisposedException">
158+ /// Thrown when the transaction has been disposed.
159+ /// </exception>
60160 protected override YdbConnection ? DbConnection
61161 {
62162 get
@@ -66,6 +166,13 @@ protected override YdbConnection? DbConnection
66166 }
67167 }
68168
169+ /// <summary>
170+ /// Gets the isolation level of this transaction.
171+ /// </summary>
172+ /// <remarks>
173+ /// Returns Serializable for SerializableRw transaction mode, otherwise Unspecified.
174+ /// The actual isolation level depends on the transaction mode used when creating the transaction.
175+ /// </remarks>
69176 public override IsolationLevel IsolationLevel => _transactionMode == TransactionMode . SerializableRw
70177 ? IsolationLevel . Serializable
71178 : IsolationLevel . Unspecified ;
@@ -107,6 +214,13 @@ private async Task FinishTransaction(Func<string, Task> finishMethod)
107214 }
108215 }
109216
217+ /// <summary>
218+ /// Releases the unmanaged resources used by the YdbTransaction and optionally releases the managed resources.
219+ /// </summary>
220+ /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
221+ /// <remarks>
222+ /// If the transaction is not completed, it will be rolled back before disposal.
223+ /// </remarks>
110224 protected override void Dispose ( bool disposing )
111225 {
112226 if ( _isDisposed || ! disposing )
@@ -120,6 +234,13 @@ protected override void Dispose(bool disposing)
120234 _isDisposed = true ;
121235 }
122236
237+ /// <summary>
238+ /// Asynchronously releases the unmanaged resources used by the YdbTransaction.
239+ /// </summary>
240+ /// <returns>A ValueTask representing the asynchronous disposal operation.</returns>
241+ /// <remarks>
242+ /// If the transaction is not completed, it will be rolled back before disposal.
243+ /// </remarks>
123244 public override async ValueTask DisposeAsync ( )
124245 {
125246 if ( _isDisposed )
0 commit comments