• 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

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

<assembly-descriptor>
   <container-transaction>
    <method>
    <ejb-name>MySessionBeanNme</ejb-name>
    <method-name>purchase</method-name>
    </method>
    <trans-attribute>Mandatory</trans-attribute>
  </container-transaction>
</assembly-descriptor>

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

to be

<trans-attribute>Required</trans-attribute>
0
 
kiranhkCommented:
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.
0
 
raghava_dgAuthor Commented:
petmagdy  ,

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

i have for example 3 method

insert
delete
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 ?

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

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