Container-Managed Transaction Type Attributes not working as expected
I am having a problem with the container-managed transactions not working as expected. I have 2 methods that work as follows:
MethodA{
for(a lot)
call MethodB;
}
@Transaction Type = RequiresNew
MethodB{
EntityManager Persist to database
}
I want the code in MethodB to be committed to the database when methodB returns. The problem is that I am running out of memory and MethodA is failing. When methodA fails after numerous calls to MethodB nothing is persisted to the database.
It is my understanding that when using requires new transactions that a new transaction is started for each call to the method and ends when the method returns while the calling method transaction is suspended.
How am I misunderstanding the requiresNew transaction attribute. What can I do to make a batch insert into my database that will not run out of memory (commit when a methodB returns)?
Thanks in advance.
[973 byte] By [
aacaina] at [2007-11-27 10:51:19]

# 1
The problem is that EJB invocation semantics for security, container-managed transactions, etc.
only apply when an invocation is made through an EJB reference. In your case, you are directly
invoking the implementation method from within the bean. The EJB container has no idea that's
happening. It's no different than invoking a utility method.
In order to get the behavior you'd like, you need to retrieve a reference to your own bean and invoke
through that.You can use SessionContext.getBusinessObject() to get the EJB reference for the
business interface through which the method in question is exposed.
--ken
ksaksa at 2007-7-29 11:30:26 >

# 3
You must not have a correct version of SessonContext.You can get to it here :
http://java.sun.com/javaee/5/docs/api/index.html
The easiest way to access SessionContext at runtime is to inject it into your bean as follows :
@Resource
private SessionContext sessionCtx;
ksaksa at 2007-7-29 11:30:26 >
