Handling Transaction in StatelessSessionBean.

Posted on 2004-11-04
Last Modified: 2010-03-31
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

Question by:raghava_dg
    LVL 13

    Expert Comment

    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
    LVL 13

    Accepted Solution

    sorry correction
    >>     <trans-attribute>Mandatory</trans-attribute>

    to be

    LVL 8

    Expert Comment

    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.

    Author Comment

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

    LVL 13

    Expert Comment

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

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now