1

I am using container-managed transaction in my EJB application.

I have two stateless Java objects, which are class A and class B.

class B is instantiated in a method of class A.

Class B has two methods : method A and method B.

Method A is using transaction type = requires_new

Method B is using transaction type = required

There are 2 sql insertion statement in the two methods respectively. Method A and method B have different sql statement.

I find out that if there is any error happened in method B, everything in method A is rollback. This is not I want. I want everything in method A is committed even though method B raise System exception.

Thus, I don't think method A and B are running using a different transaction.

Can I know is there any way to system.out.println a boolean value showing a JTA transaction is new or not?

Thanks.

Below is the additional pseudo-code snippet to clarify my question above.

stateless class A{
  method(){
    Class b = new Class B();
    b.methodA();
    b.methodB():
  }
}

stateless class B(){
   method A(){ // There is an insert statement here.}
   method B(){ // There is another insert statement here.}

   //If anything happen in method B, only rollback B, don't rollback A.
 }
Charles Brown
  • 917
  • 2
  • 10
  • 20
  • Transaction assosiated with the current thread could be obtained from TransactionManager (https://docs.oracle.com/javaee/7/api/javax/transaction/TransactionManager.html) and compared to another by it's hash code. – Aleksandr Erokhin Aug 24 '15 at 11:43
  • If methodA has Requires_new, it should't be rollbacked in case of problems in methodB, as it should already be commited. See this [post](http://stackoverflow.com/a/10821117/3701228). What server are you running? – Gas Aug 25 '15 at 08:57
  • Hi Gas. I am using weblogic I highly doubt the problem is caused by weblogic server setting. My code should work. – Charles Brown Aug 25 '15 at 14:45
  • Alex Erohin, as this is a container-type, not bean-type; I do not think we can obtain transactionManager object. I get error when called entityManager.getTranasaction(); – Charles Brown Aug 25 '15 at 15:01

1 Answers1

0

You are running the method B in the same transaction as that of method A. so failure of method B will rollback the insert happening in method A.

Run both the methods in different transactions if you don't want the method B to interfere with the method A.

If you are using Spring, can use TransactionSynchronizationManager to find out the transaction level information.

Amit Bhati
  • 5,569
  • 1
  • 24
  • 45