Inspect in memory hsqldb while debugging

后端 未结 8 2161
面向向阳花
面向向阳花 2020-12-12 13:18

We\'re using hdsqldb in memory to run junit tests which operate against a database. The db is setup before running each test via a spring configuration. All works fine. Now

相关标签:
8条回答
  • 2020-12-12 13:59

    The problem with the database-manager freezing can be resolved by starting the database-manager in a new thread, and sleeping the thread the test runs on.

    Add this code snippet to your test and you will be able to inspect the database while debugging. Remember to change the database-url to your database.

    Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
                        "--url",  "jdbc:hsqldb:mem://localhost:9001", "--noexit"
                });
            }
        });
    
    t.start();
    
    try {
        Thread.sleep(10000000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    0 讨论(0)
  • 2020-12-12 14:04

    You could also use the DatabaseManagerSwing class included in [HSQLDB][1] passing to it an open connection, that allows you to see the state of the database in the transaction the connection is in.

    DatabaseManagerSwing manager = new DatabaseManagerSwing();
    manager.main();
    manager.connect(connection);
    manager.start();
    
    0 讨论(0)
  • 2020-12-12 14:07

    In your unit test or in the @Before / setUp() method, you can add the following line to launch the HSQL Database Manager:

    
    org.hsqldb.util.DatabaseManager.main(new String[] {
      "--url",  "jdbc:hsqldb:mem:testdb", "--noexit"
    });
    

    or for the improved Swing version with "more refinements" (but about same functionality)

    
    org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
      "--url",  "jdbc:hsqldb:mem:testdb", "--noexit"
    });
    

    The DB manager lets you inspect your schema and run SQL queries on the live in-memory database while the application is running.

    Make sure to set a beakpoint or pause the execution one way or another if you want to check the state of your database at a specific line.

    0 讨论(0)
  • 2020-12-12 14:11

    The above accepted answer works perfectly only if the database name, username and password is untouched (testdb, SA, blank password).

    For custom database name, username and password you will get the following exception

    java.sql.SQLInvalidAuthroizationSpecException: invalid authorization specification - not found: SA

    Then, you have to connect manually.

    To connect directly to a non "default" DB use the following snippet

    org.hsqldb.util.DatabaseManager.main(new String[] {
      "--url",  "jdbc:hsqldb:mem:yourdbname", "--noexit",
      "--user", "dbusername", "--password", "dbpassword"
    });
    

    or for the improved Swing version

    org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
      "--url",  "jdbc:hsqldb:mem:yourdbname", "--noexit",
      "--user", "dbusername", "--password", "dbpassword"
    });
    

    Before executing the above snippet update the following

    • yourdbname - Update yourdbname with real database name
    • dbusername - Update dbusername with your database username
    • dbpassword - Update dbpassword with your database password
    0 讨论(0)
  • 2020-12-12 14:15

    Run your unit test to a breakpoint, then in the Debug perspective of Eclipse, open the Display view (Window, Show View, Display) and enter

        org.hsqldb.util.DatabaseManagerSwing.main(new String[] {
      "--url",  "jdbc:hsqldb:mem:testdb", "--noexit"
    });
    

    (as per dimdm's post), highlight it, right-click and choose Execute.

    0 讨论(0)
  • 2020-12-12 14:16

    This worked for me:

    DatabaseManager.threadedDBM();
    Thread.sleep(Long.MAX_VALUE);
    

    from https://stackoverflow.com/a/13732467/32453

    It's just the "non swing" variety DatabaseManager and prompts you for a JDBC connection string. The swing one doesn't have an equivalent method I don't think.

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