Catching a system exception caused by calling an ejb entityManager method
As I searched this forum for a discussion to answer my question, I located some discussions that while relevant indicated some people posting did not understand basic concepts regarding system exceptions and ejb抯.
So I first make this statement, and ask someone to correct it if it wrong. If an ejb method generates a system exception (for example entityManger.persist(foo)), it is the ejb container which packages the exception (in this case lets say an sql exception) into an ejb.exception and passes that system exception to the caller of the method. The container then garbage collects the instance of the ejb whose method led to the system exception. The caller of the method could be another ejb or a remote client, and it is the only place to catch the exception the server抯 ejb container has packaged. (Note a special annotation exists to inform the container weather or not any transaction in progress should be rolled back.) The point I am making is any effort to catch the exception must lie outside the ejb whose method has generated it. In my example the sql exception cannot be caught using try/catch with the persist() call. The try/catch must be outside that ejb (in another ejb or client). I read several posts indicating this inability to catch these system exception抯 inside the offending ejb was a quirk of particular app such as hibernate or jboss. I believe this is incorrect; -someone can correct me if here if needed. (The persist() and other entityManager methods do throw some signed exceptions and they can be caught at the method call, but these entity manager methods call other system resources, and exceptions which occur there cannot be caught from the offending bean.)
My question is this: How can I catch the root cause of such system exceptions resulting from calls to entity Manger functions inside ejb抯, -for example, an sql exception resulting from a persist() call? Do ejb抯 have visibility to the details of sql exceptions, if so how? Can it be done in a manner independent of the database vendor?
Or is it the case that the ejb container always wraps a system exception into an ejb exception so that the root cause information is always lost to the enterprise app. I know it gets logged on the server, but it would be extremely useful and seems reasonable to expect my ejb抯 to be able to interpret failed database transactions. One would think that there must be a way of exposing some set of standardized sql errors to the entityManagers methods, or is this wrong? Thanks.

