java - SQLite内存不足异常

标签 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 调用这 2 个方法,例如

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

然后代码就可以正常工作了。

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

最佳答案

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

这段代码:

CurrentActiveSom cas1=db.getCurrentActiveSom();

关闭连接,因此这段代码:

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

尝试从封闭数据库获取数据。如果您正在使用某种连接池,其中关闭连接会将其返回到池中,那么这是可以的。但您似乎正在使用单个物理连接。

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

关于java - SQLite内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12410778/

相关文章:

c# - SQLite递归查询获取树的第一个分支

java - 强制 Hibernate 或一般 JPA 供应商使用具有多个值的 INSERT INTO?

postgresql - Postgres 的 "\connect"命令的 JDBC 副本是什么?

mysql - 在 MySQL 触发器中计算后变量变为 NULL

java - 具有自定义 ArrayAdapter 和自定义对象的 ListView

java - 字符串变量插值 Java

php - 文件与数据库在聊天应用程序中的存储效率

java - 正则表达式匹配帮助

java - 无需 portlet 的 liferay 登录

java - 我在哪里安装 ubuntu 上的 jdbc 驱动程序?