MQCLOSE ends your application's session with a specific queue or topic object. MQOPEN gave you Hobj; MQPUT and MQGET used it; MQCLOSE tells the queue manager you are done with that mailbox slot. Until close completes, the queue manager tracks your open intent (put, get, browse) and may hold locks or reservation state associated with the handle. Production services that open dozens of queues per request must close each handle in finally blocks or use language patterns that auto-close—leaks accumulate across traffic and show up as DISPLAY QSTATUS IPPROCS counts that never drop. Close options go beyond simple release: MQCO_DELETE and MQCO_DELETE_PURGE govern permanent dynamic queue cleanup. This tutorial explains standard close, delete options, temporary versus permanent dynamic behavior, close order relative to MQCMIT, multi-handle programs, reason codes, and operational symptoms of applications that never close.
After processing, call MQCLOSE with Hconn, address of Hobj, and CloseOptions MQCO_NONE for a normal queue that remains defined for other applications. Hobj is undefined for further use—do not MQPUT after close without a new MQOPEN. The queue object itself persists on the queue manager unless you used delete options on a dynamic queue type that allows removal.
123456789101112MQLONG closeOptions = MQCO_NONE; MQCLOSE(Hconn, &Hobj, closeOptions, &compCode, &reason); if (compCode != MQCC_OK) { /* 2019 handle error if already closed */ } /* Hobj must not be reused without MQOPEN */
| Option | Effect | Typical use |
|---|---|---|
| MQCO_NONE | Release handle; queue remains | Normal QLOCAL consumers and producers |
| MQCO_DELETE | Delete empty permanent dynamic queue | PERMDYN cleanup when CURDEPTH 0 |
| MQCO_DELETE_PURGE | Delete queue and discard messages | Forced teardown of dynamic queue |
| MQCO_KEEP_SUB | Topic publish: keep subscription | Advanced pub/sub scenarios per IBM docs |
Queues created from a model with DEFTYPE(TEMPDYN) exist for the creating application's conversation. IBM MQ treats MQCLOSE on temporary dynamics such that the queue is removed when the last handle closes—MQCO_DELETE and MQCO_DELETE_PURGE are equivalent to MQCO_NONE for temps. Beginners sometimes try explicit DELETE QLOCAL in runmqsc on a temp name that already vanished. Reply queues in request/reply must stay open on the client until the reply is MQGET, then MQCLOSE removes the temp name automatically.
PERMDYN queues survive MQCLOSE with MQCO_NONE—they behave like ordinary queues until MQCO_DELETE succeeds (usually empty queue) or an operator DELETE QLOCAL. Forgotten PERMDYN queues clutter repositories and backups. Application design should document who calls MQCO_DELETE_PURGE on error paths versus leaving messages for support to inspect.
MQCLOSE is handing the mailbox key back to the post office clerk. You cannot put more letters in that slot until you check out a new key (MQOPEN again). For temporary mailboxes, the clerk removes the whole box when you return the key.
Closing the connection before objects may force implicit close but logs warnings and risks rollback surprises. Frameworks like JMS session.close() should run before connection.close().
If MQPUT under MQPMO_SYNCPOINT was not committed, MQCLOSE behavior interacts with the unit of work—consult IBM documentation for your language binding. Best practice: explicit MQCMIT or MQBACK before MQCLOSE on transactional queues. Poison message handlers that MQBACK in a loop should still close browse handles opened for inspection.
Programs with HobjIn and HobjOut for request/reply close each independently. Close order rarely matters for independent queues unless sharing a single UOW across both. Re-opening the same queue name gets a new Hobj value—do not assume numeric handle equality across open cycles.
DISPLAY QSTATUS shows open input and output process counts. Stale IPPROCS after application crash usually clear when the queue manager detects disconnect. Persistent high IPPROCS with idle apps indicates handle leak—find the APPNAME in DISPLAY CONN and restart or fix code. ALTER QLOCAL INHIBIT may be needed during incident to stop new opens while draining.
MQCLOSE is giving back the key to your classroom mailbox so someone else can use it, and for temporary boxes the teacher throws the whole box away.
Write shutdown pseudocode closing two handles then disconnecting.
When would you choose MQCO_DELETE_PURGE over MQCO_DELETE?
Explain why temp dynamic reply queues must not be closed before MQGET of the reply.
1. MQCLOSE requires:
2. MQCO_NONE means:
3. Temp dynamic queues delete on close:
4. 2019 means: