HSQLDB EJB3.0 Hibernate无法连接到DB

时间:2011-04-20 14:28:16

标签: hibernate ejb hsqldb persistence.xml

我正在尝试创建EJB3.0容器管理的持久性bean,并且在连接到单元测试使用的内存中HSQLDB时遇到问题。我正在使用OpenEJB来实现我的独立容器实现。

我在持久性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>

这是我的服务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.
}
}

这是我的单元测试:

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);
}

}

当我运行单元测试时,我收到以下错误:

  

java.sql.SQLException:分配连接时出错。原因:无法分配连接,因为:java.net.ConnectException:在端口1527上连接到服务器localhost时出错,并显示消息Connection refused。

关于我做错了什么(或根本不做)的任何想法?

由于

1 个答案:

答案 0 :(得分:1)

我是个白痴。我正在启动EJB3.0嵌入式容器,该容器在VM-OpenEJB嵌入式容器中使用GlassFish库。

那是:

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

应该是:

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

另外,因为HSQLDB是一个非JTA数据源,我需要指定我的persistence.xml持久性单元,如下所示:

<persistence-unit name="wyvern-unit" transaction-type="RESOURCE_LOCAL">