HSQLDB EJB3.0 Hibernate无法连接数据库

发布于 2024-11-02 15:40:39 字数 2745 浏览 1 评论 0原文

我正在尝试创建 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("[email protected]");
    logEntry.getAddressees().add(addressee);
    logEntryService.save(logEntry);
}

}

当我运行单元测试时,出现以下错误:

java.sql.SQLException:分配连接时出错。原因:无法分配连接,因为:java.net.ConnectException:连接到端口 1527 上的服务器本地主机时出错,并显示消息“连接被拒绝”。

关于我做错了什么(或根本没有做)有什么想法吗?

谢谢

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("[email protected]");
    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的绿洲 2024-11-09 15:40:39

我是个白痴。我正在启动 EJB3.0 嵌入式容器,它使用 GlassFish 库,而不是在 VM 内的 OpenEJB 嵌入式容器上。

即:

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

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">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文