Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8983363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:49:24+00:00 2026-06-15T20:49:24+00:00

How must I handle exceptions inside a mdb? I have the funny feeling that

  • 0

How must I handle exceptions inside a mdb? I have the funny feeling that the exception happens after the try catch block so I’m not able to catch and log it. Glassfish v3 decides to repeat the whole message. It runns into a infinite loop and writes lot’s of logfiles on the harddrive.

I’m using Glassfishv3.01 + Eclipselink 2.0.1

public class SaveAdMessageDrivenBean implements MessageListener {

    @PersistenceContext(unitName="QIS") 
    private EntityManager em;

    @Resource
    private MessageDrivenContext mdc;

    public void onMessage(Message message) {
        try {
            if (message instanceof ObjectMessage) {
                ObjectMessage obj = (ObjectMessage)message;
                AnalyzerResult alyzres = (AnalyzerResult)obj.getObject();
                save(alyzres);
            }
        } catch (Throwable e) { 
            mdc.setRollbackOnly();
            log.log(Level.SEVERE, e);
        }
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    private void save(AnalyzerResult alyzres) throws PrdItemNotFoundException {

       Some s = em.find(Some.class, somepk);
       s.setSomeField("newvalue");

       // SQL Exception happens after leaving this method because of missing field for ex.
    }
}    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:49:25+00:00Added an answer on June 15, 2026 at 8:49 pm

    You got a bad case of message poisoning…

    The main issues I see are that:

    • you are calling directly the save() method in your onMessage(): this means thet the container has no way to inject the proper transaction handling proxy around the save method
    • in any case the save() method should have @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) in order to commit in a separate transaction, otherwise it will join the onMessage transaction (which default to REQUIRED) and bypass your exception handling code, beign committed after the successful execution of onMessage

    What I woud do is:

    Move the save method to a new Stateless session bean:

    @Stateless
    public class AnalyzerResultSaver
    {
        @PersistenceContext(unitName="QIS") 
        private EntityManager em;
    
        @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
        private void save(AnalyzerResult alyzres) throws PrdItemNotFoundException {
            Some s = em.find(Some.class, somepk);
            s.setSomeField("newvalue");
            // SQL Exception happens after leaving this method
        }
    }
    

    Inject this bean in your MDB:

    public class SaveAdMessageDrivenBean implements MessageListener {
    
        @Inject  
        private AnalyzerResultSaver saver;
    
        @Resource
        private MessageDrivenContext mdc;
    
        public void onMessage(Message message) {
            try {
                if (message instanceof ObjectMessage) {
                    ObjectMessage obj = (ObjectMessage)message;
                    AnalyzerResult alyzres = (AnalyzerResult)obj.getObject();
                    saver.save(alyzres);
                }
            } catch (Throwable e) { 
                mdc.setRollbackOnly();
                log.log(Level.SEVERE, e);
            }
        }
    }
    

    Another tip: in this code the message poisoning still exists. Now it derives from the line invoking mdc.setRollbackOnly();.

    I’d suggest here to log the exception and transfer the message to a poison queue, thus preventing the container to resubmit the message ad infinitum.

    UPDATE:

    A ‘poison queue’ or ‘error queue’ is simply a mean to guarantee that your (hopefully recoverable) discarded messages will not be completely lost. It is used heavily in integration scenarios, where the correctness of the message data is not guaranteed.

    Setting up a poison queue implies defining a destination queue or topic and redeliver the ‘bad’ messages to this destination.

    Periodically, an operator should inspect this queue (via a dedicated application) and either modify the messages and resubmit to the ‘good’ queue, or discard the message and ask for a resumbit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing an iOS app that must handle several stereo audio files (ranging
Must I call -deleteRowsAtIndexPaths:withRowAnimation: inside an -beginUpdates -endUpdates block?
I have a BO method that must do two operations to fulfill its contract.
My technical lead insists on this exception mechanism: try { DoSth(); } catch (OurException)
I am developing a basic android app which must handle several different things typically
I need to create a class to handle audio AudioTrack. In this class must
Say I want to define that an URI such as: myapp://path/to/what/i/want?d=This%20is%20a%20test must be handled
I have Labellings which belong to Emails and Labels . Each labelling must be
How can I efficiently catch and handle segmentation faults from C in an OSX
What are the best practices for exceptions over remote methods? I'm sure that you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.