Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to handle a JDBC DAO 's Exception?

 
gao zhixin
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost every JDBC's function can throw a SQLException, even in the close() function, How to handle it? Should I handle it inside DAO? If I handle it inside DAO, and in catch block , I close the Connection, the problem occurs, because the Connection's close() function can throw a SQLException, How to handle it? throw it again?But if I throw it in an upper layer, how this layer know which connection should be closed? Please help me, thx.
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
probably you should go in more details for try/catch



Shailesh
 
gao zhixin
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think upstair doesn't know my question, or perhaps I explained poorly. I want to know, the SQL Exception should handle in DAO itself, or throw them to an up-layer? and is there a graceful way to handle this?
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's better to handle SQL Exceptions in DAO and let DAO throw ApplicationException with SQLException wrapped inside. If you throw the SQLException to the next layer then your controller/view layer must handle this exception (personally I don't like to see java.sql.* package being imported in any layer other than Modal).

You can write something like this in dao

public void commitChanges() throws ApplicationException
{
try
{
... //JDBC Code
}catch(SQLException e)
{
throw new ApplicationException(e.getMessage())
// or wrap the exception with InvocationTargetException so that you
// will get complete stack trace.
}
}
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gao zhixin:
perhaps I explained poorly.


Indeed, your question was not very precise? anyways hope you have got the answer of your query


Shailesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic