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

  • Home
  • SEARCH
  • 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 7014227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:29:38+00:00 2026-05-27T22:29:38+00:00

We have a Java application running on JBoss 5.1 and in some cases we

  • 0

We have a Java application running on JBoss 5.1 and in some cases we need to prevent a transaction from being closed in case a JDBCException is thrown by some underlying method.

We have an EJB method that looks like the following one

@PersistenceContext(unitName = "bar")
public EntityManager em;

public Object foo() {
  try {
    insert(stuff);
    return stuff;
  } (catch PersistenceException p) {
    Object t = load(id);
    if (t != null) {
      find(t);
      return t;
    }
  }
}

If insert fails because of a PersistenceException (which wraps a JDBCException caused by a constraint violation), we want to continue execution with load within the same transaction.

We are unable to do it right now because the transaction is closed by the container. Here’s what we see in the logs:

org.hibernate.exception.GenericJDBCException: Cannot open connection
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Cannot open connection
    at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)

   ...

Caused by: javax.resource.ResourceException: Transaction is not active: tx=TransactionImple < ac, BasicAction: 7f000101:85fe:4f04679d:182 status: ActionStatus.ABORT_ONLY >

The EJB class is marked with the following annotations

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)

Is there any proper way to prevent the transaction from rolling back in just this specific case?

  • 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-27T22:29:39+00:00Added an answer on May 27, 2026 at 10:29 pm

    You really should not try to do that. As mentioned in another answer, and quoting Hibernate Docs, no exception thrown by Hibernate should be treated as recoverable. This could lead you to some hard to find/debug problems, specially with hibernate automatic dirty checking.

    A clean way to solve this problem is to check for those constraints before inserting the object. Use a query for checking if a database constraint is being violated.

    public Object foo() {
        if (!objectExists()) {
            insertStuff();
            return stuff();
        }
        // Code for loading object...
    }
    

    I know this seems a little painful, but that’s the only way you’ll know for sure which constraint was violated (you can’t get that information from Hibernate exceptions). I believe this is the cleanest solution (safest, at least).


    If you still want to recover from the exception, you’d have to make some modifications to your code.

    As mentioned, you could manage the transactions manually, but I don’t recommend that. The JTA API is really cumbersome. Besides, if you use Bean Managed Transaction (BMT), you’d have to manually create the transactions for every method in your EJB, it’s all or nothing.

    On the other side, you could refactor your methods so the container would use a different transaction for your query. Something like this:

    @Stateless
    public class Foo {
        ...
        @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
        public Object foo() {
            try {
                entityManager.insert(stuff);
                return stuff;
            } catch (PersistenceException e) {
                if (e.getCause() instanceof ConstraintViolationException) {
                    // At this point the transaction has been rolled-backed.
                    // Return null or use some other way to indicate a constrain
                    // violation
                    return null;
                }
                throw e;
            }
        }
    
        // Method extracted from foo() for loading the object.
        public Object load() {
            ...
        }
    }
    
    // On another EJB
    @EJB
    private Foo fooBean;
    
    public Object doSomething() {
        Object foo = fooBean.insert();
        if (foo == null) {
            return fooBean.load();
        }
    
        return foo;
    }
    

    When you call foo(), the current transaction (T1) will be suspended and the container will create a new one (T2). When the error occurs, T2 will be rolled-backed, and T1 will be restored. When load() is called, it will use T1 (which is still active).

    Hope this helps!

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

Sidebar

Related Questions

I have a java web application (running on JBoss), which need to authorizate users
I have a Java desktop application running from the command line. I need to
I have an Java application running in JBoss in which I have enabled JMX
We have a Web Java based application running on JBoss with allowed maximum heap
I have a java application running on Linux system. Currently we are facing some
We have a Java application server running on Sun Solaris. We now need to
I have a Java EE application running on jboss-5.0.0.GA. The application uses BIRT report
I have a Java EE application running on jboss 4.2.3GA. Login-config.xml is including the
We have an web application running under JBoss 5 which periodically launches a 'java'
I have a Java web application running on JBoss using JAAS for authentication. I

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.