错误信息:
close
Already closed.
getrole
getrole
[Microsoft][ODBC SQL Server Driver]连接占线导致另一个 hstmt
[Microsoft][ODBC SQL Server Driver]连接占线导致另一个 hstmt
getrole
ResultSet is closed
close
Connection is closed.
departid1
rolename is
Invalid handle
ParserUtils: warning org.xml.sax.SAXParseException: URI was not reported to parser for entity [document]
java.sql.SQLException: ResultSet is closed
at sun.jdbc.odbc.JdbcOdbcResultSet.checkOpen(JdbcOdbcResultSet.java:6650)
at sun.jdbc.odbc.JdbcOdbcResultSet.next(JdbcOdbcResultSet.java:1251)
at org.apache.commons.dbcp.DelegatingResultSet.next(DelegatingResultSet.java:168)
at com.attribution.GetWorkTeamName.getWorkTeamName(GetWorkTeamName.java:30)
at org.apache.jsp.safetyworkor.setsafetyeronepoint_jsp._jspService(setsafetyeronepoint_jsp.java:402)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)tomcat连接池代码:
  <parameter>
        <name>maxActive</name>
        <value>0</value>
      </parameter>
      <parameter>
        <name>password</name>
        <value></value>
      </parameter>
      <parameter>
        <name>url</name>
        <value>jdbc:odbc:hbsafety</value>
      </parameter>
      <parameter>
        <name>driverClassName</name>
        <value>sun.jdbc.odbc.JdbcOdbcDriver</value>
      </parameter>
      <parameter>
        <name>maxIdle</name>
        <value>10</value>
java代码:getrole方法: public String getrole(String username){
String role_name="";
/*--------------------查找角色---------*/ String sqlstr="Select * from user_roles where user_name='"+username+"'";
this.init(Link_db.getCon());
try{
rs=state.executeQuery(sqlstr);
if(rs.next()){
            role_name =rs.getString("role_name") ;}   /* 角色*/
            
}catch(Exception e){
System.out.println("getrole");
System.out.println(e.getMessage());

}finally
{
close();
}
return role_name;
}
close方法:
public void close(){
if(con!=null){
try{ con.close();

}
catch(Exception e){
System.out.println("close");
System.out.println(e.getMessage());
}}
}jsp中用到的方法: String role_name=login.getrole(username);

解决方案 »

  1.   

    ResultSet有这样的特性:如果产生这个ResultSet的statement做了新的查询,或者被关闭,该ResultSet也会被关闭。所以,我觉得你要检查你的state是不是被多线程公用了?
      

  2.   

    我的state:该怎么改呀?两个state方法:用法不一样  是不是该给他加上synchronized同步设置呀?
    public   void  init(Connection con){
    try{
    state=Link_db.con.createStatement();
    }catch(Exception e){
    System.out.println("init");
    System.out.println(e.getMessage());
    }   public   Statement getStmtread(){
    try{
    con=getCon();
    stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    }catch(Exception e){
    System.out.println("getStmtread");
    System.out.println(e.getMessage());
    }
    return stmt;
    }
      

  3.   

    建议:两个方法中的statement都做成局部变量,不要使用类的成员变量,这样不好控制。
      

  4.   

    可是我有好几十个这样的方法,而且别人也在用这个statement方法。给点建议怎么写这两个statement好呀?
    public  void  init(Connection con){ 
    try{ 
    state=Link_db.con.createStatement(); 
    }catch(Exception e){ 
    System.out.println("init"); 
    System.out.println(e.getMessage()); 
    }   public  Statement getStmtread(){ 
    try{ 
    con=getCon(); 
    stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); 
    }catch(Exception e){ 
    System.out.println("getStmtread"); 
    System.out.println(e.getMessage()); 

    return stmt; 
    }在这里我先谢谢,jinxfei了!
      

  5.   

    第一个方法的返回值从void改成Statement,就跟第二个方法类似了,以第二个方法举例:
    public  Statement getStmtread(){
    try{
    con=getCon();
    //使用局部变量,类上不在定义stmt
    Statement myStmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
    }catch(Exception e){
    System.out.println("getStmtread");
    System.out.println(e.getMessage());
    }
    return myStat;

      

  6.   

    真是太麻烦你了,我按你的意思该了,可在反映速度上和不使用连接池一样了!为什么呀?
    public static synchronized Connection getCon(){
    try{
    Context context = new InitialContext();   
    //获得数据源   
          DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/sqlserver");  
           
    //获取连接   
        con = ds.getConnection();   }catch(Exception e){
    System.out.println("getCon");
    System.out.println(e.getMessage());
    }
    return con;
    }statement方法:
    public Statement init(Connection con){
    Statement state=null;
    try{ 
    state=Link_db.getCon().createStatement();
    }catch(Exception e){
    System.out.println("init");
    System.out.println(e.getMessage());
    }
    return state;
    }
    还应该怎么修改呀?谢了,我第一次使用连接池!
      

  7.   

    怎么说呢,要实现一个完善的连接池,也不是那么简单的,
    自己写的不稳定,可以使用c3p0,这是目前开源用的较多的。你的代码,如果想提高速度,可以尝试如下修改:Connection  conn=null;
    public static synchronized Connection getCon(){ 
    try{ 
    if (conn==null){
      Context context = new InitialContext();  
      //获得数据源  
          DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/sqlserver");  
          
      //获取连接  
        conn = ds.getConnection();    }catch(Exception e){ 
        System.out.println("getCon"); 
        System.out.println(e.getMessage()); 
      }

    return conn; 
    } statement方法: 
    public Statement init(Connection con){ 
    Statement state=null; 
    try{ 
    state=Link_db.getCon().createStatement(); 
    }catch(Exception e){ 
    System.out.println("init"); 
    System.out.println(e.getMessage()); 

    return state; 
      

  8.   

    首先谢谢大家了,我后来就是照大家的意思去改的,把statement变成局部变量,但是速度上慢了好多。后来我把getcon()的同步去掉了,出现下面的情况。
     在同一个页面中,有时间的查询,action提交到同一个网页。第一次打开网页不会出现错误,等查询的时候就会报结果集关闭,同一个网页,同样的方法,为什么查询的时候会出错呢,就是时间不一样了呀!
      

  9.   

    两个方法中的statement都做成局部变量,不要使用类的成员变量,这样不好控制。