【发布时间】: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。
谢谢。
【问题讨论】: