How to run H2 database in server mode?

前端 未结 5 359
谎友^
谎友^ 2020-12-02 11:46

How to start H2 database in server mode. I need to start it from my application.I tried the following code:

server = Server.createTcpServer().start();


        
相关标签:
5条回答
  • 2020-12-02 11:47

    I was getting this error when trying to start H2.
    See also http://h2database.com/javadoc/org/h2/tools/Server.html

    Exception in thread "main" org.h2.jdbc.JdbcSQLException: Feature not supported: "-~webAllowOthers" [50100-197]

    So I followed these steps:

    1. make dir mkdir h2db this directory will have your db files.
    2. Hit this command : java -cp bin/h2-1.4.197.jar org.h2.tools.Server -web -webAllowOthers -tcp -tcpAllowOthers -baseDir /home/manoj/dev/h2/h2db_6.0
      this command will start h2
    3. If you want to run h2 in backend then open vi h2.sh and paste this command in this: nohup java -cp bin/h2-1.4.197.jar org.h2.tools.Server -web -webAllowOthers -tcp -tcpAllowOthers -baseDir /home/manoj/dev/h2/h2db_6.0/ &
    4. Now run ./bin.h2.sh.
    0 讨论(0)
  • 2020-12-02 11:53

    You can use the following code to run H2 in server mode.

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
        <property name="driverClassName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:target/h2/ps;AUTO_SERVER=TRUE" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>
    

    You can use SQuirrel SQL client (http://squirrel-sql.sourceforge.net/) to connect to you H2 database and look at the tables.

    1. Create new connection.
    2. Select H2 in the driver dropdown menu
    3. Set url to your project target folder h2 folder (jdbc:h2:C:\projects\workspace\TestProject\target/h2/ps;AUTO_SERVER=true)
    4. Enter user name ("sa")
    5. Enter password ("")
    0 讨论(0)
  • 2020-12-02 12:01

    From command line,

    java -jar h2-1.3.160.jar -webAllowOthers -tcpAllowOthers
    

    this will launch an h2 database in server mode:

    Web Console server running at http://A.B.C.D:8082 (others can connect)
    TCP server running at tcp://A.B.C.D:9092 (others can connect)
    PG server running at pg://A.B.C.D:5435 (only local connections)
    

    open a browser to have an admin GUI

    0 讨论(0)
  • 2020-12-02 12:03

    As the exception message says, "Database may be already in use". You need to close all other connection(s), to make sure the database is not open in another process concurrently.

    By the way, don't use AUTO_SERVER=TRUE and the server mode at the same time. See the documentation for the automatic mixed mode. Use either one.

    I guess you are a bit confused about the different connection modes. I suggest to read the documentation about the connection modes, to make sure you understand it.

    0 讨论(0)
  • 2020-12-02 12:13

    Close all the applications that using H2 (web console, etc) Then add the AUTO_SERVER=TRUE to the end of the location in h2 console and also in java program (which you already have done)

    0 讨论(0)
提交回复
热议问题