做了一个WEB项目,其中有一个BEAN,里面全是静态方法,然后在页面中直接调用此BEAN的静态方法,每个静态方法都包括,连接数据库,查询(更新)数据语句,关闭连接,这样操作是否有问题。如果同时在线100人,都访问同一页面,就是说同时调用静态方法会不会出现问题。附上其中一个静态方法,请大家指点吧!
public static Vector getQgZxBj()
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
conn=ABean.getConn();
Vector vec=new Vector();
try{
stmt = conn.prepareStatement("select TOP 8 id,a2,a17,kh_name,kh_id from gq where sh_bs=? and lb=? and l1=? order by a17 desc,a26 desc");
stmt.setString(1,"Y");
stmt.setString(2,"求购");
stmt.setString(3,"保健食品");
rs =stmt.executeQuery();
while(rs.next())
{
QgForm gy=new QgForm();
gy.setId(rs.getInt("id"));
gy.setA2(rs.getString("a2"));
gy.setA17(rs.getString("a17"));
gy.setKh_name(rs.getString("Kh_name"));
gy.setKh_id(rs.getString("Kh_id"));
vec.add(gy);
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return vec;
}
public static Vector getQgZxBj()
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
conn=ABean.getConn();
Vector vec=new Vector();
try{
stmt = conn.prepareStatement("select TOP 8 id,a2,a17,kh_name,kh_id from gq where sh_bs=? and lb=? and l1=? order by a17 desc,a26 desc");
stmt.setString(1,"Y");
stmt.setString(2,"求购");
stmt.setString(3,"保健食品");
rs =stmt.executeQuery();
while(rs.next())
{
QgForm gy=new QgForm();
gy.setId(rs.getInt("id"));
gy.setA2(rs.getString("a2"));
gy.setA17(rs.getString("a17"));
gy.setKh_name(rs.getString("Kh_name"));
gy.setKh_id(rs.getString("Kh_id"));
vec.add(gy);
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return vec;
}
在线100人同时调用静态方法会不会出现问题
我们在把问题跨大点
在线1000人同时调用静态方法会不会出现问题你的静态方法涉及到数据库
所以就会有3个问题
1,web服务器的连接数
2,内存问题
3,数据库服务器的连接数问题问题1 可以查阅你是用的web容器的手册 设置配置文件问题2
100个人访问就会产生100个下列对象
Connection conn,PreparedStatement stmt,ResultSet rs 1000个人访问就会产生1000个下列对象
Connection conn,PreparedStatement stmt,ResultSet rs当堆积到一定程度会报内存不足的Error导致系统崩溃 问题3
数据库的连接数也可以设置,连接数大了也会影响系统所以才会有连接池技术的诞生接100分 不客气
1000个人访问,会产生1000个
Connection conn,PreparedStatement stmt,ResultSet rs 可是每次用完我都关了啊!
如果这里抛出异常了会出现什么问题?
stmt.close();
conn.close();
放在finally{}块中,才能保证资源被释放和关闭