db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Knut Anders Hatlen <knut.hat...@oracle.com>
Subject Re: concurrent execution of Statement's .addBatch() and .executeBatch()
Date Fri, 07 Dec 2012 12:01:01 GMT
Pavel Bortnovskiy <pbortnovskiy@jefferies.com> writes:

> Hello:
> Is it safe to call .addBatch() and .executeBatch() methods from
> multiple threads on the same PreparedStatement?
> Simple example: batching and executing a large number of the same
> statements (with the same PreparedStatement) by using ExecutorService.

Hi Pavel,

Both addBatch() and executeBatch() do their work in a block synchronized
on the connection, so in theory it should work to have many threads
adding batches to the same PreparedStatement. I don't think it has been
heavily tested, though.

And if the threads set any parameters on the PreparedStatement, your
application needs to synchronize the threads manually. For example, if
two threads call

  ps.setInt(1, id);
  ps.setObject(2, value);

on the same statement, you need to add synchronization to ensure that
the addBatch() call in one thread doesn't end up using one or more
parameter values set by the other thread.

Knut Anders

View raw message