iteye_10600 2009-02-07 19:05
浏览 550
已采纳

一个Webservice 服务,多次重复调用其中某个方法,出现错误。

做了一个 xfire 的Webservice 服务,提供给FLEX 调用。

其中有个操作是重复多次调用某一个方法 getStaticEntiy()有时不定出现以下的一些错误:

 

                  java.sql.SQLException: Connection  is closed

 

                 java.sql.SQLException: Invalid state, the ResultSet object is closed.

 

 

但是,我把下面的这些方法放到web项目 里面,让jsp页面调用,获取数据,随便怎么频繁的刷新页面获取数据,都能正常获取,就是放到Webservice里面不正常。

获取数据库链接是,通过 TOMCAT连接池 来获得的。

下面是部分代码:

public byte[] getStaticEntiy(String pmname, int scalesize, float x, float y)
    {
        byte[] bContent = null;
        try
        {
            conn = DBPool.getPool().getConnection();    //获取连接池中的链接

            bContent = scaledao.getStaticEntiyData(pmname, scalesize, x, y, conn);  //这是从数据库读取数据的方法   
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(conn!=null){
                try
                {
                    conn.close();                   //释放链接
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        }
        
        return bContent;
    }

 

public byte[] getStaticEntiyData(String pmname, int scalesize, float x, float y,Connection conn)
    {
        byte[] bContent = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream(500000);
        IOdeal bs = new IOdeal();
        
        sql = "select s.descript from (staticentity s inner join scale s1 on s1.scaleid = s.scaleid) " +
            " inner join projectmap p on p.pmid = s1.pmid " +
            " where p.pmname=? and s1.scalesize=? and s.x=? and s.y=?";
        try
        {
            ps = conn.prepareStatement(sql);
            ps.setString(1, pmname);
            ps.setInt(2, scalesize);
            ps.setFloat(3, x);
            ps.setFloat(4, y);
            rs = ps.executeQuery();
            while(rs.next())
            {
                bs.writeFileStream(rs.getBinaryStream(1), out);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }       
        bContent = out.toByteArray();
        try
        {
            out.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return bContent;
    }
  • 写回答

1条回答 默认 最新

  • bohemia 2009-02-08 12:13
    关注

    ps 和 rs都及时关闭.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?