做了一个 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; }