66 lines
1.7 KiB
Markdown
66 lines
1.7 KiB
Markdown
# Transactions and Disposal
|
|
|
|
DynamORM manages connections, command pools, and transaction stacks internally.
|
|
|
|
## Connection and Transaction Options
|
|
|
|
`DynamicDatabaseOptions` controls behavior:
|
|
|
|
- `SingleConnection`
|
|
- `SingleTransaction`
|
|
- `SupportSchema`
|
|
- `SupportStoredProcedures`
|
|
- `SupportNoLock`
|
|
- `SupportTop` / `SupportLimitOffset` / `SupportFirstSkip`
|
|
|
|
Typical setup:
|
|
|
|
```csharp
|
|
var options =
|
|
DynamicDatabaseOptions.SingleConnection |
|
|
DynamicDatabaseOptions.SingleTransaction |
|
|
DynamicDatabaseOptions.SupportLimitOffset |
|
|
DynamicDatabaseOptions.SupportSchema;
|
|
```
|
|
|
|
## Transaction Usage
|
|
|
|
```csharp
|
|
using (var db = new DynamicDatabase(factory, connectionString, options))
|
|
using (var conn = db.Open())
|
|
using (var tx = conn.BeginTransaction())
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.SetCommand("UPDATE users SET first = 'Ada' WHERE id = 1").ExecuteNonQuery();
|
|
tx.Commit();
|
|
}
|
|
```
|
|
|
|
Global transaction mode is also available via `db.BeginTransaction()`.
|
|
|
|
## Disposal Guarantees
|
|
|
|
Current disposal behavior includes idempotent guards on key objects:
|
|
|
|
- `DynamicDatabase`
|
|
- `DynamicConnection`
|
|
- `DynamicCommand`
|
|
- `DynamicTransaction`
|
|
- `DynamicTable`
|
|
- builder and parser helpers
|
|
|
|
This prevents repeated cleanup from throwing or re-disposing lower-level resources.
|
|
|
|
## Pooling and Rollback Behavior
|
|
|
|
Behavior validated by `DynamORM.Tests/Helpers/PoolingTests.cs`:
|
|
|
|
- Disposing the database invalidates active commands.
|
|
- Open transactions are rolled back during disposal when not committed.
|
|
|
|
## Practices
|
|
|
|
- Prefer `using` blocks for `DynamicDatabase`, connections, commands, transactions, and builders.
|
|
- Do not manually re-dispose the same object from multiple ownership paths unless `IsDisposed` is checked.
|
|
- Keep transaction scope short and explicit.
|