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