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 7741991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:10:10+00:00 2026-06-01T09:10:10+00:00

I am using Spring with Hibernate in my project.There are many methods written in

  • 0

I am using Spring with Hibernate in my project.There are many methods written in DAO implementation java file and every method is using the same try/catch/finally lines of code which seem redundant to me.
I am told to optimize/refactor the code since the file LOC exceeds 10k.I read somewhere that using HibernateDaoSupport we need not to worry about exceptions or closing the session. It will be taken care of by Spring itself.
Could somebody please help me how to proceed or do the needful or any better way to handle exceptions?I am pasting below code of one method in DAO layer.

public class CSDDaoImpl extends HibernateDaoSupport implements CSDDao {

public Deal getDealStructure(long dealId) throws CSDServiceException {  
        Session session = null;  
        try {  
            session = getSession();  
            Deal deal = (Deal) session.createCriteria(Deal.class).add(  
                    Restrictions.eq("dealId", dealId)).uniqueResult();  
            return deal;  
        } catch (DataAccessResourceFailureException darfex) {  
            String message = "Failed to retrieve the deal object.";  
            CSDServiceException ex = new CSDServiceException(message, darfex);  
            ex.setStackTrace(darfex.getStackTrace());  
            ex.setErrorCode(Constants.DATA_ACCESS_FAILURE_EXP);  
            ex.setMessageToUser(message);  
            throw ex;  
        } catch (IllegalStateException isex) {  
            String message = "Failed to retrieve the deal object.";  
            CSDServiceException ex = new CSDServiceException(message, isex);  
            ex.setStackTrace(isex.getStackTrace());  
            ex.setErrorCode(Constants.ILLEGAL_STATE_EP);  
            ex.setMessageToUser(message);  
            throw ex;  
        } catch (HibernateException hbex) {  
            String message = "Failed to retrieve the deal object.";  
            CSDServiceException ex = new CSDServiceException(message, hbex);  
            ex.setStackTrace(hbex.getStackTrace());  
            ex.setErrorCode(Constants.HIBERNATE_EXP);  
            ex.setMessageToUser(message);  
            throw ex;  
        } finally {  
            if (session != null && session.isOpen()) {  
                try {  
                    session.close();  
                } catch (HibernateException hbex) {  
                    log.error("Failed to close the Hibernate Session.", hbex);  
                    hbex.printStackTrace();  
                    CSDServiceException ex = new CSDServiceException(  
                            "Failed to close the Hibernate Session.", hbex);  
                    ex.initCause(hbex.getCause());  
                    ex.setStackTrace(hbex.getStackTrace());  
                    throw 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-01T09:10:11+00:00Added an answer on June 1, 2026 at 9:10 am

    The best approach of handling exceptions is i believe through writing an Exception Interceptor to intercept all your DAO calls and you can catch the ones you only need in your application and wrap it with your own custom application specific exceptions.

    You definitely do not need to work directly with session once an exception is thrown. That would defeat the purpose of using HibernateDaoSupport and Spring.

    Have a look at this link : http://static.springsource.org/spring/docs/current/spring-framework-reference/html/classic-spring.html

    Hope that helps.

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

Sidebar

Related Questions

I am developing project using Spring , Struts2 & Hibernate . Now I want
I have a project with spring and hibernate in GWT, I am using below
I am creating a project using maven with spring and hibernate. I added a
I'm trying to make a project using struts 2, spring 3 and hibernate, but
I'm trying to build a spring roo project using hibernate and an oracle database.
I have some web-project (Spring MVC + Hibernate). For hibernate objects I am using
i have a java web application, using Spring, Jersey (jax-rs) and Hibernate. It runs
I am trying to develop a .NET Web Project using NHibernate and Spring.NET, but
We're using Spring/Hibernate on a Websphere Application Server for AIX. On my Windows machine,
I'm writing a web application using Spring/Hibernate that displays a report to a user,

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.