Search code examples
pythonibm-mqpymqi

How to Put the message using UOM (begin, commit, backout). getting error: 2012 MQRC_ENVIRONMENT_ERROR


import pymqi

queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
conn_info = f'{host}({port})'

# Connect to the queue manager
qmgr = pymqi.connect(queue_manager, channel, conn_info)
queue = pymqi.Queue(qmgr, queue_name, pymqi.CMQC.MQOO_OUTPUT)
try:
    for rec in range(1,21):
        message = f"hello python {rec}"
        queue.put(message)
except Exception as err:
    print(f"(err)")
queue.close()
qmgr.disconnect()

When I did the PUT operation using the above code it worked as expected.

When I am trying to do the PUT operation using the below code. It is giving me an error saying FAILED 2012: MQRC_ENVIRONMENT_ERROR.

import pymqi

queue_manager = 'QM1'
channel = 'DEV.APP.SVRCONN'
host = '127.0.0.1'
port = '1414'
queue_name = 'TEST.1'
conn_info = f'{host}({port})'

# Connect to the queue manager
qmgr = pymqi.connect(queue_manager, channel, conn_info)
queue = pymqi.Queue(qmgr, queue_name, pymqi.CMQC.MQOO_OUTPUT)
pmo = pymqi.PMO()
pmo.Options = pymqi.CMQC.MQPMO_SYNCPOINT | pymqi.CMQC.MQPMO_FAIL_IF_QUIESCING

transaction = False
try:
    # transaction start
    qmgr.begin()
    # set the flag true after qmgr begin
    transaction = True  
    for rec in range(1,21):
        mqmd = pymqi.MD()
        mqmd.Version = pymqi.CMQC.MQMD_VERSION_2
        if rec == 9:
            raise Exception("Some Error occur")
        message = f"hello python {rec}"
        queue.put(message, mqmd, pmo)
    # commit the transaction if all message were successfully processed
    qmgr.commit()
except Exception as err:
    if transaction:
        # rollback the transaction if any error occur during the processing
        qmgr.backout()
finally:
    queue.close()
    qmgr.disconnect()

The MQRC_ENVIRONMENT_ERROR typically indicates a problem with the environment or configuration, rather than an issue with the code itself but where and what should I need to check?

IBM MQ's transactions (begin/commit/backout) should be used in conjunction with the MQOO_INPUT_EXCLUSIVE and MQOO_OUTPUT options. These options control how the queue is opened and dictate the ability to participate in transactions.


Solution

  • You are not using an external transaction manager, therefore, you cannot use the MQBEGIN MQ API call (i.e. qmgr.begin() ). Remove it and your code will be doing a local UOW.

    Also, if you want the consumer application to be using a local UOW then add the following to your code plus the MQBACK & MQCMIT MQ API calls.

    gmo = pymqi.GMO()
    gmo.Options = pymqi.CMQC.MQGMO_SYNCPOINT | pymqi.CMQC.MQGMO_FAIL_IF_QUIESCING