H2 database error: Database may be already in use: “Locked by another process”

后端 未结 13 1110
时光取名叫无心
时光取名叫无心 2020-12-12 23:52

I am trying to use the H2 database from a Java application.

I created the database and its tables through the H2 Console and then I try to connect from Java using <

相关标签:
13条回答
  • 2020-12-13 00:20

    You can also delete file of the h2 file database and problem will disappear.

    jdbc:h2:~/dbname means that file h2 database with name db name will be created in the user home directory(~/ means user home directory, I hope you work on Linux).

    In my local machine its present in: /home/jack/dbname.mv.db I don't know why file has a name dbname.mv.db instead a dbname. May be its a h2 default settings. I remove this file:

    rm ~/dbname.mv.db 
    

    OR:

    cd ~/ 
    rm dbname.mv.db 
    

    Database dbname will be removed with all data. After new data base init all will be ok.

    0 讨论(0)
  • 2020-12-13 00:20

    I ran into similar problems running with ORMLite from a web application. I initially got stuck on the syntax to use server mode in the url. The answers above helped with that. Then I had the similar user/password error which was easier to figure out. I did not have to shut anything down or erase any files. The following code worked:

    protected ConnectionSource getConnectionSource() throws SQLException {
        String databaseUrl = "jdbc:h2:tcp://localhost/~/test";
        return new JdbcConnectionSource(databaseUrl,"sa","sa");
    }
    

    To use H2 in server mode on wildfly, I Modifed connection-url in standalone.xml

    <datasource jndi-name="java:jboss/datasources/ExampleDS" pool- name="ExampleDS" enabled="true" use-java-context="true">
         <connection-url>jdbc:h2:tcp://localhost/~/test</connection-url>
                   …
    </datasource>
    
    0 讨论(0)
  • 2020-12-13 00:22

    I had the same problem. in Intellj, when i want to use h2 database when my program was running i got the same error. For solve this problem i changed the connection url from

    spring.datasource.url=jdbc:h2:file:~/ipinbarbot
    

    to:

    spring.datasource.url=jdbc:h2:~/ipinbarbot;DB_CLOSE_ON_EXIT=FALSE;AUTO_SERVER=TRUE
    

    And then my problem gone away. now i can connect to "ipinbarbot" database when my program is. If you use Hibernate, also don't forget to have:

    spring.jpa.hibernate.ddl-auto = update
    

    goodluck

    0 讨论(0)
  • 2020-12-13 00:22

    I was facing this issue in eclipse . What I did was, killed the running java process from the task manager.

    It worked for me.

    0 讨论(0)
  • 2020-12-13 00:23

    Ran into a similar issue the solution for me was to run fuser -k 'filename.db' on the file that had a lock associated with it.

    Hope this helps!

    0 讨论(0)
  • 2020-12-13 00:24

    If you are running same app into multiple ports where app uses single database (h2), then add AUTO_SERVER=TRUE in the url as follows:

    jdbc:h2:file:C:/simple-commerce/price;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;AUTO_SERVER=TRUE
    
    0 讨论(0)
提交回复
热议问题