9 Using Batching with Message-Driven Beans

Within an MDB, business logic, possibly including database transactions, is performed within the onMessage() method. Within an EJB application, multiple MDBs can perform multiple onMessage() calls concurrently. If each onMessage() call performs a container-managed transaction, this can create a lot of overhead.

WebLogic Server provides a mechanism for grouping multiple container-managed transaction MDB onMessage() calls together under a single transaction. This mechanism can help increase the performance of an EJB application by implicitly grouping all of the work from different onMessage calls into a single request.

For information on transaction management within MDBs, see Configuring Transaction Management Strategy for an MDB.

Note:

Transaction batching is not effective for all MDB applications. For example, database deadlocks can occur in an application where an MDB makes multiple calls to a database. Using the transaction batching feature will cause the MDB to lock more rows per transaction which can lead to database deadlocks.

Configuring MDB Transaction Batching

You can enable MDB transaction batching by defining the max-messages-in-transaction element or using the equivalent property in activationConfigProperty. The element is part of the message-driven-descriptor element of the weblogic-ejb-jar.xml deployment descriptor.

max-messages-in-transaction defines the batch size WebLogic Server uses to process onMessage() transactions. However, increasing the batch size can increase latency. You should start with a small value, 5 for example. You can increase this value as your application performance allows.

When using MDB batching, more messages are processed per transaction. This may cause more transactions to time out since more work is being performed in each transaction. You can increase the transaction timeout be increasing the value of trans-timeout-seconds attribute of weblogic-ejb-jar.xml. Alternatively, you can use @TransactionTimeoutSeconds annotation, as follows:

import weblogic.javaee.TransactionTimeoutSeconds;
...; 
@TransactionTimeoutSeconds(value = 60);
...;
public class MyMDB ...

How MDB Transaction Batching Works

MDB transaction batching does not require any changes to application code. As far as the application is concerned, individual messages are still processed one by one. There is no application level message list.

Internally, WebLogic Server creates a transaction for a batch. A message is added to the transaction until the number of messages in the transaction is equal to the batch size defined by max-messages-in-transaction or the equivalent property in activationConfigProperty. When the number of messages in the equals max-messages-in-transaction or there is no next message to be added to the transaction, the transaction is submitted for processing. See Figure 9-1.

Figure 9-1 MDB Transaction Batching Transaction Processing Flow

Description of Figure 9-1 follows
Description of "Figure 9-1 MDB Transaction Batching Transaction Processing Flow"

If an individual onMessage() call fails, then the entire batch is rolled back. If the failure was due to a transaction timeout, as defined in the trans-timeout-seconds attribute of weblogic-ejb-jar.xml, the MDB container temporarily reduces the batch size and attempts to process the transactions with smaller batches.

If failure occurs for another reason, the MDB reprocesses each message within the failed batch as an individual transaction. This avoids the scenario where an individual onMessage() call can permanently hang an entire batch.