通过Spring以服务器模式启动H2数据库

发布于 2024-08-06 23:41:41 字数 780 浏览 14 评论 0原文

我正在尝试通过 Spring 以服务器模式启动 H2 数据库(我希望它在不同的进程中运行)。 目前我正在使用 java Runnable.exec 启动 h2 数据库(使用命令:“java -cp h2.jar org.h2.tools.Server”)

我知道有一种方法可以通过 Spring 来完成。我尝试将以下内容添加到 spring 配置中,但它不起作用(它没有启动 H2 数据库):

    <bean id="org.h2.tools.Server" class="org.h2.tools.Server"
        factory-method="createTcpServer" init-method="start" destroy-method="stop">
        <constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,8043" />
    </bean>

    <bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server"
        factory-method="createWebServer" init-method="start">
        <constructor-arg value="-web,-webAllowOthers,true,-webPort,8082" />
    </bean>

我将不胜感激任何帮助/想法

I'm trying to start the H2 database in server mode (I want it to run in a different process) via Spring.
Currently I'm using java Runnable.exec to start the h2 database (using the command: "java -cp h2.jar org.h2.tools.Server")

I know that there is a way to do it via Spring. I tried to add the following to the spring configuration, but it didn't work (it didn't start the H2 database):

    <bean id="org.h2.tools.Server" class="org.h2.tools.Server"
        factory-method="createTcpServer" init-method="start" destroy-method="stop">
        <constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,8043" />
    </bean>

    <bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server"
        factory-method="createWebServer" init-method="start">
        <constructor-arg value="-web,-webAllowOthers,true,-webPort,8082" />
    </bean>

I would appreciate any help/ideas

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

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

发布评论

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

评论(3

平生欢 2024-08-13 23:41:42

是否有: ?

<beans default-lazy-init="true" ...

你的 Spring 配置文件中

Do you happen to have:

<beans default-lazy-init="true" ...

in your Spring configuration files?

白日梦 2024-08-13 23:41:42

最近我必须做相同的配置来进行单元测试和检查数据,这对我有用(Spring 3.1.4)。然后,您只需连接 jdbc:h2:tcp://localhost:8043/mem:test 并确保在测试结束时添加 while(true){} 。

<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:mem:;TRACE_LEVEL_FIlE=4"/-->
    <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>
<bean class="org.h2.tools.Server" factory-method="createTcpServer" init-method="start" destroy-method="stop">
    <constructor-arg>
        <array>
            <value>-tcp</value>
            <value>-tcpAllowOthers</value>
            <value>-tcpPort</value>
            <value>8043</value>
        </array>
    </constructor-arg>
</bean>

Recently I had to do the same configuration to make unit test and check data, this works for me (Spring 3.1.4). Then you just have to connect with jdbc:h2:tcp://localhost:8043/mem:test and make sure to put a while(true){} at the end of your test.

<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:mem:;TRACE_LEVEL_FIlE=4"/-->
    <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1"/>
    <property name="username" value="sa"/>
    <property name="password" value=""/>
</bean>
<bean class="org.h2.tools.Server" factory-method="createTcpServer" init-method="start" destroy-method="stop">
    <constructor-arg>
        <array>
            <value>-tcp</value>
            <value>-tcpAllowOthers</value>
            <value>-tcpPort</value>
            <value>8043</value>
        </array>
    </constructor-arg>
</bean>
旧人 2024-08-13 23:41:42

您确定确实调用了Server类中的createTcpServer方法吗?您是否尝试过在那里设置断点?

H2 教程 声称如何以编程方式创建和启动服务器:

import org.h2.tools.Server;
...
// start the TCP Server
Server server = Server.createTcpServer(args).start();
...
// stop the TCP Server
server.stop();

您的 Spring 定义似乎模仿相同的初始化。但你总是可以尝试手动执行 - 也许这是 Spring 配置中的一些错误。

编辑:

我已经尝试过你的配置,它对我有用。是什么让您认为服务器未启动?它不会在标准输出上打印任何内容,但该进程会侦听 8043 端口。所以看起来还不错。

Are you sure the createTcpServer method in the Server class is really called? Have you tried setting up a breakpoint there?

H2 tutorial claims this how you can create and start the server programmatically:

import org.h2.tools.Server;
...
// start the TCP Server
Server server = Server.createTcpServer(args).start();
...
// stop the TCP Server
server.stop();

Your Spring definition seems to mimick the same initialization. But you can always try doing it manually - maybe it's some fault in Spring configuration.

EDIT:

I've tried your configuration and it works for me. What makes you think the server is not started? It doesn't print out anything on stdout, however the process listens at the 8043 port. So it seems pretty OK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文