Bitronix 交易管理器
我正在尝试从 JPA 迁移到 JTA 并使用 bitronix 事务管理器。尝试运行单元测试时,我收到以下错误消息。根据bitronix文档,这是正常的,b/c我的spring上下文配置正在尝试加载资源两次(一次在基类中,然后在测试类中,请参见下面的代码),我已经尝试了与atomikos相同的操作,并且得到了类似的结果。
原因: java.lang.IllegalArgumentException: 具有唯一名称“xyzDb”的资源 已注册
我的基类
@ContextConfiguration(locations = {"classpath:com/xyz/baseContext.xml"})
@Transactional
public abstract class AbstractTestSupport extends Assert implements ApplicationContextAware
{
在一些单元测试中我必须扩展测试支持并添加一个上下文配置文件,如下所示。因此它为基类加载一次上下文,为子类加载一次上下文,并失败
子类
@ContextConfiguration(locations = {"classpath:com/xyz/testContext.xml"})
public class UnitTest extends AbstractTestSupport
{
在测试之后我将关闭上下文,因此只要不使用另一个上下文配置文件扩展基类,下一个测试就可以正常工作。
@AfterClass
public static void onTearDownAfterClass() throws Exception
{
applicationContext.shutdownApplicationContext();
assertFalse("Spring application context is still active after shutdown. ", applicationContext.isActive());
}
我想将上下文配置文件保留在子类中并使其像这样工作,任何想法都将不胜感激......
I'm trying to migrate from JPA to JTA and use bitronix transaction manager. I'm getting below error message when try to run unit tests. According to bitronix documentation this is normal b/c my spring context configuration is trying to load the resources twice (once in base class and then in the test class, see code below), I have tried the same with atomikos and I got similar result.
Caused by:
java.lang.IllegalArgumentException:
resource with uniqueName 'xyzDb'
has already been registered
My base class
@ContextConfiguration(locations = {"classpath:com/xyz/baseContext.xml"})
@Transactional
public abstract class AbstractTestSupport extends Assert implements ApplicationContextAware
{
In some unit tests I have to extend the test support and add a context config file like below. so it loads context once for base class and another time for child class and fails
Child class
@ContextConfiguration(locations = {"classpath:com/xyz/testContext.xml"})
public class UnitTest extends AbstractTestSupport
{
After the test I'm shutting down context, so next test works fine as long as it doesn't extend the base class with another context config file.
@AfterClass
public static void onTearDownAfterClass() throws Exception
{
applicationContext.shutdownApplicationContext();
assertFalse("Spring application context is still active after shutdown. ", applicationContext.isActive());
}
I want to keep context config files in the child classes and make this work like that, any ideas greatly appreciated....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误消息基本上意味着您在抛出异常时第二次创建了具有唯一名称“xyzDb”的连接池(还记得您需要在 BTM 池上设置一个 uniqueName 属性吗?)。您不能这样做:每个连接池必须具有唯一的名称,并且必须先关闭,然后才能创建另一个具有相同名称的连接池。
我想您的两个上下文文件之间存在一些重叠,导致出现这种情况,或者连接池并不总是像应有的那样关闭。不幸的是,您发布的信息太少,无法获得明确的答案。
The error message basically means you created the connection pool with unique name 'xyzDb' (remember there is a uniqueName property you need to set on BTM's pools?) for the second time at the time the exception is thrown. You cannot do that: each connection pool must have a unique name and must be closed before another one with an identical name can be created.
I suppose there is some overlap between your two context files causing this or maybe the connections pools aren't always closed like they should. Unfortunately you published too little information to get a definitive answer.