Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Private Methods in Stateless EJB  RSS feed

 
priya Anand
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I work in a system which interacts with SAP systems and use stateless session beans for data processing.
All my processing happens in one single method.
But this method calls private methods within the public method.
Also, I call a public method "print" which belongs to a plain java class inside my public method.
I see that my data gets mixed up in case of multiclients.
Usually it happens after the "print" method call but not necessarily.
Can somebody throw a light on how the transactions for private method work? Do they use the transaction of the calling method?
Is there a problem in calling a method of java class inside public method of EJB.
PS: This is a stateless EJB and no transaction attributes are defined for this.
Any help is appreciated.


Thanks,
Priya

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can somebody throw a light on how the transactions for private method work? Do they use the transaction of the calling method?

Yes.


Is there a problem in calling a method of java class inside public method of EJB.

No


PS: This is a stateless EJB and no transaction attributes are defined for this.

So you are not using transactions? Why are you asking abou them then?



I see that my data gets mixed up in case of multiclients.
Usually it happens after the "print" method call but not necessarily.

How do you store your data?
 
priya Anand
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think my question is a bit confusing.

I am not using transaction attributes so what is the default transaction for my business method in stateless EJB?
Will there be a seperate transaction for my private methods?
I have only one business method.
And my data is stored in map, which is initialized in ejbCreate method.

Thanks


 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am not using transaction attributes so what is the default transaction for my business method in stateless EJB?

No idea - the spec. doesn't specify this. Why are you not setting one?


Will there be a seperate transaction for my private methods?

No. Even if the container is deciding to provide default transaction behaviour all calls to other code from this method will be in a transaction.


And my data is stored in map, which is initialized in ejbCreate method.

There is your problem - nothing to do with transactions. Stateless Session beans need to be thread safe.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Sturrock wrote:

I am not using transaction attributes so what is the default transaction for my business method in stateless EJB?

No idea - the spec. doesn't specify this. Why are you not setting one?



Actually it depends on the version of the EJB spec. As per the EJB3 spec section 13.3.7:

By default, the value of the transaction attribute for a method of a bean with container-managed transaction demarcation is the REQUIRED


From what i remember, the earlier versions of the spec did not specify this.

 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priya,

You cannot store application state in an instance variable of a stateless session bean. If you need access to state across calls, you will need to use a stateful session bean. The default transaction type is REQUIRED, but this does not have to do with the problem you seem to have.

Regards,
Reza
 
priya Anand
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your replies.

I am using EJB 2.0.

Just to be on the safer side I will assign a Transaction attribute as REQUIRED.

But I still dont get how can I make my EJB, thread safe.

 
Reza Rahman
author
Ranch Hand
Posts: 580
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All of what was said above applies to EJB 2.x. You simply cannot use instance variables to store application state in a stateless session bean since bean instances are assigned randomly to clients. In fact, some containers do not do any pooling and EJB instances are created per call, such like the Spring prototype scope.

Hope it helps,
Reza
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!