日期:2014-05-16  浏览次数:20551 次

公司旧框架tmd 导致tomcat访问不了 连接池被占用了 JdbcProvider
错误代码如下:
try {
			conn=JdbcProvider.getInstance().getConnection();
			sm=conn.prepareStatement(sql);//办事指南
			rs=sm.executeQuery();
			while(isBSZLExsist && rs.next()){
				Map<String,String> map=new HashMap<String, String>();
//				map.put("id", rs.getString("id")); 
				map.put("title", rs.getString("title"));
				map.put("docpuburl",rs.getString("docpuburl"));
				map.put("docreltime", rs.getString("docreltime"));
				list3.add(map);
			}
			System.out.println("办事指南=="+list3.size());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
                   //这边有问题  没有释放数据库资源
		if(isExsist || isContentExsist){
		try {
			sm=conn.prepareStatement(sql1);//信箱
			rs1=sm.executeQuery();
			int count = 0;
			while(isExsist && rs1.next()){
				Map<String,String> map=new HashMap<String, String>();
				map.put("id", rs1.getString("id"));
				map.put("title", rs1.getString("title"));
				map.put("docpuburl",rs1.getString("docpuburl"));
				map.put("createdate", rs1.getString("createdate"));
				list4.add(map);
				count ++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}



正确代码如下:

try {
			conn=JdbcProvider.getInstance().getConnection();
			sm=conn.prepareStatement(sql);//办事指南
			rs=sm.executeQuery();
			while(isBSZLExsist && rs.next()){
				Map<String,String> map=new HashMap<String, String>();
//				map.put("id", rs.getString("id")); 
				map.put("title", rs.getString("title"));
				map.put("docpuburl",rs.getString("docpuburl"));
				map.put("docreltime", rs.getString("docreltime"));
				list3.add(map);
			}
			System.out.println("办事指南=="+list3.size());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{//正确代码
			if(conn!=null){//正确代码
				try {//正确代码
					conn.close();//正确代码
				} catch (SQLException e) {//正确代码
					e.printStackTrace();//正确代码
				}//正确代码
			}
		}
		if(isExsist || isContentExsist){
		try {
			conn=JdbcProvider.getInstance().getConnection();//正确代码
			sm=conn.prepareStatement(sql1);//信箱
			rs1=sm.executeQuery();
			int count = 0;
			while(isExsist && rs1.next()){
				Map<String,String> map=new HashMap<String, String>();
				map.put("id", rs1.getString("id"));
				map.put("title", rs1.getString("title"));
				map.put("docpuburl",rs1.getString("docpuburl"));
				map.put("createdate", rs1.getString("createdate"));
				list4.add(map);
				count ++;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}



总之 用完数据库资源 一定要释放 否则 旧框架就访问不了,切记