I am trying to connect with IBM MQ using Python's 'pymqi' library.
I want to use the transaction capability in the IBM MQ but it showing me MQI Error. Comp: 2, Reason 2012: FAILED: MQRC_ENVIRONMENT_ERROR.
The code is working as regular but it error happened after adding qmgr.begin() statement.
Required assistance.
import logging
from datetime import datetime
import pymqi
logging.basicConfig(level=logging.INFO)
queue_manager = 'QM1'
channel = b'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'DEV.QUEUE.1'
conn_info = f'{host}({port})'
conn_info = conn_info.encode('utf-8')
user = 'app'
password = 'xxxxxx'
# Setting up CD() parameters
cd = pymqi.CD()
cd.ChannelName = channel
cd.ConnectionName = conn_info
cd.ChannelType = pymqi.CMQC.MQCHT_CLNTCONN
cd.TransportType = pymqi.CMQC.MQXPT_TCP
# Message to be sent
message = f'Message sent to {queue_name} at: {datetime.now()}'
kwargs = {
'user': user,
'password': password,
'cd': cd
}
try:
# Connect to the queue manager
qmgr = pymqi.QueueManager(None)
qmgr.connect_with_options(queue_manager, **kwargs)
# Start a transaction
qmgr.begin()
try:
# Open the queue within the transaction
queue = pymqi.Queue(qmgr, queue_name)
# Put a message to the queue within the transaction
queue.put(message)
logging.info(message)
# Close the queue within the transaction
queue.close()
# Commit the transaction
qmgr.commit()
except pymqi.MQMIError as e:
# Rollback the transaction in case of an error
logging.error(f"MQI error: {e}")
qmgr.backout()
raise
finally:
# End the transaction
qmgr.disconnect()
except pymqi.MQMIError as e:
logging.error(f"MQI error: {e}")
qmgr.begin() will use MQBEGIN which is not allowed on a client connection.
Local transactions in MQ are automatically started if you do a put or get with the syncpoint option set. This is suitable if want a transaction that only includes updates involving a single MQ queue manager. You can specify the syncpoint option on put as follows:
Global transactions coordinated by MQ are only allowed for bindings connection applications, not for clients.
Global transactions including MQ using client connection need to use an XA-compliant external transaction coordinator and configure the MQ extended transactional client. This is documented here: https://www.ibm.com/docs/en/ibm-mq/9.3?topic=server-configuring-extended-transactional-client.
The application will need to use the transaction manager coordination calls rather than MQ calls for operations like commit or backout.