• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

Handling Transaction in StatelessSessionBean.

Hi Guyz ,,

I need a advice from you .

Is it a good practice to handle JDBC transactions in a StatelessSessionBean  ?

I have a SattelessSessionBean in which I create a JDBC connection object . abd close that connectionobject in ejbRemove.

In the bean i need to do some inserts and deletes using sql queries . all these operations should be in a transaction .
So if I do begin tran and commit tran on the connection will it be safe  ?

I remember ,  i read some where that ..StatelessSession beans will be reused b/w multiple clients . If that is the case then my transaction will not work . But I am not very sure abt this . Can any one please let me know abt this ?

Thanks for ur help

  • 3
1 Solution
use declarative transaction managment, if ur session ejb method for example called purchase, in ur ejb-jar.xml put something like:


hence all the Update operations using the datasource in this method will automatically be transaction managed
sorry correction
>>     <trans-attribute>Mandatory</trans-attribute>

to be

since the connection is getting closed in remove method, all the clients will be using the same connection object. so your SQL will not be having different transactions. all the clients will be using the same transaction which will not produce the results as you desire.

Probably you can just lookup you datasource in your setcontext method and get a different connection in your purchase method and close it before exiting the method then u can use the declarative transaction as petmagdy specified or you can commit/rollback programmatically.
raghava_dgAuthor Commented:
petmagdy  ,

I am getting the data source from the application servers connection pool .

i have for example 3 method

move .

in the deployment descriptor , for method insert and delete if i give <trans-attribute>Required</trans-attribute> .  This may be in a single transaction .

My plan is to call methods insert and delete from method move . I want even move should also be in transaction .
My Qtn is ..in this situation my transactions will be nested . that means a PARENT transaction is started in move method which will call insert and delete methods which has a Transaction .

  How this will be handled ?  Will parent transaction continue to insert and delete methods ?

nooo, required means, if no transaction yet creat new , if their is a transaction join

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now