View content of H2 or HSQLDB in-memory database

后端 未结 11 1365
失恋的感觉
失恋的感觉 2020-12-02 05:27

Is there a way to browse the content of an H2 or an HSQLDB in-memory database for viewing? For example, during a debugging session with Hibernate in order to check when the

11条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 05:54

    You can run H2 web server within your application that will access the same in-memory database. You can also access the H2 running in server mode using any generic JDBC client like SquirrelSQL.

    UPDATE:

    Server webServer = Server.createWebServer("-web,-webAllowOthers,true,-webPort,8082").start();
    Server server = Server.createTcpServer("-tcp,-tcpAllowOthers,true,-tcpPort,9092").start();
    

    Now you can connect to your database via jdbc:h2:mem:foo_db URL within the same process or browse the foo_db database using localhost:8082. Remember to close both servers. See also: H2 database in memory mode cannot be accessed by Console.

    You can also use Spring:

    
        
    
    
        
    
    
    
        
        
    
    

    BTW you should only depend on assertions and not on manual peeking the database contents. Use this only for troubleshooting.

    N.B. if you use Spring test framework you won't see changes made by a running transaction and this transaction will be rolled back immediately after the test.

提交回复
热议问题