h2数据库给出对象已经关闭错误

时间:2018-03-17 07:14:30

标签: java h2

我有一个应用程序可以频繁且几乎连续地访问我的H2数据库。我所做的是每个方法打开数据库对其进行更改并关闭它。我遇到的问题是,我收到消息org.h2.jdbc.JdbcSQLException: The object is already closed [90007-196]即使我已经打开了database。以下是我的代码

public boolean insertStadiumGates(ArrayList<StadiumGate> stadiumGates) {
    this.openDatabase();
    int id = 0;
    if (this.isOpen()) {
        try {
            for (StadiumGate stadiumGate : stadiumGates) {
                PreparedStatement preparedStatement = connect.prepareStatement("INSERT INTO " + STADIUM_GATES_TB
                        + " ( " + GATE_ID + ", "
                        + "  " + STADIUM_ID + ", "
                        + "  " + STADIUM_NAME + ", "
                        + "  " + GATE_NAME + ") "
                        + "VALUES (?, ?, ?, ?)",
                        Statement.RETURN_GENERATED_KEYS);
                preparedStatement.setLong(1, stadiumGate.getGateId());
                preparedStatement.setLong(2, stadiumGate.getStadiumId());
                preparedStatement.setString(3, stadiumGate.getStadiumName());
                preparedStatement.setString(4, stadiumGate.getGateName());
                preparedStatement.executeUpdate();
                preparedStatement.closeOnCompletion();

            }

        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    this.closeDatabase();
    return id > 0;

}

public boolean insertStadiumTicketTypes(ArrayList<StadiumTicketType> stadiumTicketTypes) {
    this.openDatabase();
    int id = 0;
    if (this.isOpen()) {
        try {
            for (StadiumTicketType stadiumTicketType : stadiumTicketTypes) {
                PreparedStatement preparedStatement = connect.prepareStatement("INSERT INTO " + STADIUM_TICKET_TYPES_TB
                        + " ( " + TICKET_TYPE_ID + ", "
                        + "  " + STADIUM_ID + ", "
                        + "  " + TICKET_PRICE + ", "
                        + "  " + STADIUM_NAME + ", "
                        + "  " + TICKET_TYPE_NAME + ") "
                        + "VALUES (?, ?, ?, ?, ?)",
                        Statement.RETURN_GENERATED_KEYS);
                preparedStatement.setLong(1, stadiumTicketType.getTicketTypeId());
                preparedStatement.setLong(2, stadiumTicketType.getStadiumId());
                preparedStatement.setString(3, stadiumTicketType.getTicketPrice().toString());
                preparedStatement.setString(4, stadiumTicketType.getStadiumName());
                preparedStatement.setString(5, stadiumTicketType.getTicketTypeName());
                preparedStatement.executeUpdate();
                preparedStatement.closeOnCompletion();

            }

        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    this.closeDatabase();
    return id > 0;

}



   public void clearDatabaseTable(String table) {
    this.openDatabase();
    if (this.isOpen()) {
        try {
            PreparedStatement preparedStatement = connect.prepareStatement("DELETE FROM " + table,
                    Statement.RETURN_GENERATED_KEYS);
            preparedStatement.executeUpdate();
            preparedStatement.closeOnCompletion();


        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    this.closeDatabase();
}



private boolean isOpen(){
    boolean isOpen = false;
    try {
        isOpen = connect.isClosed();
    } catch (SQLException ex) {
        Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
    }
    return isOpen;
}


   private void openDatabase() {
    try {
        Class.forName("org.h2.Driver");
        connect = DriverManager.getConnection(DATABASE_NAME, USERNAME, PASSWORD);
        makeTables();
    } catch (ClassNotFoundException | SQLException e) {
        e.getMessage();
    }
}

private void closeDatabase() {
    try {
        connect.close();
    } catch (SQLException e) {
    }
}

我如何调用我的数据库方法

       private void downloadStadiumGates() {
       if(downloadListener != null)
    downloadListener.downloadStarted();
    Call<StadiumGatesResult> call = service.downloadStadiumGates();
    call.enqueue(new Callback<StadiumGatesResult>() {
        @Override
        public void onResponse(Call<StadiumGatesResult> call, Response<StadiumGatesResult> response) {
            StadiumGatesResult result = response.body();
            logger.error(TAG+" Download Response:"+response.toString());
            if (result != null) {
                    if(result.getStadiumGates() != null){
                        database.clearDatabaseTable(Database.STADIUM_GATES_TB);
                        database.insertStadiumGates(result.getStadiumGates());
                    }
                    if(downloadListener != null)
                downloadListener.downloadFinished();
            }else 
                if(downloadListener != null)
                downloadListener.downloadFailed();
           //Download ticket types
           downloadStadiumTicketTypes();

        }
        @Override
        public void onFailure(Call<StadiumGatesResult> call, Throwable t) {
               logger.error(TAG+" Download Response:", t);
               if(downloadListener != null)
               downloadListener.downloadFailed();
               //Download ticket types
               downloadStadiumTicketTypes();
        }
    });

} 


 private void downloadStadiumTicketTypes() {
       if(downloadListener != null)
    downloadListener.downloadStarted();
    Call<StadiumTicketTypesResult> call = service.downloadTicketTypes();
    call.enqueue(new Callback<StadiumTicketTypesResult>() {
        @Override
        public void onResponse(Call<StadiumTicketTypesResult> call, Response<StadiumTicketTypesResult> response) {
            StadiumTicketTypesResult result = response.body();
            logger.error(TAG+" Download Response:"+response.toString());
            if (result != null) {
                  if(result.getStadiumTicketTypes() != null){
                        database.clearDatabaseTable(Database.STADIUM_TICKET_TYPES_TB);
                        database.insertStadiumTicketTypes(result.getStadiumTicketTypes());
                }
                  if(downloadListener != null)
                downloadListener.downloadFinished();
            }else 
                if(downloadListener != null)
                downloadListener.downloadFailed();
           downloadSchedules();

        }
        @Override
        public void onFailure(Call<StadiumTicketTypesResult> call, Throwable t) {
               logger.error(TAG+" Download Response:", t);
               if(downloadListener != null)
               downloadListener.downloadFailed();
          downloadSchedules();
        }
    });

}

1 个答案:

答案 0 :(得分:0)

我确定你的方法踩在一起。含义。一个方法是在另一个方法打开时关闭数据库。试试这个来说明我的意思(你的日志记录当然是信息:-)):

   private void openDatabase() {
    try {
        Class.forName("org.h2.Driver");
        logger.info("opening database");
        connect = DriverManager.getConnection(DATABASE_NAME, USERNAME, PASSWORD);
        makeTables();
    } catch (ClassNotFoundException | SQLException e) {
        e.getMessage();
    }
   }

    private void closeDatabase() {
       try {
           logger.info("Closing database");
           connect.close();
       } catch (SQLException e) {
       }
    }