1

Happy New Year's Eve. I have a database connection pooling- well I think it's that- problem. When I click the link to this page, "Schedule Now", everything loads fine. There's a database connection that is created in the constructor of the bean because i have several methods in this bean that read/write to the database, so i dont want to open a new connection everytime. So, i have a global connection variable that the methods share. Everything works well as long as I take my time before a re-click on the link again. However if I click on the link to the page in quick intervals. click.click.click.click, I get the error org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object. How can I prevent the application from giving me this error? (how can I make a connection available everytime regardless of how quickly i request the page, by clicking the link?) I have attempted changing the content.xml; for maxWait="2000", in content.xml, i have also tried maxWait="-1". Any help or link to a tutorial or any reading that will help will greatly be appreciated.

Thank You.

Here is the bean with relevant code.

@ManagedBean(name = "scheduleAppBean")
@ViewScoped
public class ScheduleAppBean {
/* 
variables omitted
 */
//datasource to access database
@Resource(name="jdbc/mydb") private DataSource source;
private Connection   conn;

//constructor
public ScheduleAppBean(){
    job= new ArrayList<ScheduleAppHelper>();
    propertiesOptions= new ArrayList<SelectItem>();
    try {            
        Context ctx = (Context) new InitialContext();
          source = (DataSource) ((InitialContext) ctx).lookup("java:comp/env/jdbc/mydb");
       conn=source.getConnection();
    } catch (NamingException ex) {
        Logger.getLogger(SchedulesBean.class.getName()).log(Level.SEVERE, null, ex);
    }catch (SQLException ex) {
        Logger.getLogger(ScheduleBean.class.getName()).log(Level.SEVERE, null, ex);
    }
}

//called when page first loads to fill propertiesOptions list

public void loadProperties(){
   // clear any previous list data
    propertiesOptions.clear();
    try { 

       Connection conn=getDatabaseConnection();
       PreparedStatement query= conn.prepareStatement("select propName from property;");
       ResultSet result= query.executeQuery();
       // reset variable to zero in case some data is present
       propertiesNumber=0;
       //track current customer number
       int count=0;
       //retrieve all clients and add to list, update propertiesNumber
       while(result.next()){
           propertiesOptions.add(new SelectItem(result.getString("propName")));
           //increment variables
           propertiesNumber++;
           count++;
       }


    } catch(Exception ex){
            Logger.getLogger(ScheduleBean.class.getName()).log(Level.SEVERE, null, ex);
        }

}

private Connection getDatabaseConnection(){       
    if(conn!=null)
        return conn;
    else{
        try {            
            Context ctx = (Context) new InitialContext();
            source = (DataSource) ((InitialContext) ctx).lookup("java:comp/env/jdbc/mydb");
            conn=source.getConnection();
            return conn;
            } catch (NamingException ex) {
                Logger.getLogger(SchedulesBean.class.getName()).log(Level.SEVERE, null, ex);
            }catch (SQLException ex) {
                 Logger.getLogger(ScheduleBean.class.getName()).log(Level.SEVERE, null, ex);
            }catch(Exception ex){
                Logger.getLogger(ScheduleBean.class.getName()).log(Level.SEVERE, null, ex);
            }

    }
    return conn;
}

}

Here is the context.xml

<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" removeAbandoned="true"
 maxActive="20" maxIdle="10" maxWait="2000" name="jdbc/mydb" password="*****" 
type="javax.sql.DataSource"   url="jdbc:mysql://localhost:3306/******"username="*****"/>

Here is my JSF page.

<h:body onload="#{scheduleAppBean.loadProperties()}">
 <div class="scheduleNavigation">
            <ul>
                <li>
                    <h:form>
                        <h:commandLink value="Assign Schedule" styleClass="mainHeader" action="../administrator/assignschedule.xhtml">
                        </h:commandLink>
                    </h:form>
                </li>
                <li>
                    <h:form>
                         <!--This is the link I click repeatedly-->
                         <h:commandLink value="Schedule Now" styleClass="mainHeader" action="../administrator/appointments.xhtml">
                        </h:commandLink>
                    </h:form>
                </li>
                <li>
                    <h:form>
                        <h:commandLink value="My Schedule" styleClass="mainHeader" action="../administrator/myschedule.xhtml">
                        </h:commandLink>
                    </h:form>
                </li>
            </ul>
        </div>
</h:body>

Immer Alexis
  • 23
  • 1
  • 5
  • Looking around, I found this link to be of most help. http://stackoverflow.com/questions/2313197/jdbc-mysql-connection-pooling-practices. – Immer Alexis Jan 04 '14 at 14:18

0 Answers0