开发者

JDBC error: End of stream was detected on a read

I have a problem accessing a database through a JDBC connection.

End of stream was detected on a read

The error occurs at several different points at random.

I appreciate any开发者_高级运维 help.


The root cause of this problem lies in the communication line between your Java code and the DB server in question. It can be everything. A bug in your JDBC code, a bug in the JDBC driver, a bug in NIC driver, a crappy NIC, a poor network cable, a stuttering DB server, a DB server dropping connections because it runs out of them, etcetera.

There's no straightforward cause, even not based on the stacktrace. I would start checking if the JDBC code is properly and robust written. I.e. acquiring and closing all DB resources in shortest possible scope in a try/finally block.

E.g.

public Entity find(Long id) throws SQLException {
    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    Entity entity = null;

    try {
        connection = database.getConnection();
        statement = connection.prepareStatement(SQL_FIND_BY_ID);
        statement.setLong(1, id);
        resultSet = statement.executeQuery();
        if (resultSet.next()) {
            entity = new Entity();
            entity.setSomething(resultSet.getObject("something"));
            // ...
        }
    } finally {
        close(resultSet);
        close(statement);
        close(connection);
    }

    return entity;
}

Next step would be upgrading the JDBC driver and then checking the hardware matters.

Good luck nailing down the cause of the problem.


In my case it was legacy com.microsoft.sqlserver.jdbc.SQLServerDriver

I realized this when saw this message in the Windows Event Logs on the SQL server machine: "Encryption is required to connect to this server but the client library does not support encryption; the connection has been closed. Please upgrade your client library."

Solution in my case was to disable "Force Encryption" in the "SQL Server configuration manager" / "Sql Server network configuration" / Right click on "Protocols for ..." / "Properties" / "Force Encryption" set to "No"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜