测试在 Eclipse 中单独工作(右键单击,以 Junit 身份运行),但是当我使用 maven 从命令行运行它们时,它会在尝试获取与 HSQL 数据库的 DataSource 连接时停止。
LogicalConnectionImpl [DEBUG] Obtaining JDBC connection
对我来说最奇怪的是,如果我将运行的测试限制为 10 个(不是类,而是单个 @Test 方法),它将起作用。但是一旦第 11 次测试运行,它就会爆炸。测试组合无关紧要,所以如果我有 30 个测试,并且我选择 11 个或更多来运行,那么它将失败。这让我认为这是一个 DataSource 连接最大问题,但到目前为止还没有详细说明。我曾尝试仅为咯咯笑而添加堆大小,但这没有用。
所以在这个挂起之前,我有多个成功的 JDBC 连接和发布。
这是我的 applicationContext-HSQL.xml,它用于 Eclipse 和命令行版本。我知道 b/c 我已经通过弄乱下面的 Class 值来迫使他们每个人失败。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:flow="http://www.springframework.org/schema/webflow-config"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.company"/>
<import resource="classpath*:/application-context-cxf.xml"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation" value="META-INF/persistence.xml" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="false" />
<property name="generateDdl" value="false" />
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
</bean>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:test" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" >
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
我所有的测试类都使用 Spring 3.2.5、JPA、DBUnit 并扩展 AbstractInMemoryTests.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-hsqldb.xml"})
public class AbstractInMemoryTests {
private static final String FLAT_XML_DATASET = "FlatXmlDataSet.xml";
@Autowired
BasicDataSource bds;
@Before
public void setUp() throws Exception {
DatabaseOperation.CLEAN_INSERT.execute(getConnection(), getDataSet());
}
@SuppressWarnings("deprecation")
private IDataSet getDataSet() throws Exception {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(FLAT_XML_DATASET);
IDataSet dataset = new FlatXmlDataSet(inputStream);
return dataset;
}
private IDatabaseConnection getConnection() throws Exception {
Connection jdbcConnection = bds.getConnection();
IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
return connection;
}
}
关于我为什么会挂在那个连接上的任何想法,或者关于如何排除故障的想法?
谢谢,肖恩