0

コード:

    Connection connection;
    String url, usernameDB, passwordDB;

    url = "...";
    usernameDB = "...";
    passwordDB = "..."; 

    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(url, usernameDB, passwordDB);
    Statement statement = connection.createStatement();

    queryResult = statement.executeQuery(query); 
    boolean moreRecords = queryResult.next();
    if(!moreRecords)
    {
        out.writeBoolean(false);
        out.flush();
    }
    else
    {
        int cols=0;
        out.writeBoolean(true);
        out.flush();
        out.reset(); // line 1
        cols = queryResult.getMetaData().getColumnCount();
        out.writeInt(cols);
        out.flush();
        out.reset(); 
        out.flush();
        out.reset();
        do
        {
            for(int i=1;i<=cols;i++)
            {
                out.writeObject(queryResult.getObject(i)); // line 2
                out.flush();
                out.reset();
            }
            out.writeBoolean(false);
            out.flush();
            out.reset();
        }
        while(queryResult.next());
    }

「out」は ObjectOutputStream です。

上記のコードの 1 行目に到達すると、queryResult オブジェクトがリセットされ、2 行目に到達すると例外が発生します。

「java.sql.SQLException: 結果セットの終了後」。

接続のタイムアウトを増やす方法を見つけようとしましたが、方法が見つかりませんでした。上記のコードの 1 行目に到達すると、データベースへの接続が失われ、queryResult オブジェクトが破壊されるようです。

これを解決する方法はありますか、または結果セットを (その値で) 複製する方法はありますか?

編集

このコードは tomcat 6 で実行されます。ServerSocket を開き、接続ごとに新しいスレッドを開始して、上記のコードを実行します....

4

1 に答える 1

0

いくつかの調査の後、解決策を見つけました。

オブジェクトを使用してCachedRowset、ResultSet からのコピーとそのすべての値を保持します。

これは変更後のコードです。

CachedRowSet cachedResults = new CachedRowSetImpl();

queryResult = statement.executeQuery(query);
cachedResults.populate(queryResult); // this is important!!
boolean moreRecords = cachedResults.next();
if(!moreRecords)
{
    out.writeBoolean(false);
    out.flush();
}
else
{
    int cols=0;
    out.writeBoolean(true);
    out.flush();
    cols = cachedResults.getMetaData().getColumnCount();
    out.writeInt(cols);
    out.flush();
    do
    {
        for(int i=1;i<=cols;i++)
        {
            out.writeObject(cachedResults.getObject(i));
            out.flush();
        }
                    out.writeBoolean(false);
        out.flush();
    }
    while(cachedResults.next());
}
于 2012-03-25T16:46:12.367 に答える