1
votes

I couldn't find enough information about artemis transactions in the documentation. It was not clear to me how artemis transactions work so I would like to clarify it. There are two methods in org.apache.activemq.artemis.api.core.client.ClientSession class:

 /**
    * Commits the current transaction, blocking.
    *
    * @throws ActiveMQException if an exception occurs while committing the transaction
    */
   void commit() throws ActiveMQException;

and

/**
    * Rolls back the current transaction.
    *
    * @throws ActiveMQException if an exception occurs while rolling back the transaction
    */
   void rollback() throws ActiveMQException;

I cannot find any method that begins transaction and I do not know what exactly is a transaction in artemis. What operations can be committed or rollbacked?

I have tested several scenarios and I have my conclusions. Thera are two operations which can be commited or rollbacked: message sending and message receiving (acknowledgment). Methods ClientSession#commit and ClientSession#rollback are only for committing and rollbacking messages sending and acknowledgment. No method is needed to begin transaction. There are only uncommitted messages which can be committed or rollbacked. Two arguments autoCommitSends and autoCommitAcks that can be used during session creation are essential for artemis transaction.

Let's consider message sending.

If autoCommitSends is set to true then ClientSession#commit and ClientSession#rollbackdo nothing.

If autoCommitSends is set to false then ClientSession#commit method has to be called after sending message (ClientProducer#send) to really send massage. Method ClientSession#commit commits all sent (in this session) and uncommitted messages. Messages can be rollbacked by calling ClientSession#rollback method. Method ClientSession#rollback rollbacks all sent (in this session) and uncommitted messages.

Let's consider message receiving.

If autoCommitAcks is set to true then ClientSession#commit and ClientSession#rollbackdo nothing.

If autoCommitAcks is set to false then ClientSession#commit method has to be called after message acknowledgment (ClientMessage#acknowledge) to really acknowledge massage. Method ClientSession#commit commits all acknowledged (in this session) and uncommitted messages. Messages acknowledgments can be rollbacked by calling ClientSession#rollback method. Method ClientSession#rollback rollbacks all acknowledged (in this session) and uncommitted messages.

Are my conclusions correct?

===EDIT===

I delete part of this question related to acknowledge and individualAcknowledge because it was unrelated to main question and made my question too complex.

1
You've got lots of questions here (both explicit and implicit). They are all about transactions, but you need to focus down onto individual issues. For example, perhaps just ask about how acknowledge and individualAcknowledge relate in this question, and create a new questions about how you start a transaction, how auto-commit works, etc.Justin Bertram
On Stack Overflow a question should focus only on a single problem. When you ask multiple questions it makes it difficult to identify a correct answer. For example, what if an answer only addresses one part of your question? Assuming that it's accurate, is it "correct"? What if there are 2 answers and they each accurately address different parts of your question. Which one is "correct"? This kind of ambiguity is bad for community involvement and devalues the content.Justin Bertram
Question is about transactions so I expect answer about transactions. You are right that I unnecessarily mentioned about acknowledge and individual acknowledge. I will delete this part from question.Mariusz
You ask, "What operations can be committed or rollbacked?" and then you ask, "Are my conclusions correct?" However, you have lots of conclusions, and each one might potentially need to be addressed. Also, you don't need to add a message to the question explaining your edit. Anybody can look at the edit history and see what you did. You can even provide a short explanation that will appear in the edit history.Justin Bertram

1 Answers

0
votes

There is no method to "start" a transaction with an Artemis core ClientSession. If autoCommitSends or autoCommitAcks is false then any send or ack performed by the session once it is created will be a part of the transaction until commit() or rollback() is called. Based on the values set for autoCommitSends and autoCommitAcks both sending and acking can be a part of the same transaction or just one or the other.

If autoCommit is true for either sends or acks then calling commit() or rollback() won't have any functional impact on the corresponding operation. However, the client will still send the command to the broker and wait for the broker's response. It's the client's responsibility not to invoke these operations unless it's actually necessary.