This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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
Author

Database Updates in EJB method

Tmmet Johnson
Ranch Hand

Joined: Nov 03, 2004
Posts: 56
Hi,
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
Rahul,

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: http://aspose.com/file-tools
 
subject: Database Updates in EJB method
 
Similar Threads
MVC modify model from outside of the table.
long post IBM.158
Trasactions in EJB
Action Class or Regular Java Class to use?
287 Pre-assessment test questions - Please answer