1

Is it possible to use TransactionManagementType.CONTAINER instead of TransactionManagementType.BEAN in a remote EJB implementation?

For further clarification, I don't want to deal with begin and roolback methods in my busines layer but I didn't find another way to make it work.

I'm using Glassfish 5 and corbaname:iiop protocol to access my remote EJB.

Example of usage:

This code block runs on my server:

package br.com;
@Remote
interface IRemote {void method();}

@Stateless
@TransactionManagement(value = TransactionManagementType.BEAN)
class Remote implements IRemote { 
@Resource
private UserTransaction ut;

void method(){
    try{
        ut.begin();
        <my business logic>
        ut.commit();
    }
    catch(Exception e){...}
}

And this code block runs in an app on my local machine:

@Stateless
class LocalBean {
    @EJB(mappedName = "corbaname:iiop:myserver.com:3700#java:global/RemoteApp/Remote!br.com.IRemote"
    private IRemote remote;
} 

I'm following this tutorial from Oracle but it uses TransactionManagementType.BEAN without any explaination.

Is there another way to do this without the need of the transaction above?

fantaghirocco
  • 4,761
  • 6
  • 38
  • 48

3 Answers3

0

Look at Container-Managed Transactions

RequiresNew Attribute If the client is running within a transaction and invokes the enterprise bean’s method, the container takes the following steps:

Suspends the client’s transaction

Starts a new transaction

Delegates the call to the method

Resumes the client’s transaction after the method completes

If the client is not associated with a transaction, the container starts a new transaction before running the method.

You should use the RequiresNew attribute when you want to ensure that the method always runs within a new transaction.

https://docs.oracle.com/javaee/5/tutorial/doc/bncij.html

Hider
  • 56
  • 3
  • When i use whatever @TransactionAttribute i got the error on deployment "Method level transaction attributes may not be specified on a bean with transaction type [Bean]. Please see server.log for more details." – Henrique Fernandes Cipriano Nov 30 '17 at 13:56
  • And when i don't use @TransactionAttribute i got the exception in runtime "TransactionRequiredException" when i perform a persist operation – Henrique Fernandes Cipriano Nov 30 '17 at 14:00
  • Transaction attributes can only be used in CMTs: See https://docs.oracle.com/javaee/6/api/javax/ejb/TransactionAttribute.html "The TransactionAttribute annotation...can only be specified if container managed transaction demarcation is used." – JL_SO Sep 17 '18 at 15:48
0

By default (from the EJB spec) a session bean use ContainerManagedTransaction and Required if there is no annotation. This should work remote or local.

Also you can do it explicit

@TransactionManagement(value = TransactionManagementType.CONTAINER)
@Required
public class MyBean ... {
   @Supported  // override class level annotaion
   public void method(...) {
   }
}

From the error message above I suppose you try to use the TMType.BEAN but added a @Required or so to the method, this is suppressed by container as the combination is not allowed!

wfink
  • 347
  • 1
  • 6
  • When i use `@TransactionManagement(value = TransactionManagementType.CONTAINER)`, I got a exception in client: `javax.ejb.EJBException: java.lang.SecurityException ... Caused by: java.lang.SecurityException at com.sun.jts.jta.TransactionManagerImpl.rollback(TransactionM‌​anagerImpl.java:376)` – – Henrique Fernandes Cipriano Dec 05 '17 at 03:07
  • About the @Required and @Supported i didn't find the classes... I use maven project with ` javax javaee-api ${javaee.api.version} provided ` – Henrique Fernandes Cipriano Dec 05 '17 at 03:09
0

You need to remove @TransactionManagement-Annotation completely from your remote bean, then it defaults to CMT. The usage of UserTransaction will then be illegal. So you have to remove that as well, but that was the point, no?

But be aware: you might create remote transactions which means that the two containers might synchronize using XA-Protocol. If you don't want that, define the TransactionAttribute to TransactionAttributeType.REQUIRES_NEW, then you will have the same behavior as before, without explicite user-transactions.

aschoerk
  • 3,333
  • 2
  • 15
  • 29
  • When i remove @TransactionManagement, I got a exception in client: javax.ejb.EJBException: java.lang.SecurityException ... Caused by: java.lang.SecurityException at com.sun.jts.jta.TransactionManagerImpl.rollback(TransactionManagerImpl.java:376) – Henrique Fernandes Cipriano Dec 05 '17 at 02:54
  • I don't think that the SecurityException is related to CMT, probably: [see](https://stackoverflow.com/questions/23844092/container-managed-transactions-can-not-rollback) just catch it, or solve the cause of it. Perhaps you can show the full message and stacktrace – aschoerk Dec 05 '17 at 06:44