3
votes

I am using EJB in order to take advantages of:

  1. Concurrent (instead of creating 2 threads, I divided the work into 2 EJB beans).

  2. Pooling (I use stateless EJB a lot and I love the idea that the pool contains a specific number of bean). This way, I am not afraid of running out of memory. Memory usage is more predictable).

  3. Asynchronous processing (all I need is just an annotation).

Well, the problem is I am using it with MongoDB so I don't need any transaction. I can use @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) and @TransactionAttribute(TransactionAttributeType.NEVER) annotations but... it means I must specify it everywhere?

Is there anyway to disable EJB transaction by default?

2
Since MongoDB doesn't support JTA transactions, simply don't annotate your EJBs. - cassiomolin
isn't it true that all EJB methods have @TransactionAttribute(TransactionAttributeType.REQUIRED) by default? If I don't specify any annotation then it has @TransactionAttribute(TransactionAttributeType.REQUIRED), doesn't it? For example if you invoke bean.methodCall() and if methodCall() only return "hello, world" String, will the container try to wrap a transaction around it? - Long HDi
What difference will it make if your database doesn't support transactions? - cassiomolin
Have a look at Java EE 7 Samples on GitHub. This example uses EJB and MongDB. - cassiomolin
well, that example doesn't prove anything. Of course it works but that doesn't answer my question. What I want is disabling EJB transaction so that the EJB container doesn't have to "check" for transaction and manage transaction. That will make the bean lighter. In the link above, the container still try to wrap the method with transaction even though the transaction is useless but it's still there. - Long HDi

2 Answers

9
votes

In an EJB 3.0 container, annotate your EJB (or EJB method) with:

@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
@TransactionAttribute(value=TransactionAttributeType.NEVER)
public class YourBean

for BEAN management. For CONTAINER management instead:

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(value=TransactionAttributeType.NEVER)
public class YourBean
1
votes

The default value is managed by the container but if you dont specify nothing to do i think you solve your problem.

Or annotate all the Ejb to don´t support transaction

@Stateless
@TransactionManagement(TransactionManagementType.NEVER)
public class YourBean

Remember that the ejb transactions are executed in a hierarchical way, ie if the first method being invoked does not support methods "children methods" are handled in the same way