RD_WF
RD_WF

Reputation: 115

Spring 4.1.6 JdbcTemplate Blocking and Synchronus?

I have an injected JDBCTemplate instance, and the code basically executes

private JdbcTemplate template;

public OutputType getOutput(InputType input) {
    CallType call = new CallType(input);
    CallbackType callback = new CallbackType(input);

    OutputType output = (OutputType) template.execute(call, callback);
    ...
}

I assume the execute method actually connects to the database and retrieves the result. However, I am having trouble finding out how the control flow works from the documentation.

Is the response from execute blocking (thread occupies a CPU core the entire time waiting for the database response)? Is it synchronous, but not blocking (i.e. thread sleeps/is not scheduled until the response is ready)? Is it asynchronous (execute returns immediately but output is incomplete/null, all database processing logic is in the callback)?

I have used several different databases so I am unsure of what actually happens in JdbcTemplate. If my terminology is incorrect please let me know. Thanks!

Upvotes: 2

Views: 2180

Answers (1)

Ben M
Ben M

Reputation: 1892

The JDBC protocol itself is synchronous and blocking - it will block on socket I/O awaiting the database response. While this doesn't mean you couldn't asynchronously call a JDBC provider (manually spawn a separate thread, use actors, etc.), the actual connection to the database will always be synchronous.

JDBCTemplate is also fully synchronous and blocking, there is no thread magic going on under the hood.

Upvotes: 3

Related Questions