数据库
首页 > 数据库> > spring – 从Maven Command Line获取HSQL连接时,JPA测试挂起

spring – 从Maven Command Line获取HSQL连接时,JPA测试挂起

作者:互联网

测试在Eclipse中单独工作(右键单击,以Junit身份运行),但是当我使用maven从命令行运行它们时,它会在尝试获取与HSQL数据库的DataSource连接时停止.

LogicalConnectionImpl [DEBUG] Obtaining JDBC connection

对我来说最奇怪的部分是,如果我将测试限制为10(不是类,而是单独的@Test方法),它将起作用.但是,一旦进行了第11次测试,就会爆炸.测试组合没关系,所以如果我有30个测试,并且我选择运行11个或更多,那么它将失败.这让我觉得它是一个DataSource连接最大问题,但到目前为止还没有运气细节.我已经尝试添加堆大小只是为了咯咯笑,但这不起作用.

因此,在此挂起之前,我有多个成功的JDBC连接和发布.

这是我的applicationContext-HSQL.xml,它用于Eclipse和Command Line版本.我知道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>

我的所有Test类都使用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;
}
}

关于为什么我可能会挂在那个连接上的任何想法,或者关于如何排除故障的想法?

谢谢,
     肖恩

解决方法:

根据通常的协议,良好的夜间睡眠可以帮助您思考.我完全覆盖了我的DataSource的appContext-hsqldb.xml属性的maxActive设置

<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="" />
    ****<property name="maxActive" value="-1"/>****
</bean>

希望这有助于将来的其他人.

http://commons.apache.org/dbcp/apidocs/org/apache/commons/dbcp/BasicDataSource.html

标签:spring,jpa,datasource,hsqldb,dbunit
来源: https://codeday.me/bug/20190517/1123260.html