• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1011
  • Last Modified:

DBConnection Pool getConnection returns null

Hi,

I've reused the DBConnection Pool Manager class implemented by someone else. I tested it on my development machine and it works fine but once i rolled it out to production, sometimes i'm getting a null from the getConnection() method. This does not happen all the time, sometimes it works other times it fails. This makes troubleshooting task difficult as I can't identify in what scenario it may fail.

Here's a snippet of my codes where it fails :
--------------------------------------
 DBConnectionManager objConnMgr = null;
 objConnMgr = DBConnectionManager.getInstance();
 con = objConnMgr.getConnection("oracle");

* con returned is null.

Does anyone know what are the possible reasons of getting null in the con object?
0
osiruz
Asked:
osiruz
  • 8
  • 5
  • 5
  • +1
1 Solution
 
flumpmanCommented:
How many threads are accessing the DBConnectionManager class?  If multiple threads are accessing it then it could be a synchronization issue.

If you are using multiple threads, then try synchronizing the getConnection method and see if that works.

It could also be because of other reasons - perhaps the database is running out of connections?

--
flumpman
0
 
osiruzAuthor Commented:
How can i tell the number of threads accessing the DBConnection class? and how do i synchronize it if that's the problem? Pls. advise.

I'm just a beginner at this DBConnection pool thing, pls. excuse me if I am posting some questions which may sound silly. Thanks for the help.
0
 
flumpmanCommented:
What type of application are you developing?

If you are using the DBConnection pool class in a web application (servlets, JSPs), it is likely that multiple threads are accessing the class (a thread for each request from the browser).

If you are developing your application as a Swing/AWT or console application only one thread will be accessing the class unless you specifically create new threads.

For more information on threads and synchronization, check out the "Essential Classes" trail on the Java tutorial.

http://java.sun.com/docs/books/tutorial/

--
flumpman
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
osiruzAuthor Commented:
I'm using it for my web application (JSPs mainly and some servlets)
0
 
flumpmanCommented:
Ok, so it is going to be accessed by multiple threads at one time.

You need to check if the getConnection method is synchronized.  This would have the signature something like

public synchronized Connection getConnection(String name)

as opposed to

public Connection getConnection(String name)

The "synchronized" keyword means that a monitor (lock) on the object is required to call that method.  Only one thread can hold the monitor at one time, resulting in only one thread being able to access any of the synchronized methods of that object at one time.

If it isn't synchronized then you could change the signature and recompile to see if that has any effect.  If you don't have the source code then you could synchronize on the instance of the object by using a synchronized code block.

e.g.

DBConnectionManager objConnMgr = null;
objConnMgr = DBConnectionManager.getInstance();
synchronized (objConnMgr) {
    con = objConnMgr.getConnection("oracle");
    ...
}

I would really recommend that you read the Threads section of the Java Tutorial.  These concepts are fundamental to Java and especially relevant when developing web applications.

--
flumpman
0
 
osiruzAuthor Commented:
Thanks for the feedback.

I've checked codes, the getInstance() returns a synchronized DBConnectionManager.

===================================
static synchronized public DBConnectionManager getInstance() {
        if (objInstance == null) {
            objInstance = new DBConnectionManager();
        }
        intClients++;
        return objInstance;
    }
===================================

but the getConnection() method is not synchronized.

===================================
 public Connection getConnection(String name) {
        DBConnectionPool objPool = (DBConnectionPool) hshPools.get(name);
        if (objPool != null) {
            return objPool.getConnection();
        }        
        return null;
    }

===================================

Will this be the reason why it fails to get a connection sometimes? Do i need to change the getConnection() method to synchronized? Thanks again for your help.

0
 
flumpmanCommented:
The getInstance method doesn't return a "synchronized DBConnectionManager", it returns a DBConnectionManager from a synchronized method.

The problem could reside in the getConnection method in the DBConnectionPool class.  It depends on what is happening inside this method (and if it synchronized).  Again you could see if the getConnection method is synchronized or you could synchronize on the objPool object.

e.g.

public synchronized Connection getConnection()

or

synchronized (objpool) {
    return objPool.getConnection();
}

The first way is preferable as it ensures that any code calling the getConnection method acquires a lock first.  The second method will only work as long as the only way of getting a connection is through the getConnection(String) method of the DBConnectionManager class.

--
flumpman
0
 
girionisCommented:
 If you are running it with Servlets you do not need to synchronize the methods, only instance variables. Servelts are synchronized by default and every new request will create a new thread to handle uit. Each thread will have its own copy of the getConnection method. If you have instance variables synchronize them to avoid possible problems.

  Why not have a static method that returns a connection object?

public static Conenction getConnection()
{
...
...
}
0
 
flumpmanCommented:

The getConnection method is not in the servlet but in a singleton instance.  Therefore synchronization is necessary...

--
flumpman
0
 
objectsCommented:
Why does it need to be synchronized? I don't see any reason that it should.
And from the description of the problem, synchronising isn't going to make any difference.

I'd say your problem is that either you have created a connection pool named "oracle" or your connection pool is running out of available connections. Check that you set up your connection pool(s) correctly.
0
 
osiruzAuthor Commented:
i've checked the logs it seems like I'm able to get the objPool but the connection itself is null.

The log "Returning valid connection" is being logged, but i suspect the objPool.getConnection() itself is returning null.

Does anyone knows what might be the error when having a valid objPool but there's no connection available in the pool? Run out of connections maybe? If so, how do I check how many connections is left available in the pool?

Pls help.

------------------------------------------------
  public synchronized Connection getConnection(String name) {
        DBConnectionPool objPool = (DBConnectionPool) hshPools.get(name);
        if (objPool != null) {
            log("Returning valid connection");
            return objPool.getConnection();
        }
        else
        {
             log("getConnection(str) is null");
        }
        return null;
    }
0
 
objectsCommented:
As I mentioned above your pool may be empty and unable to create new connections. Whose connection pool implementation are you using? I'd recomend checking the documentation and configuration of your pooling.

0
 
osiruzAuthor Commented:
0
 
osiruzAuthor Commented:
now i'm getting this error instead,

java.sql.SQLException: Io exception: Broken pipe

does anyone knows, what could this be due to?
0
 
objectsCommented:
The javadoc for the method you are using states that it returns null if the maximum number of connections has already been reached.:

/**
* Returns an open connection. If no one is available, and the max
* number of connections has not been reached, a new connection is
* created.
*
* @param name The pool name as defined in the properties file
* @return Connection The connection or null
*/

There is a similiar method that waits until a connection is available, but may still return null if a connection does not become available within timelimit:

/**
* Returns an open connection. If no one is available, and the max
* number of connections has not been reached, a new connection is
* created. If the max number has been reached, waits until one
* is available or the specified time has elapsed.
*
* @param name The pool name as defined in the properties file
* @param time The number of milliseconds to wait
* @return Connection The connection or null
*/

You could also try increasing the max number of exceptions in the properties file.
0
 
objectsCommented:
> now i'm getting this error instead,

What did you change?

The error sounds like connection to the db maybe getting lost.
0
 
osiruzAuthor Commented:
I did not change anything. after i restart my webserver everything is back to normal again. I forsee that the first error ( getconnection() returns null ) may happen again after some tine.
0
 
objectsCommented:
I'm sure it will, once all your connections in your pool are used up.
Increase the max number of connections and ensure you are returning connections to the pool once finished with.
0
 
osiruzAuthor Commented:
Thanks for your help.

I'd replaced the entire DBConnection class instead. It is working fine now.

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now