tomcat连接池问题

用tomcat内置的连接池配置(maxActive=20),在jsp页面得到连接。头几次进页面没问题,之后奇慢无比,进不去页面,什么原因?请高手指点,不胜感激。(注:jsp页面内用session存连接,如果session==null重新建连接)。

把连接存在session里面不太好吧, 迟早吃光连接池.
连接应该是即时获取即时释放.
以前我也试过跨页面io使用Connection, 结果创造了tomcat一天重启5次的纪录.

通常做法是建立一个SERVERLET,负责所有的数据库访问.然后在每个会话过程中保存它的应用,通过它来实现数据库的访问和数据结果的封装,然后返回。

通常做法是写一个singleton类,把查找到的ds,保存在里面,通过它来实现数据库连接与释放,然后返回。

like this:



// 在此输入java代码
/*
* 数据源管理器,可以管理多个数据源
* 提高 JNDI 查找的速度
*/


package comm.ds;

import java.util.HashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DataSourceManager {

private static DataSourceManager instance;

private Map dataSources;
private Context context;

// This is private, and can't be instantiated directly
private DataSourceManager() throws NamingException {
dataSources = new HashMap();

// Get the context for caching purposes
context = new InitialContext();

/**
* In non-J2EE applications, you might need to load up
* a properties file and get this context manually. I've
* kept this simple for demonstration purposes.
*/

}

public static DataSourceManager getInstance() throws NamingException {
// Not completely thread-safe, but good enough
// (see note in article)
if (instance == null) {
instance = new DataSourceManager();
}
return instance;
}

public DataSource lookup(String jndiName)
throws NamingException {

// See if we already have this interface cached
DataSource ds = (DataSource) dataSources.get(jndiName);

// If not, look up with the supplied JNDI name
if (ds == null) {
ds =(DataSource) context.lookup(jndiName);

// If this is a new ref, save for caching purposes
dataSources.put(jndiName, ds);
}
return ds;
}
}

求教,按楼上所述方法,我在需要连接时应使用下面的代码:


Connection con = null;
con = DataSourceManager.getInstance().lookup("java:comp/env/jdbc/...").getConnection();
if ( null == con ) ...

可是,感觉在线程安全上是否有些问题?并发访问安全么?