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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:30:57+00:00 2026-05-16T14:30:57+00:00

I have a the following use case where I’m receiving a message via JMS

  • 0

I have a the following use case where I’m receiving a message via JMS regarding an entity, via a unique property of it (not PK) and it requires me to update the state of the entity:

HibernateUtil.beginSession();  
HibernateUtil.beginTransaction();  
try{  
  Entity entity = dao.getEntityByUniqueProperty(propertyValue);  
  if (entity==null){  
    entity = dao.addEntityByUniqueProperty(propertyValue)  
  }  
  entity.setSomeProperty(otherPropertyValue);
  HibernateUtil.commitTransaction();
} catch (ConstraintViolationException e){  
  HibernateUtil.rollbackTransaction();  
  //Do other things additionally  
} catch (StaleStateObjectException e){  
  HibernateUtil.rollbackTransaction();  
  //Do other things additionally  
} finally {  
  HibernateUtil.closeSession();  
}

In this use case I have to be prepared for the fact that the entity which I’m trying to update is not yet created and so I request that such entity be created (a template of it to be precise with the unique property) and then I change it.
My dillema is as follows:
On the one hand I have two clearly different blocks and I should use the different catch clauses where appropriate BUT seeing as the end case where the entity is not there when I query but is there a ms later when I try to create it (hence ConstraintViolationException) is something that shouldn’t happen too often and to insert because of that an additional commit/beginTransaction at the middle just seems waistfull.

I’m mainly concerned about the additional performance hit of the session synchronization and the JDBC connection which are done when the commit/begin occur.
Am I wrong? Am I looking for optimization where I shouldn’t? Am I missing something?
Thanks in advance

  • 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-05-16T14:30:58+00:00Added an answer on May 16, 2026 at 2:30 pm

    I think I’ve actually found the specific question to my use case and that is to open the transaction only when actually needed and so I save the premature performance dillema:

    try {
        HibernateUtil.beginSession();
        Entity entity = dao.getEntityByUniqueProperty(propertyValue);
        if (entity==null){
            HibernateUtil.beginTransaction();
            try {
                entity = dao.addEntityByUniqueProperty(propertyValue)
                HibernateUtil.commitTransaction();
            } catch (ConstraintViolationException e){
                HibernateUtil.rollbackTransaction();
                HibernateUtil.closeSession();
                HibernateUtil.beginSession();
                entity = dao.getEntityByUniqueProperty(propertyValue);
                //Do other things additionally
            }
        }
        entity.setSomeProperty(otherPropertyValue);
        HibernateUtil.commitTransaction();
    } catch (StaleStateObjectException e){
        HibernateUtil.rollbackTransaction();
        //Do other things additionally
    } finally {
        HibernateUtil.closeSession();
    }
    

    This will enable me to localize the specific risks in each Transaction and also avoid commiting and opening a transaction when it is unneeded.
    Thanks for your comments.

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

Sidebar

Related Questions

No related questions found

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.