内存模式下的H2数据库无法通过Console访问

发布于 2024-10-18 21:48:34 字数 2178 浏览 2 评论 0原文

我在 servlet 上下文启动时通过以下代码在 H2 数据库中创建一个内存数据库,

    void initDb()  {
    try {

        webserver = Server.createWebServer().start();

        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1","SA","");
        InputStream in = getClass().getResourceAsStream("script.sql");
        if (in == null) {
            System.out.println("Please add the file script.sql to the classpath, package " + getClass().getPackage().getName());
        } else {
            RunScript.execute(conn, new InputStreamReader(in));
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery("SELECT TO_CHAR(bday,'DD/MM/yyyy hh24:mi') FROM TEST2");
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
            rs.close();
            stat.close();
            conn.commit();
            conn.close();
        }




    //accessed using url jdbc:h2:tcp://localhost/mem:db1    
    try{ 

                CachedRowSet crs = new DBConnector().executeQuery("select * from  test2"); 
                while(crs.next()){ 
                System.out.println("ARGUMENT_NAME:"+crs.getString(1)); 
               // System.out.println(",DATA_TYPE:"+crs.getString("DATA_TYPE"));       
                } 
                crs.close(); 
        }catch(SQLException e){ 
            e.printStackTrace(); 
        } 

    } catch (Exception e) { //this exception gets throws connection failed!
        System.out.println("Exception initializing memory H2 database"+e);
    }
}

稍后我在同一个 JVM 中通过 url jdbc:h2:mem:db1 进行访问,这也有效。但是当我想通过 jdbc:h2:tcp://localhost/mem:db1 访问它时,它在同一个 JVM 或不同的 JVM 中都不起作用。

我实际上想以嵌入式模式运行系统并使用控制台查看内容。如果我在相同的 servlet 上下文启动方法中启动 Web 服务器,我可以看到控制台,但它仍然没有使用 url jdbc:h2:tcp://localhost/mem:db1.

如果我使用命令行启动服务器

java -cp "WebContent/WEB-INF/lib/h2-1.3.148.jar;hsqldb.jar;%H2DRIVERS%;%CLASSPATH%"  org.h2.tools.Console %*
and url as 'jdbc:h2:tcp://localhost/mem:db1'

然后尝试连接,令人惊讶的是它连接但没有数据。似乎它正在自己创建一个单独的服务器和一个不同的数据库。所以没有数据。

I am creating an in memory database in H2 database by the following code on servlet context startup

    void initDb()  {
    try {

        webserver = Server.createWebServer().start();

        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1","SA","");
        InputStream in = getClass().getResourceAsStream("script.sql");
        if (in == null) {
            System.out.println("Please add the file script.sql to the classpath, package " + getClass().getPackage().getName());
        } else {
            RunScript.execute(conn, new InputStreamReader(in));
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery("SELECT TO_CHAR(bday,'DD/MM/yyyy hh24:mi') FROM TEST2");
            while (rs.next()) {
                System.out.println(rs.getString(1));
            }
            rs.close();
            stat.close();
            conn.commit();
            conn.close();
        }




    //accessed using url jdbc:h2:tcp://localhost/mem:db1    
    try{ 

                CachedRowSet crs = new DBConnector().executeQuery("select * from  test2"); 
                while(crs.next()){ 
                System.out.println("ARGUMENT_NAME:"+crs.getString(1)); 
               // System.out.println(",DATA_TYPE:"+crs.getString("DATA_TYPE"));       
                } 
                crs.close(); 
        }catch(SQLException e){ 
            e.printStackTrace(); 
        } 

    } catch (Exception e) { //this exception gets throws connection failed!
        System.out.println("Exception initializing memory H2 database"+e);
    }
}

I am later on accessing by url jdbc:h2:mem:db1 in the same JVM, which is working too. But when i want to access it by jdbc:h2:tcp://localhost/mem:db1 it is not working either in the same JVM or in different JVM.

I actually want to run the system in embedded mode and see the contents using the console. If I start the webserver in the same servlet context startup method I am able to see the Console but it is still not connecting to the in memory DB with url jdbc:h2:tcp://localhost/mem:db1.

If I start the server using Command line using

java -cp "WebContent/WEB-INF/lib/h2-1.3.148.jar;hsqldb.jar;%H2DRIVERS%;%CLASSPATH%"  org.h2.tools.Console %*
and url as 'jdbc:h2:tcp://localhost/mem:db1'

And then try to connect, surprisingly it connects but with no data. Seems like it is creating a seperate server on its own and its a different db. So there is no data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卸妝后依然美 2024-10-25 21:48:34

要使内存数据库可供其他进程使用,您需要在打开数据库的同一进程中启动 TCP 服务器。例子:

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import org.h2.tools.Server;

public class TestMem {
    public static void main(String... args) throws Exception {

        // open the in-memory database within a VM
        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:mem:test");
        conn.createStatement().execute("create table test(id int)");

        // start a TCP server
        // (either before or after opening the database)
        Server server = Server.createTcpServer().start();

        // .. use in embedded mode ..

        // or use it from another process:
        System.out.println("Server started and connection is open.");
        System.out.println("URL: jdbc:h2:" + server.getURL() + "/mem:test");

        // now start the H2 Console here or in another process using
        // java org.h2.tools.Console -web -browser

        System.out.println("Press [Enter] to stop.");
        System.in.read();

        System.out.println("Stopping server and closing the connection");
        server.stop();
        conn.close();
    }
}

To make the in-memory database available for another process, you need to start a TCP server in the same process as the database was opened. Example:

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import org.h2.tools.Server;

public class TestMem {
    public static void main(String... args) throws Exception {

        // open the in-memory database within a VM
        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:mem:test");
        conn.createStatement().execute("create table test(id int)");

        // start a TCP server
        // (either before or after opening the database)
        Server server = Server.createTcpServer().start();

        // .. use in embedded mode ..

        // or use it from another process:
        System.out.println("Server started and connection is open.");
        System.out.println("URL: jdbc:h2:" + server.getURL() + "/mem:test");

        // now start the H2 Console here or in another process using
        // java org.h2.tools.Console -web -browser

        System.out.println("Press [Enter] to stop.");
        System.in.read();

        System.out.println("Stopping server and closing the connection");
        server.stop();
        conn.close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文