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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:55:29+00:00 2026-06-09T02:55:29+00:00

I have the following situation with Java persistence: public ReturnCodes startWork() { sessionBackup =

  • 0

I have the following situation with Java persistence:

public ReturnCodes startWork() {

    sessionBackup = (BaseService<Backup>) ctx.lookup("XYZ/BackupServiceImpl/local");

    Backup backup = new Backup();
    backup.setStatus(BackupStatus.EXECUTING);
    ....
    sessionBackup.save(Backup) //at this point is not actualy saved into DB

    ....//try to connect somewhere
    if ( !ConnectSuccess) {
        sessionBackup.remove(backup);
        return ReturnCodes.REQUESTABORT
    }
    ....
}

@Stateless
public class BackupServiceImpl extends BaseServiceImpl<Backup> implements
    BaseService<Backup>
{

    @Override
    protected Class<Backup> getEntityClass()
    {
        return Backup.class;
    }
}

And the save and remove methods of BaseServiceImpl:

public abstract class BaseServiceImpl<T extends Serializable> implements
    BaseService<T>
{

  protected EntityManagerFactory emf;

  public T save(T entity)
  {
    EntityManager em = emf.createEntityManager();
    em.persist(entity);

    return entity;
  }

  public void remove(T entity)
  {
    EntityManager em = emf.createEntityManager();
    try
    {
      final Method method = getEntityClass().getMethod("getId");
      final String id = (String) ((T) method.invoke(entity));
      entity = em.find(getEntityClass(), id);
      em.remove(entity);
    }
    catch (final Exception ex)
    {
      logger.log(Level.WARNING, "Unexpected error", ex);
    }
  }
}

I don’t want to save into the DB in case ConnectSuccess fails, but the remove method fails to find the entity (because is not yet into the DB), and after returning ReturnCodes.REQUESTABORT the entity is saved.
How can I cancel the save?

  • 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-09T02:55:30+00:00Added an answer on June 9, 2026 at 2:55 am

    In general in this case you do a setRollbackOnly().

    You may also throw an exception which will trigger the rollbackonly.

    See http://www.developerscrappad.com/547/java/java-ee/ejb3-x-jpa-when-to-use-rollback-and-setrollbackonly/

    By the way doing this in your code:

    catch (final Exception ex)
    {
      logger.log(Level.WARNING, "Unexpected error", ex);
    }
    

    is too broad and will block rollback functionality.

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

Sidebar

Related Questions

Imagine I have the following situation: Test1.java import java.lang.ref.WeakReference; public class Test1 { public
I have the following situation. I have a HashMap in Java with keys as
I have the following situation... I use CallableStatement from java.sql package. When I use
I have the following two buttons: jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {
I have the following situation. I have a Java Class that inherits from another
I have the following situation: using a classical Java server (using ServerSocket) I would
I have the following situation: I have a java code launching a stored procedure.
I have the following situation: Interface public interface Ranged{ public Object getRangeStart(); public Object
It is the following situation: I currently have multiple Java projects in Eclipse. All
I have an annoying problem with Java’s layout managers. I have the following situation:

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.