1

I'm running a Glassfish 4.1 server with a H2 database in tcp/multi-user mode. I'm trying to programmatically update tables within a singleton bean. I always get the following exception:

org.h2.jdbc.JdbcSQLException: Timeout trying to lock table "GLOBALS"; SQL statement: ALTER TABLE "PUBLIC".GLOBALS ADD STARTFLAG VARCHAR(512) [50200-176]

I know that the table is locked, since one of the container-managed entity managers seems to have an open connection on that table. But at the time of the alter table statement, there would be no need for any connection...

Therefore I am wondering if there is any possibility to close all jdbc connections using container-managed JPA ?

Here's the initialization string I am using to start/connect to the H2 database:

jdbc:h2:tcp://localhost/~/datastore/database;AUTO_SERVER=TRUE;MVCC=TRUE 
  • I already tried to close the container-managed entity manager and the entity manager factory (but I think this is not the correct approach, since it's handled by the container) before the update statements are executed.

  • I tried to detach the managed objects before the update statements are executed.

  • I tried to add the MVCC=TRUE option (multi version concurrency).

  • Also tried to set FILE_LOCK=NO, but then I get another exception which tells me that this combination of options is not valid.

but without success so far...

Any ideas highly appreciated - thanks in advance!

salocinx
  • 3,715
  • 8
  • 61
  • 110

1 Answers1

1

Solution that comes to my mind is shutdowing server and reopen connection again.

If you are using java configuration you can use server.stop()

import org.h2.tools.Server;
...
// start the TCP Server
Server server = Server.createTcpServer(args).start();
...
// stop the TCP Server
server.stop();

If you're creating db via your URL. You can create a bat file and call it programatically from your application.

java org.h2.tools.Server -tcpShutdown tcp://localhost:9092

You can find more information from H2 Website

Ali Arda Orhan
  • 764
  • 2
  • 9
  • 24
  • Unfortunately I am using container-managed jpa on the glassfish application server. Therefore the container itself controls the life-cycle of the h2-server and jdbc connections. Thanks for the idea anyway. – salocinx Jan 09 '15 at 13:23