user389913
user389913

Reputation: 41

postgres multiple JDBC select statements in batch

How do you issue a single JDBC call to Postgres which includes 2 select statements? The two select statements select from different sets of tables and return two different result sets - so cannot be union-ed. The goal is to ensure that what is read from the database is a consistent snapshot of data in the tables at the instant the selects are issues. If two separate select statements are issued, there is a chance of some data getting updated after the first select and before the second one.

Look like java.sql.Statement's addBatch() and executeBatch() can not be used - this can only be used for updates. Attempting to use it with selects causes a PSQLException with message "A result was returned when none was expected." on executeBatch().

Upvotes: 1

Views: 1592

Answers (1)

Craig Ringer
Craig Ringer

Reputation: 324751

You don't need to jump through any hoops for this. Just BEGIN a transaction in SERIALIZABLE isolation and the two statements will see a consistent view of the data.

See the JDBC tutorial.

There is no other way to ensure two statements see the same data. You could send them together, but unless they're wrapped in a transaction they could still see different data.

Upvotes: 3

Related Questions