简体   繁体   English

用servlet关闭数据库连接的问题

[英]problem with closing connection on db with servlet

I am trying to run my app with tomcat an the first time that I am debugging/running the app its work fine. 我第一次尝试使用tomcat运行我的应用程序时,我正在调试/运行该应用程序,但其工作正常。 but when I am trying to run on second time I am getting error "XJ040". 但是当我尝试第二次运行时,出现错误“ XJ040”。

      "Failed to start database 'C:\Documents and Settings\vitaly87\.netbeans-     derby\articals' with class loader WebappClassLoader
         context: /WebApplication1
     delegate: false
        repositories:

I thinks the problem because something wrong with closing the connections.Because when I stop my server the problem goes away until the second run. 我认为是问题所在,因为关闭连接时出现问题。由于我停止服务器后,问题才解决,直到第二次运行。

here the code: 这里的代码:

      private Connection connect = null;
//private Statement  stmt = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
     public  ArrayList<story> stories=new ArrayList<story>();
            void getStories(String  version) throws SQLException{

       try{

         Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

          }catch(ClassNotFoundException e){
              System.out.println(e);
          }
            connect = DriverManager.getConnection( "jdbc:derby:C:\\Documents and Settings\\vitaly87\\.netbeans-derby\\articals", "admin", "admin");
         // statement = connect.createStatement();
              int  ArticlesId= Integer.parseInt(version);
          preparedStatement = connect.prepareStatement("SELECT * FROM admin.articles  where    id>"+ArticlesId+"");
              resultSet = preparedStatement.executeQuery();
          while (resultSet.next()) {
    stories.add(new      story(resultSet.getString("title"),resultSet.getString("date"),resultSet.getString("text")));
}
            close();
            }
            //close connection
private void close() {
    try {
        if (resultSet != null) {
            resultSet.close();
        }



        if (connect != null) {
            connect.close();
        }
    } catch (Exception e) {

    }

thanks for helping 谢谢你的帮助

Best always close connections in a finally 最好总是在最后关闭连接

connect = DriverManager.getConnection(...)

try
{
    // use connection
}
finally
{
    try
    {
        connect.close()
    }
    catch (SQLException e)
    {
        // log e
    }
}

In your code the connection will not be closed if you have an exception for example in parseInt(version) or exequteQuery() 在您的代码中,如果您在parseInt(version)或exequteQuery()中有异常,则不会关闭连接

also in your case I don't think closing of result set is necessary because you are closing the connection anyway. 同样在您的情况下,我认为不必关闭结果集,因为无论如何您都在关闭连接。

try {
    if (resultSet != null) {
        resultSet.close();
    }

    if (connect != null) {
        connect.close();
    }
} catch (Exception e) {

}

is problematic because 1. if resultSet.close() throws an exception the connection is never closed and 2. you won't see any exceptions thrown in this method. 这是有问题的,因为1.如果resultSet.close()引发异常,则连接永远不会关闭,并且2.该方法不会引发任何异常。 I'd recommend to at least log the catched exceptions. 我建议至少记录所捕获的异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM