如何获取用于JPA/JTA DAO集成测试的JNDI数据源? [英] How to obtain JNDI data source for JPA/JTA DAO integration test?

查看:174
本文介绍了如何获取用于JPA/JTA DAO集成测试的JNDI数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPA应用程序,该应用程序在persistence.xml中指定了JTA事务.无论出于何种原因,我发现使用JTA时,还必须在persistence.xml中指定一个JNDI数据源.很好,除非您尝试在容器外进行集成测试并且JNDI不可用.

I have a JPA application that has specified JTA transactions in persistence.xml. For whatever reason, I have found that when using JTA, you MUST specify a JNDI data source within persistence.xml as well. This is fine, unless you are trying to go integration testing outside a container and JNDI is not available.

我的问题是:

a)是否有将jdbc数据源注入到我的JTA事务管理器中的方法? b)如果没有,在集成测试过程中如何处理JNDI查找?

a) is there anyway to inject a jdbc datasource into my JTA transaction manager? b) if not, how do a handle a JNDI lookup during integration testing?

启动集成测试时出现的错误是:

The error I get when firing up the integration test is:

 Caused by: org.springframework.......DataSourceLookupFailureException:
 Failed to look up JNDI DataSource with name 'java:comp/env/jdbc/myAppDataSource';
 nested exception is javax.naming.NoInitialContextException: Need to specify
 class name in environment or system property, or as an applet parameter,
 or in an application resource file:  java.naming.factory.initial

推荐答案

我自己没有对此进行实验,但是它可以通过独立的事务管理器和XA兼容数据源来实现.在Spring 2.5中,大多数示例都使用JOTM和 XAPool .但是在Spring 3.0中不再支持这些功能,并且 Atomikos 似乎是替代品"(它提供了独立的事务管理器和XA数据源).我在下面添加了一些配置示例.

I didn't experiment this myself but it is doable with a standalone transaction manager and a XA compliant datasource. With Spring 2.5, most samples use JOTM and the JotmFactoryBean and the XAPool. But these are non longer supported in Spring 3.0 and Atomikos appears to be the "replacement" (it provides a standalone transaction manager and a XA datasource). I've added some configuration samples below.

替代方法是运行容器内测试(使用嵌入式Java EE容器,例如 Cargo )之类的API.

The alternative would be to run in-container tests (using an embedded Java EE container e.g. GlassFish v3 Embedded or an API like Cargo).

  • Spring, JPA, and JTA with Hibernate and JOTM
  • Using Spring with multiple databases Part 1 (check the whole serie)
  • Replacing JOTM and XAPool with Atomikos (0.9.3) (another configuration example)
  • Spring / JTA / JPA unit test : Rollback not working
  • Guy Pardon: Transactional J2EE Apps with Spring

这篇关于如何获取用于JPA/JTA DAO集成测试的JNDI数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆