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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:27:20+00:00 2026-06-02T01:27:20+00:00

Maybe a stupid question, but is it necessary to make a rollback on a

  • 0

Maybe a stupid question, but is it necessary to make a rollback on a transaction in the catch-block if the EntityManager.merge() throws an exception?
Or does the exception itself mean that the merge won´t work so that next time I run commit the previous changes that throwed the exception won´t apply?

Example:

public void setPerson(Person person) {
EntityManagerFactory emf =   Persistence.createEntityManagerFactory("MyLib");
     EntityManager em = emf.createEntityManager();
     try {            
         if(!em.getTransaction().isActive()){
            em.getTransaction().begin();
         }
         em.merge(person);
         em.getTransaction().commit();
         emf.getCache().evict(Person.class); // clear Person cache
     } catch (Exception ex){
         em.getTransaction().rollback(); // Is this necessary?   
     } finally {
         em.close();
     }
}
  • 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-02T01:27:21+00:00Added an answer on June 2, 2026 at 1:27 am

    The answer depends on the details of em.merge(person) method and the implementation of your database driver.

    If that method only performs one update statement, then the rollback is superfluous. If however it may run multiple updates, then it’s not that clear.

    I personally would keep it there

    If the rollback is removed and your merge method errors our after some updates are done but others are not, then closing a database connection without explicit commit or rollback will either commit or rollback the transaction, depending on the driver implementation. According to the javadoc for java.sql.Connection, the behaviour depends on the implementation. Hence you may end up committing partial updates if you do not rollback yourself on error.

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

Sidebar

Related Questions

This maybe a stupid question, but what does it mean by declaring this: public
maybe this is a stupid question but : i run perl 5.8.8 and i
This question is for a web application. And maybe it's a stupid question but
This maybe a stupid question, but as I can not easily undo my change
Well, maybe it is a stupid question, but I cannot resolve this problem. In
maybe stupid question, but i dont know answer. What is difference between using GetModuleHandle
Maybe this is a very stupid question but... Why should I use tinytext and
Maybe is just a stupid question, but, I would appreciate having an explanation of
Maybe I'm asking a stupid question, but I can't understand this behavior: <?php $this->meeting->google_id
I know this maybe a stupid question but maybe I'm just confused with the

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.