1

「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 の同じオブジェクトを使用したこれら 2 つのメソッドの呼び出し

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

次に、上記の例外が発生します。

しかし、これらの 2 つのメソッドを異なるオブジェクト DBhandler のように呼び出すと

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

その後、コードは正常に機能しています。

これは、接続による同期の問題によるものですか? この問題を解決するには?

4

2 に答える 2

1

「メモリ不足」エラーは奇妙に見えますが、決定的なエラーの 1 つは、Connectionプログラムの実行ごとに (コンストラクターで) 作成し、各データ アクセス メソッドでそれを閉じることにあります。

このコード:

CurrentActiveSom cas1=db.getCurrentActiveSom();

を閉じるConnectionため、次のコード:

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

閉じたデータベースからデータを取得しようとします。これは、接続を閉じると接続がプールに返される、ある種の接続プーリングを使用している場合は問題ありません。しかし、単一の物理接続で作業しているようです。

したがって、各データ アクセス メソッドではなく、DB からのデータの取得が完了したら、それを閉じてください。

于 2012-09-13T16:45:03.783 に答える
-1

`rs.next() を呼び出す前に接続を閉じるので、ResultSet はすでに閉じられている接続から読み取ろうとします。

于 2016-05-01T22:01:57.990 に答える