1

The below is my requirement: 1. An MDB receives a message

  1. Triggers a asynchronous method in another session bean - asynchronous because this method will be a long running thread and we don't want to hold the MDB thread for long time. The asynchronous method read's records from DB, processes them and calls #3.

  2. Writes to another MQ and then inserts some data into DB. POSTING TO MQ and DB INSERT should be in one transaction.

Here is the implementation :

For #1 - Using an MDB - container managed transaction without any transaction attribute.

For #2 - A stateless session bean - container managed, asynchronous, but transaction attribute as NOT_SUPPORTED (not supported because this is a long running thread, so don't want the transaction to be timed out).

For# 3 - A stateless sessions bean (invoked from #2 for every record that's being read in 2 - executed in a loop) - transaction attribute - REQUIRES_NEW because this method posts to MQ and Inserts into DB.

Issues:

  1. Runtime Exception - When I throw a run time exception from #3, the next records are not processed - the session bean just exits.

  2. Exception - When throwing a custom exception, the message on the queue is not reverted when DB insert fails.

What is the best way to implement or resolve this issue.

I did my best to give details - appreciate any help on this.

Sanjeev
  • 119
  • 4
  • 18
  • What do you want to happen when the DB insert fails? Do you want to catch and log the exception and continue processing the remaining records? Do you want to abort, and if so, do you want to re-process all the records or just the ones you didn't get to process? It could be that a Java batch job would be helpful here, but in any case, maybe start by explaining what your thoughts on error handling are at a high-level, and then maybe someone could help with the exception handling / transaction details. – Scott Kurz Mar 11 '19 at 14:42
  • Incase when a DB insert fails, I would want to just skip that record processing and continue with other records. But when this failure happens, I want to revert the message put on the MQ. Thought about java batch, but would that work well with a combination of MQ and DB combination? Not sure of how the chunk oriented processing works for MQ messages. – Sanjeev Mar 12 '19 at 05:16
  • 1
    So if you just want to throw and handle your own custom exception, you could just inject the EJBContext `@Resource private EJBContext context;` and call `setRollbackOnly()`. (Your 2., probably doesn't do this since checked exceptions don't rollback EJBs by default). – Scott Kurz Mar 12 '19 at 14:57
  • It sounds like you're familiar with Java Batch. If batch seems like it would be useful to you, maybe because you would benefit from checkpoint and restart, maybe because of reuse enabled with the framework of reader/processor/writer logic and the parameterization from XML, then you might consider declaring a "skippable" exception for your custom exception. If you were to order your code so that you only did the MQ put if the DB insert succeeded, then you'd have no need to do a rollback, and so the "skip" might work out for you, allowing you to continue to the next record. – Scott Kurz Mar 12 '19 at 15:00
  • Thanks Scott..setRollbackOnly worked. I need to do more testing and will update if any issues found with this approach. – Sanjeev Mar 13 '19 at 03:30

0 Answers0