File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Database Updates in EJB method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Database Updates in EJB method" Watch "Database Updates in EJB method" New topic

Database Updates in EJB method

Tmmet Johnson
Ranch Hand

Joined: Nov 03, 2004
Posts: 56
Can anyone help me out ?
I am using Stateless session bean. In my bean, I call BO class which performs business validations and then , call Data Access Object class
(say, class A) .The class , A updates Db2 database table A, then, calls Data Access Object class B which updates database table B and class A then calls Data Access Object C which updates tables C.All the above are performed in the same DAO class A's method.

TheEJB method transaction attribute has been set to Required.

My question is can I pass the same connection from class A to class B and C. Or,is it good to use separate connections (fetched from the connection pool) in class A,B and C ?
I know both ways will work. Can someone say me which will be faster and better than the other.
Thanks in advance.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
I will go with separate connections.Establishing a database connection is a costly affair.But we are avoiding this by making a connection pool.So after each method call you can close the connection, it will go back to the pool and in the second method you can again request the pool to get you a connection.

Other advantage might be like the code will be cleaner.

Rahul Bhattacharjee
LinkedIn - Blog
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79

But what is it that you will benefit out of passing separate connection objects. Just for code cleanliness, I dont think it is wise to pass different objects. Tho you will be implementing a connection pool, other connection objects are not going to be waiting for this particular bean to complete. They might be used y other threads. If the whole process is in a single transaction, then I think we should pass the same connection object, while the Required Trans-attribute is fine.
I agree. Here's the link:
subject: Database Updates in EJB method
It's not a secret anymore!