Discussions

EJB design: Database connection convention in BMP

  1. Database connection convention in BMP (9 messages)

    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

    Rupesh

    Threaded Messages (9)

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

    Kevin Leong
    www.zhanra.com
  3. Database connection convention in BMP[ Go to top ]

    If he's using bmp, the app server has that built-in.
  4. Database connection convention in BMP[ Go to top ]

    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.


  5. Database connection convention in BMP[ Go to top ]

    "Then close the connection when you finished with it, my understanding is that this returns it to the pool, rather than actually closing it. "

    That's correct.
  6. Database connection convention in BMP[ Go to top ]

    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.

    SG
  7. Database connection convention in BMP[ Go to top ]

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

    Pranab
  9. Database connection convention in BMP[ Go to top ]

    Hi,

      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.
  10. hey Rupesh Where are you?
    Remember me?