Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLServerException: "The result set is closed" when using Streams as return type

When trying to stream results from Microsoft SQL Server 2014 I always get an SQLServerException: The result set is closed. Also tried with selectMethod=cursor in the JDBC URL to no avail. Is there something MSSQL specific to consider? Using Spring Boot 1.4.0.RELEASE, Spring Data JPA 1.10.2.RELEASE.

Sample repository interface:

package sample;

import sample.Contact;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Stream;

@Repository
public interface MyRepository extends JpaRepository<Contact, String> {

    Stream<Contact> findByContactid(String contactid);
}
like image 891
slauth Avatar asked Dec 06 '25 07:12

slauth


1 Answers

I've found that making the service method that is calling the repository be inside of a transaction gets rid of this error for me.

@Transactional(readOnly=true)
public List<Results> myServiceMethod() {
    return repo.findByContactid("12345").map(c->c.getName()).collect(toList());
}
like image 136
Ben L. Avatar answered Dec 07 '25 23:12

Ben L.