项目报错:No operations allowed after connection closed

项目报错:No operations allowed after connection closed

今天写web项目时又曝出了以前没解决的错误,今天仔细想想终于把他理解了

错误原因

  1. 在servlet里,当我创建完一个Dao层对象,并且调用根据主键返回数据库的一个对象的方法时,调用了connection一次,调用完成后就将它close了。
  2. 但接着我在servlet里获得这个对象时,我又需要调用这个Dao层对象的另一个方法,即删除我调用的这个对象,而他又需要去调用一次connection,这时,connection已经在刚才的方法中被关闭了,所以报错。
  3. (请忽略我Dao层的删除逻辑是传入整个对象再进去寻找删除)

BaseDao部分代码

private Connection connection = null;

    public Connection getConnection() throws Exception{
        if (connection == null){
            Class.forName(JDBC_DRIVER);
            connection = DriverManager.getConnection(DB_URL,USER,PASSWORD);
        }
        return connection;
    }

    public void closeConnection() throws Exception{
        if (connection != null){
            connection.close();
        }
    }

SuperBillsDao层部分代码

(SuperBillsDao extend BaseDao)

根据主键返回id的方法:

	//根据主键选择bill
    public SuperBills selectById(int billid) throws Exception{
        SuperBills superBills = null;
        Connection connection = this.getConnection();
        //获取数据库执行对象
        String sql = "select * from superbills where billid=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,billid);
        //执行数据库语句
        ResultSet rs = ps.executeQuery();
        while (rs.next()){
            int id = rs.getInt(1);
            String name = rs.getString(2);
            String desc = rs.getString(3);
            int count = rs.getInt(4);
            BigDecimal price = rs.getBigDecimal(5);
            int ispayment = rs.getInt(6);
            int providerid = rs.getInt(7);
            String creator = rs.getString(8);
            String time = rs.getString(9);

            superBills = new SuperBills();
            superBills.setBillid(id);
            superBills.setProductname(name);
            superBills.setProductdesc(desc);
            superBills.setProductcount(count);
            superBills.setTotalprice(price);
            superBills.setIspayment(ispayment);
            superBills.setProviderid(providerid);
            superBills.setCreator(creator);
            superBills.setCreatetime(time);
        }
        rs.close();
        ps.close();
        closeConnection();
        return superBills;
    }

根据对象删除此对象的方法
(逻辑有问题,懒得改了)

    //删除bill
    public void delete(SuperBills superBills) throws Exception{
        Connection connection = this.getConnection();
        String sql = "delete from superbills where billid=?";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setInt(1,superBills.getBillid());
        ps.execute();
        ps.close();
        closeConnection();
    }

servlet代码

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(request.getParameter("billid"));
        SuperBillsDao superBillsDao = new SuperBillsDao();
        try {
            SuperBills superBills = superBillsDao.selectById(id);
            superBillsDao.delete(superBills);
            System.out.println("BillDelete is ok");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("doPost");
        response.sendRedirect("BillListServlet");
    }

问题(冲突)代码

SuperBills superBills = superBillsDao.selectById(id);
superBillsDao.delete(superBills);

解决方法

try {
      SuperBills superBills = superBillsDao.selectById(id);
      SuperBillsDao secondDao = new SuperBillsDao();
      secondDao.delete(superBills);
      System.out.println("BillDelete is ok");
} catch (Exception e) {
   e.printStackTrace();
}

根据主键查询完后,connection已经被关闭,无法再调用
所以这里再开一个Dao,用于删除操作,有些臃肿和低效率
但很省事儿

总结

错误原因

前一次调用完connection后close掉了,同一个Dao层对象无法再使用connection

目前的解决方法

再new一个Dao对象,调用另外的方法
(不过调一个方法就要新建一个对象属实憨憨)
希望各位带佬有更好的解决方法请告诉我和同样没搞懂的朋友

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值