【问题标题】:Tables and schema lost, hsqldb and maven sql plugin表和模式丢失,hsqldb 和 maven sql 插件
【发布时间】:2011-12-16 14:01:16
【问题描述】:

我正在使用 Spring (3.1.x)、JSF 2、JPA 2 (Hibernate Provider) 为 tomcat 6.x 开发 Web 应用程序。我想测试我的 DAO 类。 我的应用程序数据库在 MySql 下。 对于测试,我想在内存中使用 HSQLDB。

我在 hsqldb 下创建了一些创建模式和表的脚本,我用 maven sql 插件调用它们。

pom.xml:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>sql-maven-plugin</artifactId>
                <version>1.5</version>

                <dependencies>
          <!-- specify the dependent jdbc driver here -->
                    <dependency>
                        <groupId>hsqldb</groupId>
                        <artifactId>hsqldb</artifactId>
                        <version>1.8.0.10</version>
                    </dependency>
                </dependencies>

        <!-- common configuration shared by all executions -->
                <configuration>
                    <driver>org.hsqldb.jdbcDriver</driver>
                    <url>jdbc:hsqldb:mem:testOpen</url>
                    <username>sa</username>
                    <password></password>
          <!-- You can comment out username/password configurations and
               have maven to look them up in your settings.xml using ${settingsKey}
          -->
                    <settingsKey>sensibleKey</settingsKey>
          <!--all executions are ignored if -Dmaven.test.skip=true-->
                    <skip>${maven.test.skip}</skip>
                </configuration>

                <executions>
                    <execution>
                        <id>drop-db-before-test-if-any</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
              <!-- need another database to drop the targeted one -->
                            <url>jdbc:hsqldb:mem:testOpen</url>
                            <autocommit>true</autocommit>
                            <sqlCommand>DROP SCHEMA testOpen CASCADE</sqlCommand>
              <!-- ignore error when database is not avaiable -->
                            <onError>continue</onError>
                        </configuration>
                    </execution>

                    <execution>
                        <id>create-db</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <url>jdbc:hsqldb:mem:testOpen</url>
              <!-- no transaction -->
                            <autocommit>true</autocommit>
                            <sqlCommand>CREATE SCHEMA testOpen AUTHORIZATION DBA</sqlCommand>
                        </configuration>
                    </execution>

                    <execution>
                        <id>create-tables</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <url>jdbc:hsqldb:mem:testOpen</url>
                            <autocommit>true</autocommit>
                            <srcFiles>
                                <srcFile>conf/script_sql/hsqldb/create_tables.sql</srcFile>
                            </srcFiles>
                        </configuration>
                    </execution>

                    <execution>
                        <id>check-data</id>
                        <phase>process-test-resources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <url>jdbc:hsqldb:mem:testOpen</url>
                            <autocommit>true</autocommit>
                            <printResultSet>true</printResultSet>
                             <sqlCommand>SELECT * FROM INFORMATION_SCHEMA.SYSTEM_COLUMNS WHERE TABLE_NAME NOT LIKE 'SYSTEM_%'</sqlCommand>
                        </configuration>
                    </execution>

            </executions>
            </plugin>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
      http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="C4OpenPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>java:comp/env/jdbc/open_tomcat</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.transaction.flush_before_completion" value="true"/>
            <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
            <property name="hibernate.connection.zeroDateTimeBehavior" value="convertToNull"/>
        </properties>
    </persistence-unit>

    <persistence-unit name="C4OpenTestPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
            <property name="hibernate.connection.username" value="sa" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:testOpen" />
            <property name="hibernate.default_schema" value="testLineopen"/>
        </properties>
    </persistence-unit>
</persistence>

问题是在测试阶段调用我的测试类时,我的架构似乎不存在。在检查数据执行中,表存在于良好的模式中。 哪里有问题 ?桌子在哪里?

编辑: 我也尝试使用文件数据库,但它失败了,锁定问题。 数据库锁获取失败:lockFile。

谢谢。

【问题讨论】:

    标签: hibernate maven hsqldb


    【解决方案1】:

    可能的原因是测试在不同的 JVM 中运行,而他们对其他 JVM 中的内存 HSQLDB 一无所知。如果您使用基于文件而不是内存,它是否有效?

    【讨论】:

    • 文件数据库出现此错误:数据库锁定获取失败。
    【解决方案2】:

    您报告的“数据库锁定获取失败”错误清楚地表明测试在不同的 JVM 中运行。

    测试的最佳选择是带有remote_open 选项的 HSQLDB 服务器实例。当您进行第一次连接时,此选项会在服务器中创建一个数据库(可以是内存数据库)。有关详细信息和示例,请参阅http://hsqldb.org/doc/2.0/guide/listeners-chapt.html

    如果您使用同一个服务器实例多次运行测试,则可以在每次运行测试套件开始时删除架构。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2011-08-10
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多