1

I am attempting to create a EJB3.0 container-managed persistence bean and am having issues with connecting to the in-memory HSQLDB used by unit tests. I am using OpenEJB for my standalone container implementation.

I have the following in my persistence XML.

    <persistence-unit name="wyvern-unit-test">
    <description>In-memory HSQLDB database persistence unit used for unit testing.</description>
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.acme.test.model.LogEntry</class>
    <class>com.acme.test.modell.Addressee</class>
    <properties>
        <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
        <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:db"/>
        <property name="hibernate.connection.username" value="sa"/>
        <property name="hibernate.connection.password" value=""/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
</persistence-unit>

And this is my service bean:

@Stateless
public class LogEntryServiceBean implements LogEntryService {

@PersistenceContext
private EntityManager entityManager;

@Override
public LogEntry find(String uuid) {
    return entityManager.find(LogEntry.class, uuid);
}

@Override
public void save(LogEntry logEntry) {
    entityManager.merge(logEntry);
}

@Override
public void remove(LogEntry logEntry) {
    entityManager.remove(entityManager.merge(logEntry)); // NOTE: [MF] Using "seek and destroy" pattern.
}
}

This is my unit test:

public class LogEntryServiceBeanTest {

private static Context context;

@BeforeClass
public static void beforeClass() {
    context = EJBContainer.createEJBContainer().getContext();
}

@AfterClass
public static void afterClass() throws NamingException {
    context.close();
}

@Test
public void createLogEntryTest() throws NamingException {
    LogEntryService logEntryService = (LogEntryService) context.lookup("java:global/classes/LogEntryServiceBean");
    LogEntry logEntry = new LogEntry();
    Addressee addressee = new Addressee();
    logEntry.setSummary("This is a log entry.");
    addressee.setName("John Smith");
    addressee.setEmailAddress("john.smith@acme.com.au");
    logEntry.getAddressees().add(addressee);
    logEntryService.save(logEntry);
}

}

When I run my unit tests I get the following error:

java.sql.SQLException: Error in allocating a connection. Cause: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused.

Any ideas on what I am doing wrong (or not doing at all) ?

Thanks

S73417H
  • 2,661
  • 3
  • 23
  • 37

1 Answers1

1

I'm an idiot. I was starting the EJB3.0 embedded container which uses GlassFish libraries instead on the in-VM OpenEJB embedded container.

That is:

@BeforeClass
public static void beforeClass() {
   context = EJBContainer.createEJBContainer().getContext();
}

Should be:

@BeforeClass
public static void beforeClass() {
    Properties properties = new Properties();
    properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
    context = new InitialContext(properties);
}

Also, becuase HSQLDB is a non-JTA data source I needed to specify my persistence.xml persistence-unit as follows:

<persistence-unit name="wyvern-unit" transaction-type="RESOURCE_LOCAL">
S73417H
  • 2,661
  • 3
  • 23
  • 37