JdbcTemplate provides empty ResultSet for RowCallbackHandler but a non-empty one for ResultSetExtractor

33 Views Asked by At

I am trying to transfer some data from a MS SQL Server database to a Postgres database. My plan is to write a Spring Boot application that reads records from one datasource (mssql) and inserts them into the other datasource (postgres). Before getting into the weeds I wanted to check if my application can reach both the databases. Here is what I have so far:

@Service
public class ScenarioTransferService
{
    @Autowired private JdbcTemplate srcTemplate;
    @Autowired private JdbcTemplate dstTemplate;

    public void handle()
    {
        srcTemplate.query("select plant from plant", rs ->
        {
            while (rs.next())
            {
                System.out.println("source plant: " + rs.getString(1));
            }
        });

        dstTemplate.query("select plant from plant", new ResultSetExtractor<Void>()
        {
            @Override
            public Void extractData(ResultSet rs) throws SQLException, DataAccessException
            {
                while (rs.next())
                {
                    System.out.println("destination plant: " + rs.getString(1));
                }
                return null;
            }
        });

        dstTemplate.query("select plant from plant", new RowCallbackHandler()
        {
            @Override
            public void processRow(ResultSet rs) throws SQLException
            {
                while (rs.next())
                {
                    System.out.println("destination plant: " + rs.getString(1));
                }
            }
        });
    }
}

Notice that I am querying dstTemplate using both ResultSetExtractor and RowCallbackHandler. Each of these is supposed to give me access to the same underlying ResultSet but when I run this, I get output only from the ResultSetExtractor because the RowCallbackHandler gives me an empty ResultSet.

It is also worth noting that srcTemplate, which is connected to an MS SQL Server database, is also using RowCallbackHandler but as lambda and that works fine. It is just the postgres RowCallbackHandler that isn't working regardles of whether I use lambda or call it directly. Any clue what's going on?

If this were a database connection issue or if there was truly no data in the postgres database I would expect the ResultSetExtractor to give me the same results.

0

There are 0 best solutions below