HSQLDB EJB3.0 Hibernate无法连接数据库
我正在尝试创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我是个白痴。我正在启动 EJB3.0 嵌入式容器,它使用 GlassFish 库,而不是在 VM 内的 OpenEJB 嵌入式容器上。
即:
应该是:
另外,因为 HSQLDB 是非 JTA 数据源,所以我需要指定 persistence.xml 持久性单元,如下所示:
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:
Should be:
Also, becuase HSQLDB is a non-JTA data source I needed to specify my persistence.xml persistence-unit as follows: