Raj K
Raj K

Reputation: 470

EJB Transaction

I read all about the EJB Transactions through online resources and when I applied it, it's not working as explained.

What I'm trying to do is

I have an stateless EJB method, whose transaction annotation is

 @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) 

and I'm trying to invoke a method in another stateless EJB, whose Transaction annotation is

 @TransactionAttribute(TransactionAttributeType.REQUIRED).

Here in above what I thought is a new Transaction is created in my Caller method whose annotation is

TransactionAttributeType.REQUIRES_NEW 

and when i call an second method whose annotation is

  TransactionAttributeType.REQUIRED 

the previous transaction carries on, but in my case a new transaction is created in the second method.

Do anyone can help me whats going on here, thanks in advance.

I have posted my code below.

EOutboundHandler.java

@Stateless
@EJB(name = "EOutboundHandler")
public class EOutboundHandler {

@EJB
    private EData eData;

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public void Process() {
        while (ProcessRequests()) {
        }
    }
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    private boolean ProcessRequests() {
 EQueue eQueue = eData.searchEQ();
}
}

EData.java

     @Stateless
        @EJB(name="EData")
        @TransactionAttribute(TransactionAttributeType.REQUIRED)
        public class EData {
            @PersistenceContext(unitName=EDataConstants.PERSISTANCE_UNIT_NAME)
            private EntityManager em;
        public EdiTxnQ searchEdiTxnQForSendMsg()
            {

                String searchSql = 
                        "SELECT * FROM dbo.EQueue with (updlock, readpast) Where id = 1";

                Query searchQuery = em.createNativeQuery(searchSql, EdiTxnQ.class);
                List<EdiTxnQ> list = searchQuery.getResultList();

                if (list.isEmpty())
                {
                    return null;
                }
                else
                {
                    return (EdiTxnQ)list.get(0);
                }

            }
}

Upvotes: 0

Views: 255

Answers (1)

Amit
Amit

Reputation: 1121

IMO, you would need to invoke "ProcessRequests()" using the EJB Stub ( I am not sure what is it called in newer version of EJBs, earlier it was EJB Remote/Local interface). Because you are invoking "ProcessRequests()" as a normal method invocation from within a method which has "Not required" transaction attribute, the "Required_new" is not kicking in. Hope this helps. -Amit

Upvotes: 1

Related Questions