Multithreading and JdbcSQLException: Database is already closed

1,276 views
Skip to first unread message

Piero Molino

unread,
Jan 17, 2014, 3:42:20 PM1/17/14
to h2-da...@googlegroups.com
Hello,

I'm trying to add some rows to a database with multiple threads.
I create a connection, i assign it to each thread and then each thread does the inserts.
After a certain variable amount of time, everything stops working and i get the following error:

Grave: Error in the SQL
org.h2.jdbc.JdbcSQLException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-174]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:332)
at org.h2.message.DbException.get(DbException.java:172)
at org.h2.message.DbException.get(DbException.java:149)
at org.h2.message.DbException.get(DbException.java:138)
at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1413)
at org.h2.jdbc.JdbcConnection.checkClosed(JdbcConnection.java:1388)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:265
at com.myapp.YADatabaseManager.insertYArow(YADatabaseManager.java:307)

the YADatabaseManager.java:307 points to the creation of a prepared statement:
statement = connection.prepareStatement(queryString);

But i have never closed the database connection from my code, there's only one close line but it is runned after all the threads have finished.

I also tried to open a different connection for each thread, but the result is exactly the same. As the error occurs after several hours it's quite hard to reproduce. The last time i tried it broke after 2 hours when the db was 800mb approximatively.

The version of h2 is 1.3.174 and the jdbc connection string is:
jdbc:h2:/Users/w4nderlust/ya_db/2011/database/database

Any idea of what could be the cause and what can i do to solve it?
How can i give a better explaination? (considered that the dataset is huge and the process is really heavy?)

Thanks,
Piero

Noel Grandin

unread,
Jan 18, 2014, 3:28:34 AM1/18/14
to h2-da...@googlegroups.com
I suspect that some part of your code is calling system.exit() which will cause the database to shut down. 
--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to h2-database...@googlegroups.com.
To post to this group, send email to h2-da...@googlegroups.com.
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/groups/opt_out.

Piero Molino

unread,
Jan 18, 2014, 9:02:41 AM1/18/14
to h2-da...@googlegroups.com
Hi Noel,
thank you for your answer.
I checked and actually there's no System.exit() anywhere (other than the initial checking of command line parameters, but that point is already passed when the error occurs).
If i can give you more details for clarifying the problem, just tell me what should i do.
Piero
To unsubscribe from this group and stop receiving emails from it, send an email to h2-database+unsubscribe@googlegroups.com.

Noel Grandin

unread,
Jan 18, 2014, 10:23:00 AM1/18/14
to h2-da...@googlegroups.com
I'm sorry, but I'm pretty sure that either your code or one of the
libraries you use is the problem here.

There are only one of three ways that the DB gets shut down
(1) you shut down it down explicitly using the SHUTDOWN command
(2) Someone calls System.exit() in which case the exit handler we
install runs and shuts down the DB
(3) the DB gets corrupted, but then there should have been an earlier
stack trace.

What you can try is to add ";DB_CLOSE_ON_EXIT=FALSE" to your JDBC URL.
That will stop us from installing the exit handler.

Since you are spawning threads here, are you making sure that those
threads have completed (using the Thread#join()) method) before
shutting down your program?

Thomas Mueller

unread,
Jan 18, 2014, 10:35:11 AM1/18/14
to H2 Google Group
Hi,

Maybe there is an out-of-memory problem at some point? You should get an exception in your code, but you could also check in the <databaseName>.trace.db file. In such cases, the database engine closes the database, because out-of-memory can corrupt internal (in-memory) data structures of the database. H2 tries to ensure such corruptions don't go to disk and therefore closes the database.

Regards,
Thomas


--
You received this message because you are subscribed to the Google Groups "H2 Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to h2-database...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages