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

60 Views Asked by At
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.

2

There are 2 best solutions below

1
Roger On BEST ANSWER

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
0
Mark Taylor On

MQBEGIN

a) Is only used/needed to start XA (global) transactions, not one-phase transactions

b) Is only available for local bindings, not clients. If you try to use it from within a client, it returns MQRC_ENVIRONMENT_ERROR

From your program, there doesn't seem to be any reason for the call to begin. One-phase transactions are started automatically by the _SYNCPOINT option on put/get.