从Spring连接到HSQL DB

时间:2014-08-27 12:10:08

标签: spring hibernate jdbc hsqldb

我使用Maven插件启动HSQL DB(我在集成测试中使用DB):

<plugin>
    <groupId>com.btmatthews.maven.plugins.inmemdb</groupId>
    <artifactId>inmemdb-maven-plugin</artifactId>
    <version>1.4.3</version>
    <configuration>
        <monitorKey>inmemdb</monitorKey>
        <monitorPort>11527</monitorPort>
    </configuration>
    <executions>
        <execution>
            <id>run</id>
            <goals>
                <goal>run</goal>
            </goals>
            <phase>pre-integration-test</phase>
            <configuration>
                <daemon>true</daemon>
                <type>hsqldb</type>
                <database>MY_DB</database>
                <username>user1</username>
                <password>111</password>
            </configuration>
        </execution>
        <execution>
            <id>stop</id>
            <goals>
                <goal>stop</goal>
            </goals>
            <phase>post-integration-test</phase>
        </execution>
    </executions>
</plugin>

当数据库启动并运行时,我可以创建JDBC连接:

Class.forName("org.hsqldb.jdbcDriver");
Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost:9001/MY_DB", "sa", "");
System.out.print(connection);

此代码打印:org.hsqldb.jdbc.JDBCConnection@2d22efc3

但是,当我尝试使用相同的连接设置在Spring中配置数据源时,我得到一个异常(java.net.ConnectException: Connection refused):

<bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:hsql://localhost:9001/MY_DB" />
    <property name="username" value="sa" />
    <property name="password" value="" />
</bean>

我还尝试了usernameuser1password111,它产生了另一个例外:org.hsqldb.HsqlException: invalid authorization specification - not found: user1

如何从Spring连接到此数据库?

1 个答案:

答案 0 :(得分:1)

根据律师的建议,当你试图从Spring连接时,我补充说你的hsql db没有运行。