question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hibernate and BlobStreams causing "The TDS protocol stream is not valid" exception

See original GitHub issue

Driver version or jar name

6.1.6.jre8-preview and higher

hibernate: 5.2.13.Final’

SQL Server version

MSSQL Server 2012

Client operating system

MacOs and Windows

Java/JVM version

Java 1.8.0 r144

Problem description

It is not possible to get mssql-jdbc, hibernate and blob streaming to work.

We store and retrieve large data (mostly images) from the database. Loading the data completely into memory will cause a Out of Memory Exception.

Using Blob and getBinaryStream will the cause Exception “The TDS protocol stream is not valid”. This behavior is reproducible since 6.1.6.jre8-preview. The version 6.1.5.jre8-preview works, but the blob is hold completely into memory causing the mentioned OoM-Exceptions.

I think the issue is related to: https://github.com/Microsoft/mssql-jdbc/issues/567 and https://github.com/Microsoft/mssql-jdbc/issues/16

As far as I understand, a workaround would be not to close the resultset. However, these are handled by hibernate.

Expected behavior and actual behavior

I would assume, that I can read the blob stream to the very end. Instead reading the stream will throws exception.

Repro code

We have an entity with just a blob and an id.

@Entity
@Table (
        name = "blobs"
)
@NamedQueries({
        @NamedQuery(name = "findById", query = "SELECT b FROM BlobEntity b WHERE b.id like :id"),
})

public class BlobEntity {
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;

    @NotNull
    @Column
    private Blob blob;

    public Blob getBlob(){
        return blob;
    }

    public BlobEntity setBlob(Blob blob) {
        this.blob = blob;
        return this;
    }

    public long getId() {
        return id;
    }
}

Now, retrieving the blob and writing the stream to file will cause the exception:

Transaction tx;

try (Session session = sessionFactory.openSession()) {
  tx = session.beginTransaction();

  BlobStoreEntity result = (BlobStoreEntity) session.getNamedQuery("findById")
                    .setParameter("id", 11).uniqueResult();

  tx.commit();
           
  Files.copy(
    result.getBlob().getBinaryStream(), 
    Paths.get("/tmp/somepath"));
} catch (Exception e) {
  e.printStackTrace(); // The TDS protocol stream is not valid" exception
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rene-yecommented, Feb 22, 2018

Hi @Kinchkun, we are aware of this issue and a fix is undergoing testing. The changes proposed may be breaking and we have decided to not release it until we are fully confident of its stability. If you would like to test the changes proposed, you can grab a copy here, and follow PR #595.

0reactions
Lonzakcommented, Aug 6, 2021

@cheenamalhotra I have a question: I don’t see a final release of 6.5. Did all the changes of 6.5.X went into version 7.0?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting TDS protocol stream is not valid issue
Hi,. I'm using Sql Server 12.0.4100.1 and driver mssql-jdbc-6.2.1.jre7. Below is my test code: public void blobRandD(){.
Read more >
SQLServerException: The TDS protocol stream is not valid.
Hello...Everybody I use Microsoft SQL Server 2005 JDBC Driver\sqljdbc_1.2 to connect to database. When I view one of my report I got these ......
Read more >
How to stream data to database BLOB using Hibernate (no in ...
The exact question is: how to avoid loading stream into memory when storing a blob in database using Hibernate, using direct streaming instead....
Read more >
Problem connecting to Repository ; The TDS protocol Stream ...
Error: The TDS protocol stream is not valid..] ... Anyway how I fixed the problem (not the root cause) was I followed note...
Read more >
Hibernate Community • View topic - The incoming tabular data ...
Post subject: The incoming tabular data stream (TDS) remote procedure call ... This exception is driving me crazy and I can't find the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found