JSF数据库连接池

时间:2014-01-02 01:08:23

标签: jsf tomcat connection-pooling

新年除夕。祝你新年快乐。我有一个数据库连接池 - 我认为这就是问题。当我点击此页面的链接“立即安排”时,一切正常。有一个在bean的构造函数中创建的数据库连接,因为我在这个bean中有几个读/写数据库的方法,所以我不想每次都打开一个新的连接。所以,我有一个方法共享的全局连接变量。只要我再次点击链接之前花时间,一切都很顺利。但是,如果我以快速间隔点击页面链接。 click.click.click.click,我收到错误org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法获取连接,池错误超时等待空闲对象。如何防止应用程序给我这个错误? (无论我通过单击链接请求页面的速度如何,每次都可以建立连接?)我试图更改content.xml;对于maxWait =“2000”,在content.xml中,我也尝试过maxWait =“ - 1”。任何有关教程的帮助或链接或任何有用的阅读将非常感激。

谢谢。

这是具有相关代码的bean。

@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;
}

}

这是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="*****"/>

这是我的JSF页面。

<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>

0 个答案:

没有答案