Hi Everybody ,
I my project I have written a BMP which deals with two tables in Oracle.
I am opening the connection to the database in the setEntityContext() method using it in all the database routines and then closing it in the unSetEntityContext() method.Its working fine.
But my PM says that its not effiecint to open the connection in setEntityContext() method, keeping it live and then closing it at the end in unSetEntityContext()
I have got large no. of database routine methods, will it affect performance or working of my BMP if I open & close the conn in every method .........How is it done normally .....what is the design convention for BMP for this
what the EJB specs says about this
If anybody has an idea please let me know
Thanks in Advance
Try creating a class to manage a pool of connection with methods to get and free connection from/back to the pool.
Thus, you are not actually opening and closing the DB connections (which are expensive).
If he's using bmp, the app server has that built-in.
Your App Server should provide a DataSource, accessible through the standard JNDI interface. This should provide you with the pooling you need.
I generally palm my DataAccess (DA) to another class, instantiated by the BMP Entity as required (this lets you then create factory method patterns to determine particular database implementions at runtime, if required).
Acquire the datasource in the constructor of the DA class or the setEntityContext method of your BMP:
InitialContext initCtx = new InitialContext();
DataSource dataSource = (DataSource) initCtx.lookup("jdbc/default");
Then, as you need the connection, get it from the DataSource (as in this occurs in the any method that reuires a connection):
Connection con = dataSource.getConnection();
Then close the connection when you finished with it, my understanding is that this returns it to the pool, rather than actually closing it.
"Then close the connection when you finished with it, my understanding is that this returns it to the pool, rather than actually closing it. "
I concur with Sridha G...regarding not creating connection in setEntityContext and instead creating and removing it in the same method call. Because removal of actual entity bean is upon the discretion of the container. if the container does not remove the bean for a while and the max connection pool creation has been reached and then new request for database connection has to wait until the entity bean holding the connections are removed or destroyed.
If you crate a conn in the entitycontext, that con is in use for lord knows how long. You should reserve a conn, use it, then return it asap. THat's what pooling is all about.
Your AppServer manages database connections in a pool anyway. When you are getting a connection and releasing it, all you are doing is getting the connection from the pool and returning it to the pool. So it's not an expensive operation.
For every connection we need to close the connection,other wise your connection pool will loose all the connection.So its not advisable to create connection in setentitycontext and close unsetentitycontext as it is called only after bean is removed .so better to close conn for every method call.
hey Rupesh Where are you?