arjuncc
arjuncc

Reputation: 3287

Database may be already in use - H2 issue

Getting following exception at times while working with H2 database. If i change the path of database file to another location, this problem will be solved. Is there any permenant solution for this problem. My database is now 19.5MB.

org.h2.jdbc.JdbcSQLException: Database may be already in use: "/opt/new/mockdata_db.mv.db". Possible solutions: close all other connection(s
); use the server mode [90020-178]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:344) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.message.DbException.get(DbException.java:167) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.mvstore.db.MVTableEngine.init(MVTableEngine.java:104) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.Database.getPageStore(Database.java:2355) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.Database.open(Database.java:659) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.Database.openDatabase(Database.java:262) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.Database.<init>(Database.java:256) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.Engine.openSession(Engine.java:57) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.Engine.openSession(Engine.java:164) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.Engine.createSessionAndValidate(Engine.java:142) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.Engine.createSession(Engine.java:125) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.Engine.createSession(Engine.java:27) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:335) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:107) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:91) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.Driver.connect(Driver.java:74) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.jdbcx.JdbcDataSource.getJdbcConnection(JdbcDataSource.java:191) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.jdbcx.JdbcDataSource.getXAConnection(JdbcDataSource.java:354) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.jdbcx.JdbcDataSource.getPooledConnection(JdbcDataSource.java:386) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.jdbcx.JdbcConnectionPool.getConnectionNow(JdbcConnectionPool.java:228) ~[h2-1.4.178.jar:1.4.178]
        at org.h2.jdbcx.JdbcConnectionPool.getConnection(JdbcConnectionPool.java:200) ~[h2-1.4.178.jar:1.4.178]

Upvotes: 2

Views: 7799

Answers (1)

Christian MICHON
Christian MICHON

Reputation: 2180

One permanent solution is in the stack trace: use the server mode instead of the embedded mode. This would imply changing the JDBC url inside your code and use the tcp mode in JDBC.

An interesting alternative would be to open in automatic mixed mode: the first connection is embedded, and all subsequent connections are in tcp mode. This also changes the JDBC url, but seems more natural and I personally used this systematically with H2 for 1.3 and 1.4 versions.

Just append ";AUTO_SERVER=TRUE" to your previous JDBC url to use the automatic mixed mode.

Upvotes: 6

Related Questions