Test connection¶
This operation gives consumers a lightweight connectivity smoke test that exercises the real transaction lifecycle (not just "can I open a connection") against a mapped entity type.
| Field | Value |
|---|---|
| Operation ID | OP-NH-13 |
| Trigger | Consumer calls IUnitOfWork.TestConnection<TEntity>() |
| Inputs | TEntity — any mapped entity type |
| Outputs | No return value; throws if the connection, mapping, or transaction lifecycle fails |
| Primary components | UnitOfWork.TestConnection<TEntity> |
| Entry point | src/SAIF.NetCore.Nhibernate/SAIF.NetCore.NHibernate/SAIF.NetCore.NHibernate/UnitOfWork.cs |
Steps¶
- Call Begin transaction (
this.Begin()), which opens aReadCommittedtransaction (BR-NH-11)
Origin: The transaction-start logic reached here is implemented in Begin transaction. That operation's steps 1–3 (throw
InvalidOperationExceptionif a transaction is already active/committed/rolled back; otherwise callSession.Clear()— a no-op on a stateless session, a first-level-cache discard on a stateful session; then callSession.BeginTransaction(IsolationLevel.ReadCommitted)) run here, on this operation's behalf, before the query in step 2 executes. 2. Callthis.GetRepository<TEntity>().Query().Where(x => false).ToList()— this builds and executes a LINQ-to-NHibernate query with a constant-falsepredicate, translated to SQL with aWHEREclause that can never match any row (SQL-004). The query still requiresTEntityto be validly mapped and the connection/transaction to be functional. 3. Call Commit (this.Commit()), which requires the transaction opened in step 1 to still be active (BR-NH-13)Origin: The commit logic reached here is implemented in Commit. That operation's steps 1–4 (throw
InvalidOperationExceptionif no transaction is active; otherwise calltransaction.Commit(), issuing a databaseCOMMITand, for a stateful session, an auto-flush of pending changes subject to the session'sFlushMode; then callSession.Clear()— a no-op on a stateless session, a first-level-cache discard on a stateful session) run here, on this operation's behalf, to finish this operation's begin/query/commit cycle.
Process flow¶
The diagram below visualizes the begin/query/commit sequence this operation chains together.
flowchart TD
Start(["TestConnection<TEntity>() called"]) --> Begin["Begin()"]
Begin --> Query["GetRepository<TEntity>().Query()<br>.Where(x => false).ToList()"]
Query --> Commit["Commit()"]
Commit --> Return(["No exception = connection OK"])
Failure and edge-case behavior¶
- Any failure in
Begin, the query translation/execution, orCommitpropagates as an exception — there is no try/catch insideTestConnection; callers must handle failures themselves. - Because this runs the full
Begin/Commitcycle, calling it while the unit of work already has an active transaction throwsInvalidOperationExceptionfromBegin(BR-NH-12). After it completes successfully, the session's transaction has been committed and detached, so the same unit of work can begin another transaction (NHibernate 5.2.7). - The constant-
falsepredicate means zero rows are ever returned regardless of table contents; this call never touches real data, only connectivity/mapping/transaction plumbing.
Related documentation¶
- Operations and processes
- Business rules
- SQL catalog
- Begin transaction
- Commit
- Repository CRUD and query operations
Sources¶
src/SAIF.NetCore.Nhibernate/SAIF.NetCore.NHibernate/SAIF.NetCore.NHibernate/UnitOfWork.cs