cancel
Showing results for 
Search instead for 
Did you mean: 

Stateless Session Bean - Container Transaction (BAPI via JRA)

Former Member
0 Kudos

Hi all,

We decided to use JRA for calling BAPI's in R3, because with JRA, the transaction management is taken care of.

My code for calling a bapi is working and the result of the bapi call is visible in R3 system (meaning a commit is done implicitly by the transaction)

Now i was trying the following to study the commit/rollback behavior, but its not behaving as i am expecting...: I have a stateless session bean (transaction type container), lets say myBean. For this bean i have two methods. methodA and methodB.


methodA()
{  // execute the posting code(call of bapi) successfully via JRA
   ...
   Interaction interaction = connection.createInteraction();
   output = (MappedRecord) interaction.execute(null, input);
   ...
   methodB(); // method B throws the exception
}

methodB()
{  throw new EJBException("Suppose something went wrong, i expect rollback now");
}

the attributes for both methods are "required".

However, when i call methodA() the result is committed every time. Shouldnt this be rollbacked because of the failure of methodB() ?

I even tried a myContext.setRollbackOnly(); at the end of methodA() (before calling methodB()) but the result was still commited to the R3 backend system...

I also tried to set the attribute for methodB() to "never".... I thought that i would receive an exception when i tried to call it from methodA() because methodB cant be called from within a transaction() but it was executed.

So i'm not even sure if my methods are called in a transaction or not...is there a way to check this?

I hope someone can explain me what i'm missing here

Kind regards,

J.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

The commit/rollback issue is solved. The bapi i was using had the parameter IN_UPDATE_TAST set tot -, not to X. It's rolling back when needed now.

However i'm still surprised by the exception that is NOT thrown when a method with attribute "never" is called from a method with attribute "required".

Kind regards,

J.

Vlado
Advisor
Advisor
0 Kudos

Hi Joren,

Are you actually calling methodB on the component interface or directly on the bean instance (i.e. this.methodB())? In the second case, the call cannot and is not intercepted by the container.

-Vladimir

Former Member
0 Kudos

Hello,

I am not sure what you mean with component interface, but since i'm calling methodB() from within methodA() i guess i am calling it on the bean instance.

So you can call any method from within another method of the bean without checking the attributes ( "never" vs required for example ) ?

so if i would do:

bean.methodA();

bean.methodB();

This should give an exception then?

[EDIT]

I tried this and the methods are executed fine without an exception... Documentation says the container throws an error if methodB() (attr never) will be executed in a trnasaction context. So my question is, what is an example that methodB() will throw the exception because its executed in a transaction context?

[/EDIT]

Kind regards,

J.

Message was edited by:

Joren Crauwels

Vlado
Advisor
Advisor
0 Kudos

Hi Joren,

With component interface I mean exactly what the spec means i.e. either the local or remote interface of the session bean. If you call the method directly on the bean instance - this is not a business method call and no transaction/security attributes are considered. And just think about it - how would the container know that you call another method from inside the bean instance?!

This is how the EJB component model works - all business method calls go thru the bean's interfaces. Here is the example:


private SessionContext mySessionContext;

public void setSessionContext(SessionContext ctx) {
    mySessionContext = ctx;
}

methodA() {
    ...
    MyComponentInterface comp = (MyComponentInterface) mySessionContext.getEJBObject();
    comp.methodB();
}

Hope this clarifies it a bit!

-Vladimir

Former Member
0 Kudos

Thx vladimir,

Sorry that i misunderstood your reference to component interface I am aware what this is

i finally saw my error.

I was calling methodA() on the local interface, but methodB() was just called within A without getting the localinterface through the sessioncontext.

These are probably basis EJB things, but i'm new to EJB's ans transactions.

Now i'm wondering, if i want to do the following:

LocalInterface local = localHome.create();

local.methodA();

local.methodB();

If i want methodB() to be called in the same transaction as method A, what is a good way to do it then? Can i for example make a business method that returns the session context and use that to invoke methodB? or is that right way to do it to make a business method methodC() in which i call methodA() and methodB() through the session context? Or some other obvious way that i'm missing here

Kind regards,

J.

Vlado
Advisor
Advisor
0 Kudos

Hi Joren,

> If i want methodB() to be called in the same

> transaction as method A, what is a good way to do it

> then? Can i for example make a business method that

> returns the session context and use that to invoke

> methodB? or is that right way to do it to make a

> business method methodC() in which i call methodA()

> and methodB() through the session context?

Yes, if all methodA, methodB, and methodC are declared with tx attribute Required, you'll get what you want

> Or some

> other obvious way that i'm missing here

Also you can start a transaction in the session bean client (servlet, another EJB with bean-managed tx) and call methodA and methodB in consequence with either Required, Supports, or Mandatory attributes sprecified for them.

HTH!

-Vladimir

Former Member
0 Kudos

Hello again ,

i'm still missing something here. I tried the following:

I made a business method getSessionContext() that returns the sessioncontext and used it in the following way:


BeanLocal beanLocal = beanLocalHome.create();

beanLocal.methodWithRequiredAttr();

BeanLocal myBeanLocal = (BeanLocal)beanLocal.getSessionContext().getEJBLocalObject();
myBeanLocal.methodWithNeverAttr();

this is executed fine without exception about the "never" attribute restrictions. Why are these methods not executed in one transaction?

Kind regards,

J.

Vlado
Advisor
Advisor
0 Kudos

OK, guess you'd better read a little bit more about tx attributes

Either in the <a href="http://java.sun.com/products/ejb/docs.html">specification</a> or in the <a href="http://java.sun.com/javaee/reference/tutorials/">tutorials</a>.

Cheers,

-Vladimir

Former Member
0 Kudos

Thx for the help and links. I'll take a look at it.

[EDIT]

After reading a bit, i think i asked a lot of stupid questions.

[EDIT]

Greetz,

J.

Message was edited by:

Joren Crauwels

Answers (0)