老话题:连接池

kellybs 2004-01-09 01:21:52
windows 2000 server +tomcat4.1.18+ms sqlserver 2000
很多人说要配置tomcat。但是我买的书,不是这样,他写了2个java文件。编译成class文件,然后在jsp页面中调用。究竟那种方法好。各有什么利弊。
我贴出2个java文件代码:
//================= ConnectionBean.java ===================
import java.io.Serializable;
import java.sql.*;
public class ConnectionBean implements java.io.Serializable
{
private Connection connection = null;
private boolean inuse = false;
public ConnectionBean()
{ }
public ConnectionBean(Connection value)
{ if (value!=null) connection = value; }
public Connection getConnection()
{ return connection; }
public void setConnection(Connection con)
{ connection = con; }
public void setInuse(boolean value)
{ inuse = value; }
public boolean getInuse()
{ return inuse; }
public boolean inUse()
{ return inuse; }
public void close()
{
try
{ connection.close(); }
catch (SQLException sqle)
{ System.err.println(sqle.getMessage()); }
}
}
...全文
17 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
kellybs 2004-01-09
  • 打赏
  • 举报
回复
不明白,没有说清楚
Leemaasn 2004-01-09
  • 打赏
  • 举报
回复
学习嘛,,,
当然要按照书上写的了。。。


具体应用,当然不能靠书本,

明白了?


yupingping 2004-01-09
  • 打赏
  • 举报
回复
我不知是在tomcat中设好,还是用java编好,也想问人
楼主买的是什么级别的书呀,要是一般的,那例子也会一般,这个应没在tomcat中设的好
kellybs 2004-01-09
  • 打赏
  • 举报
回复
//==================== PoolBean.java ======================
import java.io.Serializable;
import java.sql.*;
import java.util.*;
public class PoolBean implements java.io.Serializable
{
private String driver = null;
private String url = null;
private int size = 0;
private String username = new String("");
private String password = new String("");
private Vector pool = null;
public PoolBean() { }
public void setDriver(String value)
{ if (value!=null) driver=value; }
public String getDriver()
{ return driver; }
public void setURL(String value )
{ if (value!=null) url=value; }
public String getURL()
{ return url; }
public void setSize(int value)
{ if (value>1) size=value; }
public int getSize()
{ return size; }
public void setUsername(String value)
{ if (value!=null) username=value; }
public String getUserName()
{ return username; }
public void setPassword(String value)
{ if (value!=null) password=value; }
public String getPassword()
{ return password; }
private Connection createConnection() throws Exception
{
Connection con = null;
con = DriverManager.getConnection(url,username,password);
return con;
}
public synchronized void initializePool() throws Exception
{
if (driver==null)
throw new Exception("No Driver Name Specified!");
if (url==null)
throw new Exception("No URL Specified!");
if (size<1)
throw new Exception("Pool size is less than 1!");
try
{
Class.forName(driver);
for (int x=0; x<size; x++)
{
Connection con = createConnection();
if (con!=null)
{
ConnectionBean pcon = new ConnectionBean(con);
addConnection(pcon);
}
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
}
private void addConnection(ConnectionBean value)
{
if (pool==null) pool=new Vector(size);
pool.addElement(value);
}
public synchronized void releaseConnection(Connection con)
{
for (int x=0; x<pool.size(); x++)
{
ConnectionBean pcon =
(ConnectionBean)pool.elementAt(x);
if (pcon.getConnection()==con)
{
System.err.println("Releasing Connection " + x);
pcon.setInUse(false);
break;
}
}
}
public synchronized Connection getConnection()
throws Exception
{
ConnectionBean pcon = null;
for (int x=0; x<pool.size(); x++)
{
pcon = (ConnectionBean)pool.elementAt(x);
if (pcon.inUse()==false)
{
pcon.setInUse(true);
return pcon.getConnection();
}
}
try
{
Connection con = createConnection();
pcon = new ConnectionBean(con);
pcon.setInUse(true);
pool.addElement(pcon);
}
catch (Exception e)
{
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
return pcon.getConnection();
}
public synchronized void emptyPool()
{
for (int x=0; x<pool.size(); x++)
{
System.err.println("Closing JDBC Connection " + x);
ConnectionBean pcon =
(ConnectionBean)pool.elementAt(x);
if (pcon.inUse()==false)
pcon.close();
else
{
try
{
java.lang.Thread.sleep(30000);
pcon.close();
}
catch (InterruptedException ie)
{
System.err.println(ie.getMessage());
}
}
}
}
}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧