JSF数据库连接池
除夕快乐。我有一个数据库连接池——我想这就是问题所在。当我点击这个页面的链接时,Schedule Now,一切正常。在bean的构造函数中创建了一个数据库连接,因为我在这个bean中有几个方法可以读/写数据库,所以我不想每次都打开一个新的连接。因此,我有一个方法共享的全局连接变量。只要我在再次点击链接之前花点时间,一切都很好。但是,如果我快速单击指向页面的链接。click.click.click.click,我得到错误org.apache.tomcat.dbcp.dbcp.SQLNestedException:无法获得连接,池错误等待空闲对象超时。如何防止应用程序给我这个错误?无论我请求页面的速度有多快,如何通过单击链接使连接每次都可用?我已尝试更改content.xml;对于maxWait=2000,在content.xml中,我还尝试了maxWait=-1。任何帮助或链接到教程或任何有帮助的阅读都将不胜感激 多谢各位 下面是带有相关代码的beanJSF数据库连接池,jsf,tomcat,connection-pooling,Jsf,Tomcat,Connection Pooling,除夕快乐。我有一个数据库连接池——我想这就是问题所在。当我点击这个页面的链接时,Schedule Now,一切正常。在bean的构造函数中创建了一个数据库连接,因为我在这个bean中有几个方法可以读/写数据库,所以我不想每次都打开一个新的连接。因此,我有一个方法共享的全局连接变量。只要我在再次点击链接之前花点时间,一切都很好。但是,如果我快速单击指向页面的链接。click.click.click.click,我得到错误org.apache.tomcat.dbcp.dbcp.SQLNestedEx
@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>
环顾四周,我发现这个链接非常有用。