Maurice
Maurice

Reputation: 6433

Java Servlets with Thread is blocking?

I'm trying to use my own form of connection pooling to handle database connections but it seems to be blocking for some reason even though I used a thread, can someone help me point out my mistake please.

This is the servlet code with the thread class.

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    // PrintWriter out = response.getWriter();
    OutputStream ostream = response.getOutputStream();
    PrintStream out = new PrintStream(ostream, true, "UTF8");

    try {

        response.setContentType("text/html");
        String test = request.getParameter("test");
        System.out.println("Received text: " + test);

        if(test.equals("free"))
        {
            for (int i = 0; i < list.size(); i++) {
                dbcm.freeConnection(list.get(i));
            }
            list.clear();
        }else
        {
            GetConnThread gct = new GetConnThread(test, dbcm);
            gct.start();
        }

    } catch (Exception e) {
        e.printStackTrace();
        out.println("fail");
    } finally {
        out.close();
    }

}



private class GetConnThread extends Thread
{
    private String test;
    private DBConnectionManager dbcm;

    public GetConnThread(String test, DBConnectionManager dbcm)
    {
        this.test = test;
        this.dbcm = dbcm;
    }

    public void run()
    {
        try {
            Connection conn = dbcm.getConnection(test);
            list.add(conn);             
            System.out.println(conn);
            System.out.println("list size: " + list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

This is the getConnection method in the DBConnectionManager

    public synchronized Connection getConnection(String test) throws CGFatalException {
        Connection con = null;
        boolean connectionIsValid = false;
        if (freeConnections.size() > 0) {

            con = (Connection) freeConnections.firstElement();
            freeConnections.removeElementAt(0);

            connectionIsValid = validateConnection(con);
            if (connectionIsValid == false) {
                con = getConnection(test);
            }
        } else if (maxConn == 0 || checkedOut < maxConn) {
            con = newConnection();
            connectionIsValid = validateConnection(con);
            if (connectionIsValid == false) {
                con = getConnection(test);
            }
        }else
        {
            System.out.println("No available connections for " + test + ", try again in 2 secs....");
            try {
                Thread.sleep(2000);
                con = getConnection(test);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (con != null && connectionIsValid == true) {
            checkedOut++;
        }
        System.out.println("checkedOut: " + checkedOut);
        System.out.println("maxConn: " + maxConn);
        return con;
    }

I set the max connections to 2 so after I call the servlet the 3rd time it goes to this line of code:

System.out.println("No available connections for " + test + ", try again in 2 secs....");

When I call it the 4th time, I'm expecting

System.out.println("No available connections for " + test + ", try again in 2 secs....");

to start as a seperate thread, but the 3rd call seems to be blocking it, the endless loop is expected because I was hoping to call "free" to clear the connections and everything goes back to normal.

Upvotes: 0

Views: 714

Answers (1)

Affe
Affe

Reputation: 47994

Your getConnection method is synchronized. Every other thread is going to block on that lock acquisition until the "third" request successfully gets a connection and proceeds.

Upvotes: 2

Related Questions