Sean Charles
Sean Charles

Reputation: 359

JPA tests hang while obtaining HSQL connection from Maven Command Line

Tests work individually within Eclipse (right click, run as Junit), but when I run them from the command line with maven, it stops while trying to obtain a DataSource connection to the HSQL database.

LogicalConnectionImpl [DEBUG] Obtaining JDBC connection

The oddest part to me is that if I limit the tests being run to 10 (not classes, but individual @Test methods), it will work. But once that 11th test is run it bombs out. The test combinations do not matter, so if I have 30 tests, and I choose 11 or more to run, then it will fail. This made me think it is a DataSource connection max issue, but so far no luck with details of that. I have tried adding heap size just for giggles, but that did not work.

So prior to this hang, I have multiple successful JDBC connection and releases.

Here is my applicationContext-HSQL.xml, which is used for both the in Eclipse and Command Line versions. I know b/c I have forced each of them to fail by messing with the Class values below.

<?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>

All of my Test classes utilize Spring 3.2.5, JPA, DBUnit and extend 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;
}
}

Any ideas on why I may be hanging on that connection, or an idea of how to troubleshoot?

Thanks, Sean

Upvotes: 5

Views: 1324

Answers (3)

Leonardo Beal
Leonardo Beal

Reputation: 734

If the hanging comes after few page access, (maybe 8 which is the default maxTotal), you are not never closing the connection and opening in every request. Just use entityManager.close(); after the db queries or check if the a connection already exists before creating a new one.

The accepted answer is setting the maxTotal connections to unlimited. Connections will just keep pilling up.

Upvotes: 0

Sean Charles
Sean Charles

Reputation: 359

Per the usual protocol, a good nights sleep helps you think. I completely over-looked the maxActive setting available for the appContext-hsqldb.xml property for my DataSource

<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>

Hope this helps someone else in the future.

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

Upvotes: 5

DataNucleus
DataNucleus

Reputation: 15577

if something is hanging you can use "kill -3 {process_number}" on Unix and it gives a thread dump (if not on Unix there is an equivalent on Windows/Mac). That will show where the lock problem is, which may give you an idea what causes it and how to fix it

Upvotes: 1

Related Questions