Catching SQLException in CMP Container Generated Code
Joined: Oct 28, 2004
I hope this is an advanced question. I have exhausted two text books and most forums and cannot get an answer.
I have a CMP Entity Bean that persists to a table that has one column with a UNIQUE index.
When my client sends a JMS message to a Message Driven Bean to create another Entity Bean using data that contains a duplicate value for the UNIQUE column, I can see the SQLException on my server log.
BUT .. it seems impossible to get the container to thrown it back up to either my Entity Bean create()code or to the Message Driven Bean onMessage () method code.
Actually what happens is the server tries 11 times to create the record with the duplicate entry. It throws 11 SQLExceptions on the server log. It adds 11 junk records to the data base (all null entries except the PK which is auto generated), and my message driven bean sails happily along unaware of the error.
I know the last, because after my my onMessage () method creates an Entity Bean, it sends a message to the client saying whether it encountered an error. And it sends 11 messages saying ... nope just fine.
I know I can catch it if I switch to BMP, but it seems incomprehensible that CMP would not have some mechanisim to send something as simple as duplicate column entry back to the entity bean code or to the message driven bean code that tried to create the entity bean.
Am I missing something?
I hope this is a beginners error that only an advanced programer knows the answer to.
Joined: Sep 19, 2004
Since your transaction is failing you are getting a rollback and an MDB will push the message back on the queue. There will be 10 retry attempts (depends on your configuration) for a total of 11 failures. You will get an exception in your onMessage however.
Find the entity in question and add the entity only if the find operation throws a "not found" exception. Usually this sort of thing is take care of through a UI or some pre-message business logic. For example, you establish the contract that it's an error to attempt to add a record without checking whether it exists first. That's better than automatically checking IMO.
Joined: Sep 19, 2004
BTW, are you catching in onMessage? If that's not triggering try throwable. You will not get specific exceptions from the container but they should be chained to the original exception.