SQLite内存不足异常

时间:2012-09-13 16:29:19

标签: java sqlite jdbc synchronized

我收到“java.sql.SQLException:[SQLITE_MISUSE]库使用不正确(内存不足)”。我粘贴了我的数据库连接对象的代码示例

public class DBhandler {
   private String DBUrl="d:\\sqlitedb\\somdb.db";
   private String driverName="org.sqlite.JDBC";
   private String jdbc="jdbc:sqlite:";
   private Connection con=null;
   private Statement stmnt=null;

  public DBhandler() 
  {
        try {
            Class.forName(this.driverName);
        } catch (ClassNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

            try {
                this.con=DriverManager.getConnection(this.jdbc+this.DBUrl);
                this.stmnt=con.createStatement();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

  }

   public CurrentActiveSom getCurrentActiveSom()
 {

     CurrentActiveSom cas=null;
     String query="select * from current_active_som where active=1"; 
    ResultSet rs=null;

     try {
        rs=this.stmnt.executeQuery(query);
    if (rs.next())
    {
        cas= new CurrentActiveSom();
        cas.setMonth(rs.getString("month"));
        cas.setYear(rs.getString("year"));
        cas.setIsActive(rs.getInt("active"));

    }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

     finally
     {
         try {
            rs.close();
            this.stmnt.close();
             this.con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

     }
     return cas;
 }

 public CurrentActiveSom getIsDoneSom(String month,String year)
 {

     CurrentActiveSom cas=null;
     String query="select * from current_active_som where month='"+month+"' and year='"+year+"' and active=0"; 

    ResultSet rs=null;


     try {

             rs=this.stmnt.executeQuery(query); //this is exception line
        }

        if (rs.next())
        {
            cas= new CurrentActiveSom();
            cas.setMonth(rs.getString("month"));
            cas.setYear(rs.getString("year"));
            cas.setIsActive(rs.getInt("active"));

        }


     } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }

     finally
     {
         try {
            //rs.close(); //if i uncomment this gets null pointer exception 
            this.stmnt.close();
             this.con.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

     }
     return cas;
 }

使用DBhandler的相同对象调用这两个方法,如

 DBhandler db=new DBhandler();
 CurrentActiveSom cas1=db.getCurrentActiveSom();
 CurrentActiveSom cas2=db.getIsDoneSom(String month,String year)

然后我得到了上述异常,

但是如果我们用不同的对象DBhandler调用这两个方法,比如

   DBhandler db1=new DBhandler();
   DBhandler db2=new DBhandler();
   CurrentActiveSom cas1=db1.getCurrentActiveSom();
   CurrentActiveSom cas2=db2.getIsDoneSom(String month,String year)

然后代码工作正常。

这是因为同步问题,连接?如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

好吧,“内存不足”错误看起来很奇怪,但是一个明确的错误在于每个程序运行(在构造函数中)创建Connection一次,然后在每个数据访问方法中关闭它。

此代码:

CurrentActiveSom cas1=db.getCurrentActiveSom();

关闭Connection,所以这段代码:

CurrentActiveSom cas2=db.getIsDoneSom(String month,String year)

尝试从已关闭的数据库中获取数据。如果您正在使用某种连接池,其中关闭连接将其返回到池,则可以。但似乎你正在开发一个物理连接。

所以只需在从DB获取数据后关闭它,而不是在每种数据访问方法中。

答案 1 :(得分:-1)

在调用`rs.next()之前关闭连接,以便ResultSet尝试从连接中读取,该连接已经关闭。

相关问题