当前页面: 开发资料首页 → JSP 专题 → 求救!jsp网站发布问题
求救!jsp网站发布问题
摘要: 求救!jsp网站发布问题
小弟我是jsp初学者,试着做了个网站,采用的环境配置为JDK1.42,tomcat5.0和access数据库,近日做好了放到网上进行测试,机器为P43.0,内存2G的配置,一开始连上速度还可以,但多刷新几次或jsp页面多点几次就不行了,速度就会越来越慢,甚至会出现空指针的错误,实在不行了重启下tomcat就又没事了.
不知道这个问题怎么解决,恳请各位赐教!
我数据库连接没有采用连接池,不知道有没有关系?
麻烦各位高手把网站发布的一些经验或注意事项告知,不甚感激!
程序的问题
连接数据库次数多了 可能有没有关闭的连接或者是产生了异常
肯定是程序的问题了
数据库连接的问题喽
我一开始也认为可能是我编的程序有问题,但这几个页面只有几行简单的jsp代码,我检查了下,数据库连接也关闭了,却不知为何会出现这么严重的问题.不知道到有没有其他方面需要考虑的,比如说WEB容器配置等,谢谢指教啊!
我百分之九十肯定是你的连接没完全关闭造成的,很多人觉得自己的连接都关闭了,但其实不尽然,有时中间出了错,就会异致下面的close语句不执行。这种情况很常见,给你一个连接完全关闭的例子参考一下。
try
{
stmt=conn.createStatement();
try
{
rs=stmt.executeQuery(sql);
while(rs.next())
{
}
}
finally
{
rs.close();
rs=null;
}
}
finally
{
stmt.close();
stmt=null;
}
}
finally
{
conn.close();
conn=null;
}
谢谢terry_yip!我也一开始就想到了数据库关闭的问题,但我找来找去还是没发现关闭有什么问题.我的关闭数据库是写在一个javabean里面的,跟你提供的关闭方法大同小异,贴出来麻烦帮忙看看有没有问题?
public synchronized void close(){
try{
if(rs!=null){
rs.close(); rs=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(stmt!=null){
stmt.close(); stmt=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(con!=null){
con.close(); con=null; CON_COUNT--;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
}
在你的bean的其它代码中,例如
conn=DriverManager.getConnection(strConn,"root","8888");
stmt=conn.createStatement();
中加上try{}catch{}finally{}块,例如:
try
{
conn=DriverManager.getConnection(strConn,"root","8888");
stmt=conn.createStatement();
}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{
this.close();
}
恩 非常感谢terry_yip你的帮忙,我会去试试你的方法的,但我有点不解,忘请赐教.我的整个javabean是这样写的,希望能耐心的帮我看完,看看有什么问题,不甚感激!
package com.wasan.db;
import java.sql.*;
public class Conn {
private static Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
private static int CON_COUNT=0;
public static synchronized Connection getCon()throws Exception{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:ec","","");
CON_COUNT++;
return con;
}catch(SQLException e){
System.err.println(e.getMessage());
throw e;
}
}
public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
return stmt;
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public ResultSet getRs(String sql){
try{
stmt=getStmtread();
rs=stmt.executeQuery(sql);
return rs;
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
return stmt;
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public int getConcount(){
return CON_COUNT;
}
public PreparedStatement getPstmt(String sql){
try{
con=getCon();
pstmt=con.prepareStatement(sql);
return pstmt;
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public synchronized void close(){
try{
if(rs!=null){
rs.close(); rs=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(stmt!=null){
stmt.close(); stmt=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(con!=null){
con.close(); con=null; CON_COUNT--;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
然后我在页面中就加入以下jsp代码,其他都是html语言的,
<%
Conn con=new Conn();
ResultSet rs=con.getRs("SELECT * FROM notice ORDER BY issuetime DESC");
while(rs.next()){
String noticename=rs.getString(2);
String path=rs.getString(3);
}
con.close();
%>
非常非常感激!!!
JDBC-ODBC DRIVER只能支持一个并发连接
我把你的代码作了修改,作了1000次的循环操作,也没有出现问题。
package shuffle;
import java.sql.*;
public class Conn {
private Connection con;
private Statement stmt;
private ResultSet rs;
private PreparedStatement pstmt;
private static int CON_COUNT=0;
public synchronized Connection getCon()throws Exception{
try{
Class.forName("org.gjt.mm.mysql.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost/amtium","root","8888");
CON_COUNT++;
return con;
}catch(SQLException e){
close();
System.err.println(e.getMessage());
throw e;
}
}
public Statement getStmtread(){
try{
con=getCon();
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
return stmt;
}catch(Exception e){
close();
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public ResultSet getRs(String sql){
try{
stmt=getStmtread();
rs=stmt.executeQuery(sql);
return rs;
}catch(Exception e){
close();
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public Statement getStmt(){
try{
con=getCon();
stmt=con.createStatement();
return stmt;
}catch(Exception e){
close();
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public int getConcount(){
return CON_COUNT;
}
public PreparedStatement getPstmt(String sql){
try{
con=getCon();
pstmt=con.prepareStatement(sql);
return pstmt;
}catch(Exception e){
close();
System.err.println(e.getMessage());
e.printStackTrace();
}
return null;
}
public synchronized void close(){
try{
if(rs!=null){
rs.close(); rs=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(stmt!=null){
stmt.close(); stmt=null;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
try{
if(con!=null){
con.close(); con=null; CON_COUNT--;
}
}catch(Exception e){
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
如果你还是有问题的话,肯定像楼上所说的,你连接的方式不好,jdbc-odbc的效率是很低的,应该用jdbc来连,由于不知你是何种数据库,那条formName语句你自己上网找找。
还有,就算问题解决了,你这个bean也建议重写,原因是, 你每次操作数据库,都要人工的加上close,万一一个网站有几百个页面,出现了连接耗尽的情况,你就要每个每个页面地查看哪个没加close了,在bean中把它关闭,是最好的办法。再有,你得学习一下使用连接池。
好了,不能再当保姆了,没人可以代替你自己思考的。
很感谢terry_yip的多次帮助,也感谢xiangbo520给的意见
小弟我用的access数据库,刚学的缘故,我还第一次听说jdbc-odbc的效率是很低的,就知道老师上课跟我们就这么讲,我在网上查了下,似乎jdbc对access的支持不是很好,不知道我改怎样改用jdbc来连接呢?
terry_yip的意见很好,我也觉得每次在页面中关闭要很小心而且烦,我会重写我的bean的,谢谢!