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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.