# Business Logic — JBSbatKKCourseChgFixTgCst.terminal() [12 LOC]

| Field | Value |
|-------|-------|
| Fully Qualified Name | `eo.business.service.JBSbatKKCourseChgFixTgCst` |
| Layer | Batch (Package: `eo.business.service`) |
| Module | `service` (Package: `eo.business.service`) |

## 1. Role

### JBSbatKKCourseChgFixTgCst.terminal()

The `terminal()` method is the **business service teardown routine** for the Course Change Fix Target Extraction batch job. In the K-Opticom customer backbone system, this batch extracts service contracts that are eligible for course change confirmation fixes — a process used to correct inconsistencies arising from service contract detail modifications, plan changes, exclusion control, canceled contracts, and abnormal reservations. This method's sole responsibility is **resource cleanup**: it closes all database access objects (`JBSbatSQLAccess` instances) that were opened during the `initial()` phase, ensuring no database connections or cursors are left open after the batch processing completes. It follows the **try-finally teardown pattern** commonly used in batch services extending `JBSbatBusinessService` — the framework calls `terminal()` after `execute()` (or in an `finally` block) regardless of whether the main processing succeeded or threw an exception. As a **designated destructor** (akin to a `finalize` or `dispose` hook), it implements the resource management responsibility of the batch service, preventing connection pool exhaustion in the cron-scheduled batch execution environment.

## 2. Processing Pattern (Detailed Business Logic)

```mermaid
flowchart TD
    START(["terminal()"])
    CLOSE1["db_KK_T_IDO_RSV.close()"]
    CLOSE2["db_KK_T_SVKEI_EXC_CTRL.close()"]
    CLOSE3["db_KK_T_SVC_KEI_UCWK.close()"]
    CLOSE4["db_KK_T_IDO_RSV_063.close()"]
    END_NODE(["Return void"])

    START --> CLOSE1 --> CLOSE2 --> CLOSE3 --> CLOSE4 --> END_NODE
```

This method performs sequential resource cleanup with no conditional branches. All four `JBSbatSQLAccess.close()` calls execute in fixed order, releasing database cursor handles and connection resources associated with the tables:

- **`KK_T_IDO_RSV`** — Abnormal Reservation table (used for queries with SQL key `KK_SELECT_007`, `KK_SELECT_030`, `KK_SELECT_063`)
- **`KK_T_SVKEI_EXC_CTRL`** — Service Contract Exclusion Control table
- **`KK_T_SVC_KEI_UCWK`** — Service Contract Details table

Each `close()` call is idempotent — the underlying `JBSbatSQLAccess.close()` method safely handles a `null` receiver (the field is initialized to `null` in the class declaration).

## 3. Parameter Analysis

| No | Parameter Name | Type | Business Description |
|----|---------------|------|---------------------|
| - | (none) | - | This method takes no parameters. It operates entirely on instance fields initialized during the `initial()` phase. |

**Instance fields read by this method:**

| Field | Type | Business Description |
|-------|------|---------------------|
| `db_KK_T_IDO_RSV` | `JBSbatSQLAccess` | Database cursor handle for the Abnormal Reservation table (`KK_T_IDO_RSV`) — holds query result sets from SQL definition keys `KK_SELECT_007` and `KK_SELECT_030` |
| `db_KK_T_SVKEI_EXC_CTRL` | `JBSbatSQLAccess` | Database cursor handle for the Service Contract Exclusion Control table (`KK_T_SVKEI_EXC_CTRL`) |
| `db_KK_T_SVC_KEI_UCWK` | `JBSbatSQLAccess` | Database cursor handle for the Service Contract Details table (`KK_T_SVC_KEI_UCWK`) — holds query result sets from SQL definition key `KK_SELECT_044` |
| `db_KK_T_IDO_RSV_063` | `JBSbatSQLAccess` | Secondary database cursor handle for the Abnormal Reservation table (`KK_T_IDO_RSV`) — holds query result sets from SQL definition key `KK_SELECT_063` |

## 4. CRUD Operations / Called Services

### Pre-computed evidence from code analysis graph:

| CRUD | SC / CBS | SC Code | Entity / DB | Operation Description |
|------|----------|---------|-------------|----------------------|
| - | `db_KK_T_IDO_RSV.close` | - | - | Calls `close` on DB access for `KK_T_IDO_RSV` (Abnormal Reservation) |
| - | `db_KK_T_SVKEI_EXC_CTRL.close` | - | - | Calls `close` on DB access for `KK_T_SVKEI_EXC_CTRL` (Service Contract Exclusion Control) |
| - | `db_KK_T_SVC_KEI_UCWK.close` | - | - | Calls `close` on DB access for `KK_T_SVC_KEI_UCWK` (Service Contract Details) |
| - | `db_KK_T_IDO_RSV_063.close` | - | - | Calls `close` on DB access for `KK_T_IDO_RSV_063` (Abnormal Reservation, SQL 063) |

This method performs no data manipulation (no Create/Read/Update/Delete operations). All four calls are **resource deallocation** operations — they release database cursors and connection resources acquired during the `initial()` setup phase. The associated tables were queried in prior processing phases (likely within the `execute()` method of the parent batch service), and these `close()` calls finalize those database interactions.

## 5. Dependency Trace

| # | Caller (Screen/Batch) | Call Chain (Full Path to this Method) | Terminal (SC / CRUD / Entity) |
|---|----------------------|--------------------------------------|-------------------------------|
| 1 | Batch: JBSbatKKCourseChgFixTgCst | `JBSbatBusinessService.execute()` → `JBSbatKKCourseChgFixTgCst.terminal()` (called by framework in `finally` block) | `db_KK_T_IDO_RSV.close`, `db_KK_T_SVKEI_EXC_CTRL.close`, `db_KK_T_SVC_KEI_UCWK.close`, `db_KK_T_IDO_RSV_063.close` |

**Note:** No direct caller was found searching for `.terminal(` invocations in the codebase. This method is an **overridden lifecycle hook** called by the `JBSbatBusinessService` parent framework during batch shutdown. The batch framework (likely `JBSbatBatch` or similar orchestrator) invokes the service's `initial()`, `execute()`, and `terminal()` methods in sequence, with `terminal()` guaranteed to run in a `finally` block for cleanup regardless of processing outcome.

## 6. Per-Branch Detail Blocks

Since this method has no conditional branches, loops, or multi-path control flow, there is a single linear block.

**Block 1** — [LINEAR SEQUENCE] `(no condition)` (L708)

> Sequential database resource cleanup. All four DB access objects are closed in a fixed order. This corresponds to the comment: "ツールから生成した終了処理のソースです" ("Source code for termination processing generated from the tool").

| # | Type | Code |
|---|------|------|
| 1 | EXEC | `db_KK_T_IDO_RSV.close();` // Close DB access cursor for Abnormal Reservation table (KK_T_IDO_RSV) — SQL keys: KK_SELECT_007, KK_SELECT_030 |
| 2 | EXEC | `db_KK_T_SVKEI_EXC_CTRL.close();` // Close DB access cursor for Service Contract Exclusion Control table (KK_T_SVKEI_EXC_CTRL) |
| 3 | EXEC | `db_KK_T_SVC_KEI_UCWK.close();` // Close DB access cursor for Service Contract Details table (KK_T_SVC_KEI_UCWK) — SQL key: KK_SELECT_044 |
| 4 | EXEC | `db_KK_T_IDO_RSV_063.close();` // Close DB access cursor for Abnormal Reservation table (KK_T_IDO_RSV) — SQL key: KK_SELECT_063 |

## 7. Glossary

| Term | Type | Business Meaning |
|------|------|------------------|
| `KK_T_IDO_RSV` | Table | Abnormal Reservation table — tracks reservation records with abnormal states (e.g., reservations that failed or are in an inconsistent state during course changes) |
| `KK_T_SVKEI_EXC_CTRL` | Table | Service Contract Exclusion Control table — manages which service contracts should be excluded from certain batch processing (e.g., contracts excluded from plan change operations) |
| `KK_T_SVC_KEI_UCWK` | Table | Service Contract Details table — stores detailed line-item information for each service contract, including charges and service specifications |
| `KK_SELECT_007` | SQL Key | SQL definition key used to query the `KK_T_IDO_RSV` table for abnormal reservation records |
| `KK_SELECT_030` | SQL Key | SQL definition key used to query the `KK_T_IDO_RSV` table for abnormal reservation records (variant query) |
| `KK_SELECT_044` | SQL Key | SQL definition key used to query the `KK_T_SVC_KEI_UCWK` table for service contract details |
| `KK_SELECT_063` | SQL Key | SQL definition key used to query the `KK_T_IDO_RSV` table via the secondary handle `db_KK_T_IDO_RSV_063` |
| CRS_CHG_FIX | Domain | Course Change Fix — batch process for correcting inconsistencies in course change confirmations, triggered by service contract detail changes, plan modifications, or contract cancellations |
| JBSbatBusinessService | Class | Base class for batch business services — provides the `initial()`, `execute()`, and `terminal()` lifecycle hooks that this class overrides |
| JBSbatSQLAccess | Class | Database cursor/connection abstraction — wraps SQL query execution and result set management for a specific table; `close()` releases all associated resources |
| SVC_KEI | Field | Service Contract — the master service agreement between K-Opticom and a customer, encompassing all service line items and charges |
| IDO | Field | Abnormal/Migration — refers to reservation records with abnormal states requiring special handling during course changes |
| EXC_CTRL | Field | Exclusion Control — management of service contracts that should be excluded from certain batch operations |
| UWK | Field | Work (子ワーク) — Japanese "work" or "child work"; denotes child-level detail records of a service contract |
| ショウコウ | Field | Effect/Impact — Japanese term referring to the impact analysis of course changes on existing service contracts |
| コース変更 | Field | Course Change — modification of a customer's service configuration (e.g., changing plan type, adding/removing services) |
| 確認 | Field | Confirmation — the confirmation step in the course change process where changes are validated before being applied |
| 対称 | Field | Target — refers to records eligible for processing (e.g., "extract targets") |
| 異動予約 | Field | Abnormal Reservation — reservations that have entered an abnormal state and require special handling |
| バッチ | Field | Batch — scheduled background processing (as opposed to interactive screen processing) |
| Shift-JIS | Encoding | Japanese character encoding — the file encoding used for output CSV files in this batch (as defined by `ENCODE` constant) |
