简体   繁体   English

SQLite内存不足异常

[英]SQLite out of memory exception

I am getting the "java.sql.SQLException: [SQLITE_MISUSE] Library used incorrectly (out of memory)". 我得到“ java.sql.SQLException:[SQLITE_MISUSE]库使用不正确(内存不足)”。 I paste the code sample for my database connection object 我为数据库连接对象粘贴代码示例

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;
 }

The call to these Two methods with same object of DBhandler like 使用DBhandler的相同对象调用这两个方法

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

then I am getting the above exception , 然后我得到上述例外,

but if we call thses 2 methods with different object DBhandler like 但是如果我们用不同的对象DBhandler调用这些方法2

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

Then code is working fine. 然后代码工作正常。

Is this because of sync problem, with connection ? 这是因为连接有同步问题吗? how to resolve this problem? 如何解决这个问题?

Well, the "out of memory"error looks weird, but one definitive error lies in creating Connection once per program run (in the constructor) and then closing it in each data access method. 好吧,“内存不足”错误看起来很奇怪,但是一个明确的错误在于,每个程序运行一次(在构造函数中)创建一次Connection ,然后在每种数据访问方法中将其关闭。

This code: 这段代码:

CurrentActiveSom cas1=db.getCurrentActiveSom();

closes the Connection , so this code: 关闭Connection ,因此此代码:

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

tries to get data from a closed database. 尝试从关闭的数据库中获取数据。 This is OK if you are using some kind of connection pooling in which closing a connection returns it to the pool. 如果您使用某种连接池,在这种连接池中关闭连接会将其返回到池中,则可以。 But it seems you're working on a single physical connection. 但是似乎您正在处理单个物理连接。

So just close it after you're done getting data from DB, and not in each data access method. 因此,仅在从数据库获取数据之后关闭它,而不是在每种数据访问方法中都关闭它。

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

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

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