Shared (thread independent) connections with MQCONNX
Use this information to learn about Shared connections with MQCONNX, and some usage notes to consider.
MQCNO_HANDLE_SHARE_NONE
- Creates a non-shared connection.
In most other environments, you can use one of the following thread independent, shared connection options:
MQCNO_HANDLE_SHARE_BLOCK
- Creates a shared connection. On a
MQCNO_HANDLE_SHARE_BLOCK
connection, if the connection is currently in use by an MQI call on another thread, the MQI call waits until the current MQI call has completed. MQCNO_HANDLE_SHARE_NO_BLOCK
- Creates a shared connection. On a
MQCNO_HANDLE_SHARE_NO_BLOCK
connection, if the connection is currently in use by an MQI call on another thread, the MQI call fails immediately with a reason ofMQRC_CALL_IN_PROGRESS
.
Except for the MTS (Microsoft Transaction
Server) environment, the default value is MQCNO_HANDLE_SHARE_NONE
.
In the MTS environment, the default value is MQCNO_HANDLE_SHARE_BLOCK
.
A connection handle is returned from the MQCONNX call. The handle can be used by subsequent MQI calls from any thread in the process, associating those calls with the handle returned from the MQCONNX. MQI calls using a single shared handle are serialized across threads.
- Thread 1 issues MQCONNX and gets a shared handle h1
- Thread 1 opens a queue and issues a get request using h1
- Thread 2 issues a put request using h1
- Thread 3 issues a put request using h1
- Thread 2 issues MQDISC using h1
While the handle is in use by any thread, access to the connection
is unavailable to other threads. In circumstances where it is acceptable
that a thread waits for any previous call from another thread to complete,
use MQCONNX with the option MQCNO_HANDLE_SHARE_BLOCK
.
However blocking can cause difficulties. Suppose that in step 2, thread 1 issues a get request
that waits for messages that might not have yet arrived (a get with
wait). In this case, threads 2 and 3 are also left waiting (blocked)
for as long as the get request on thread 1 takes. If you prefer that
an MQI call returns with an error if another MQI call is already running
on the handle, use MQCONNX with the option MQCNO_HANDLE_SHARE_NO_BLOCK
.
Shared connection usage notes
- Any object handles (Hobj) created by opening an object are associated with an Hconn; so for a shared Hconn, the Hobjs are also shared and usable by any thread using the Hconn. Similarly, any unit of work started under an Hconn is associated with that Hconn; so this too is shared across threads with the shared Hconn.
- Any thread can call MQDISC to disconnect a shared Hconn, not just the thread that called the corresponding MQCONNX. The MQDISC terminates the Hconn making it unavailable to all threads.
- A single thread can use multiple shared Hconns serially, for example use MQPUT to put one message under one shared Hconn then put another message using another shared Hconn, with each operation being under a different local unit of work.
- Shared Hconns cannot be used within a global unit of work.