File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes EJB opening too many connections to DB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "EJB opening too many connections to DB" Watch "EJB opening too many connections to DB" New topic

EJB opening too many connections to DB

Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
I am a bit confused about EJBs opening connections to DB. I have 3 EJBs getting Connections from DataSource. EJB A gets a connection from Datasource, performs some DB operation and calls EJB B, EJB B does the same as EJB A and calls EJB C. When EJB C also accesses the DataSource to open connection, How many physical connections are opened to the DB or in otherwords How many active connections to DB am I using? Would it be 3 connections to DB, one for each EJB or only one connection is being used by the 3.
All EJBs have Txn attribute "Required".

Thanks in Advance.

Saulius Sinkunas

Joined: May 18, 2005
Posts: 20
I think you will have opened 3 physical DB connections. Because each time you call getConnection() you get new connection (connections may be pooled inside container).

I think it would be better close connection before calling another EJB as you can go into deadlock (if container has DB pool). For example, your pool size is 5 connections and you have 2 free DB connection. EJB A calls getConnection(), the EJB B call getConnection(). And when EJB C call getConnection(), this EJB locks locks as DB pool doesn't have any free DB connection (as they č ones are used for EJB A and B). And only some time (pool timeout) this EJB get DB connection error and returns with exception.

So better is to keep DB connection as shorter as possible - read all data, close connection and only then call EJB B if possible.
I agree. Here's the link:
subject: EJB opening too many connections to DB
It's not a secret anymore!