You can use ThreadLocal to store one Connection per thread. Something
like:
ThreadLocal<Connection> connectionHolder = new
ThreadLocal<Connection>();
public Connection getConnection() {
Connection c = connectionHolder.get();
if (c == null) {
c = (get a new connection for this thread from DriverManager
or wherever)
connectionHolder.put(c);
}
return c;
}
JDBC spec says nothing about ResultSet's thread safety (and neither
does H2's javadoc) so it hardly can be assumed to be thread safe. I'm
not expert on this, though, so if someone has real information on real-
world implementations' thread safety, it would be quite interesting.
Best Regards,
Joonas
On 21 huhti, 10:17, James Gregurich <[email protected]> wrote:
> So what do you do? Run a query on a connection, get a result set and then
> process that result set on multiple threads?
>
> On Apr 20, 2010, at 11:53 PM, Chris Schanck wrote:
>
> > The spec may not require it, but for Oracle, H2, Derby, Postgres (and I
> > think MySQL) Connection objects are thread safe as long as you do not try
> > to do "transaction interleaving", i.e., use one connection for multiple
> > transactions. In my case I want to run multiple queries in the same
> > transaction space. It doesn't by me any performance over serializing the
> > queries, but it buys me a ton of code clarity.
>
> > For my purposes, having tested for our supported backends (H2, Postgres,
> > Oracle), I'm comfortable with it.
>
> > Chris
>
> --
> You received this message because you are subscribed to the Google Groups "H2
> Database" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group
> athttp://groups.google.com/group/h2-database?hl=en.
--
You received this message because you are subscribed to the Google Groups "H2
Database" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/h2-database?hl=en.