One connection for multiple threads

30 views
Skip to first unread message

Lukas Zapletal

unread,
Oct 16, 2008, 8:34:23 AM10/16/08
to H2 Database
Hi,

is it ok to reuse one connection with multiple threads? Is the H2
connection thread-safe? And could it be when I open the database in
the read-only mode?

I cannot find this in the documentation.

Thanks in advance.

LZ

Thomas Mueller

unread,
Oct 18, 2008, 5:14:37 AM10/18/08
to h2-da...@googlegroups.com
Hi,

> is it ok to reuse one connection with multiple threads? Is the H2
> connection thread-safe?

I will add the following paragraph to the Javadocs of Connection:

* Thread safety: This database is thread-safe, because it synchronizes access
* to the connection. However, for compatibility with other databases, a
* connection should only be used in one thread at any time.

> could it be when I open the database in
> the read-only mode?

No, because even a read-only database can have state: you can create
local temporary tables and variables (SET @X=10 and so on). But you
can use multiple connections in read-only mode of course.

What is your use case?

Regards,
Thomas

Lukas Zapletal

unread,
Oct 20, 2008, 3:19:44 AM10/20/08
to H2 Database
Thank you.

Use case: we fetch database into H2 from your central server and then
we do some reporting with this "snapshot". Many concurrent reads, no
writes. Thread count = no-CPUS + 1.

Before H2 we have used java serialization API. Pros: very very fast.
Cons: needs big heap size, loading from disc is slow (takes 99 % of
the overall processing time during simple unit tests).

With H2 we are able to process all data in the same time but with
constant heap size ;-) plus great flexibility. The only one problem is
much bigger snapshot data but its not big deal. We could even
implement compacting but we do not care now.

LZ

On 18 Říj, 11:14, "Thomas Mueller" <thomas.tom.muel...@gmail.com>
wrote:

GS

unread,
Dec 9, 2012, 3:43:36 PM12/9/12
to h2-da...@googlegroups.com
Hi, Thomas:

My understanding is that if I do not care about other databases, I could just use one single
connection object for multiple threads, correct?
Is it also true if I set MULTI_THREADED=1 ? 
If it is,  then do I really need the connection pool at all?

Thanks in advance.
Reply all
Reply to author
Forward
0 new messages