Using DatabaseExceptionHandler in Struts(urgent)
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   ABHISHEK_SHRIVASTAVA
Posted On:   Wednesday, January 5, 2005 05:21 AM

The approach that I am using right now is to make an ExceptionHandler and configure it in struts-config.xml using something like following: key= "error.exception.SQL " handler= "com.exception.MyExceptionHandler " path= "error.jsp " / > This way it would catch ALL database related exceptions and throw them. But the problem goes deep into into handling a specific database exception, for ex. duplicate key exception during insert operation or child dependency during delete operation. Now different databases would throw different kind of errors for these cases. My problem is how to catch these specific SQL errors and show   More>>


The approach that I am using right now is to make an ExceptionHandler and
configure it in struts-config.xml using something like following:



key= "error.exception.SQL "
handler= "com.exception.MyExceptionHandler "
path= "error.jsp " / >


This way it would catch ALL database related exceptions and throw them. But
the problem goes deep into into handling a specific database exception, for
ex. duplicate key exception during insert operation or child dependency
during delete operation. Now different databases would throw different kind
of errors for these cases.
My problem is how to catch these specific SQL
errors and show a user friendly message according to each of them. For ex,
when duplicate key exception is thrown, I want to show a message like
"Duplicate key cannot be inserted " or show a message like "Child record
exists for this key " for a child dependency case. How can I catch particular
error codes or message thrown by the database and show them and STILL KEEP
EVERYTHING GENERIC so that tommorrow if I change my database, I only need to change the DAOs.

Thanks & Rgds,
Abhishek

   <<Less

Re: Using DatabaseExceptionHandler in Struts(urgent)

Posted By:   Kiran_K  
Posted On:   Thursday, January 20, 2005 04:21 AM


Hi,

You can handle each individual exception's in action mappings of 'struts-config.xml', as shown this piece of code.

















path="/content/UploadFile.jsp"
type="java.io.FileNotFoundException"/>



path="/content/UploadFile.jsp"
type="Exception"/>






















Here "upload.message" is a key present in your Application resources. You can specify as many different exceptions you can.

Hope this helps, Kiran.

Re: Using DatabaseExceptionHandler in Struts(urgent)

Posted By:   Prasanna_Talakanti  
Posted On:   Thursday, January 6, 2005 09:37 AM


I guess you could pass the exception you caught, something like this

try ...etc
catch(SqlException se)
throw new MyExceptionHandler(se.getErrorCode());


one of you'r handler constructor can then set a message based on the error code or you can actully pass the SqlException object to you'r handler, good luck

Re: Using DatabaseExceptionHandler in Struts(urgent)

Posted By:   Christopher_Koenigsberg  
Posted On:   Thursday, January 6, 2005 09:36 AM

Yes, you have hit upon one of the ugly things about JDBC and SQLExceptions.


You have to parse the vendor specific message strings, in the recursive SQLExceptions (since there can be more than one).


Perhaps look into the Spring Framework (www.springframework.org). It comes with some DAO tools where they do some of this vendor specific parsing for you, and translate SQLExceptions into their own hierarchy of declared exceptions.

About | Sitemap | Contact