我想用 ComboPooledDataSource这个类得到缓冲池的效果,但不在tomcat的server.xml去配置 但是通过一下的代码,当点到第10个连接(既缓冲池的最大连接数)时,打开网页速度慢的几乎为0,不知道是什么缘故,请大家告诉下。谢谢了我得程序代码如下:public class Database{
private static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static String user= "sa";
private static String pwd= "2339075";
private static String url="jdbc:microsoft:sqlserver://kind:1433;DatabaseName=ctsn";
static ComboPooledDataSource cpds=null;
public Database(){
init();
}
public static boolean testConnection()
{
Connection con = null;
try
{
con =getConnection();
return true;
// System.out.println("con is ok");
}
catch(Exception e){
System.out.println("connection is failed");
try{
cpds.close();
}catch(Exception ee){
}
return false;
}
}
public static void init(){
try{
if(cpds==null){
cpds = new ComboPooledDataSource();
cpds.setDriverClass(driver);
//loads the jdbc driver
cpds.setInitialPoolSize(5);
cpds.setJdbcUrl(url);
cpds.setUser(user);
cpds.setPassword(pwd);
cpds.setMinPoolSize(1);
cpds.setAcquireIncrement(1);
cpds.setMaxPoolSize(10);
cpds.setMaxIdleTime(180);
cpds.setMaxStatementsPerConnection(50);
// cpds.setCheckoutTimeout(180);
cpds.setMaxStatements(30);
cpds.setIdleConnectionTestPeriod(30);
}
}catch(Exception e){
e.printStackTrace();
try{
cpds.close();
}catch(Exception ce){
}
}
}
public static Connection getConnection(){
if(cpds==null)
init();
Connection connection=null;
try{
connection=cpds.getConnection();
return connection;
}catch(Exception e){
e.printStackTrace();
try{
cpds.close();
}catch(Exception ce){
}
}
return connection;
} public static void release(){
try{
if(cpds!=null)
cpds.close();
}catch(Exception e){
e.printStackTrace();
}
} public static synchronized void closeConnection(Connection con)
{
try
{
if(con != null)
con.close();
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
} public static void closeStatement(Statement smt)
{
try
{
if(smt != null)
smt.close();
smt = null;
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
} public static void closeResultSet(ResultSet rst)
{
try
{
if(rst != null)
rst.close();
rst = null;
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货