db connection issue


EJB programming & troubleshooting: db connection issue

  1. db connection issue (5 messages)

    I want to maintain a database connection as long as my entity bean is in the ready state. Thus I put the code that makes the connection into ejbActivate() and the code that closes the connection into ejbPassivate().

    But what about the home interface methods that need to access the database but are called when the bean is in the pool, before being activated.

    Somewhere I read that a database connection should not be made in setEntityContext. Is that right?

    If yes, I would need to open a database connection in every home interface method and close it when done. Right?


    Threaded Messages (5)

  2. db connection issue[ Go to top ]

    With connection pooling (which ought to be the only way you use to connect to a database from an EJB), you really shouldn't keep any particular physical database connection around longer than the life span of an entity bean method call. Here is the preferred way of getting connections:

    1. Declare an instance DataSource variable in the bean class
    2. Use JNDI to look up the datasource in the setEntityContext method
    3. Call DataSource.getConnection() in each method that required DB connection as late as you need it, and release the connection (call Connection.close()) as soon as you are done
  3. db connection issue[ Go to top ]


    Let say I create a class call DbConnectionManager that will get the DataSource through JNDI.
    Shall the class be normal Java Class, or a Singleton class or SessionBean or ??

    Thank You
  4. db connection issue[ Go to top ]

    If you have access to the datasource context, you should have access to a connection pool whenever you need it. Make sure you close the connection, but realize you are really not closing the connection but releasing the connection to the connection pool. Have you setup a datasource for a connection pool?

    Your on the right track with the DbConnectionManager. Just make it a class and you will be fine.

    Good luck,

  5. db connection issue[ Go to top ]

    One other thing, it is a good idea to not only cache your jndi lookups but cache your datasource context. This really makes a big difference with performance.
  6. db connection issue[ Go to top ]

    What do you mean by "DataSOurce Context"? Which exact Java interface or class were you referring to?