Spring / JTA / JPA 单元测试:回滚不起作用

2023-12-12

我正在尝试使用 Spring 测试实体 EJB3。

EJB 本身不使用 Spring,我希望尽量减少生产 JPA 配置的重复(例如,不重复 persistence.xml)。

我的单元测试似乎有效,但即使我的单元测试应该是事务性的,数据也会在各种测试方法之间保留......

这是我的实体:

package sample;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Ejb3Entity {

    public Ejb3Entity(String data) {
        super();
        this.data = data;
    }
    private Long id;
    private String data;

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }

    public String getData() {
        return data;
    }
    public void setData(String data) {
        this.data = data;
    }

}

我的单元测试:

package sample;

import static org.junit.Assert.*;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/appContext.xml"})
@Transactional
public class Ejb3EntityTest {

    @PersistenceContext
    EntityManager em;

    @Before
    public void setUp() throws Exception {
        Ejb3Entity one = new Ejb3Entity("Test data");
        em.persist(one);
    }

    @Test
    public void test1() throws Exception {

        Long count = (Long) em.createQuery("select count(*) from Ejb3Entity").getSingleResult();
        assertEquals(Long.valueOf(1l), count);
    }

    @Test
    public void test2() throws Exception {

        Long count = (Long) em.createQuery("select count(*) from Ejb3Entity").getSingleResult();
        assertEquals(Long.valueOf(1l), count);
    }

}

和我的 appContext.xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />

    <bean id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="userTransaction" ref="jotm" />
        <property name="allowCustomIsolationLevels" value="true" />
    </bean>

    <bean id="dataSource" class="org.enhydra.jdbc.standard.StandardXADataSource">
        <property name="driverName" value="org.h2.Driver" />
        <property name="url" value="jdbc:h2:mem:unittest;DB_CLOSE_DELAY=-1" />
        <property name="user" value="" />
        <property name="password" value="" />
        <property name="transactionManager" ref="jotm" />
    </bean>

    <bean id="emf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitPostProcessors">
            <bean class="sample.JtaDataSourcePersistenceUnitPostProcessor">
                <property name="jtaDataSource" ref="dataSource" />
            </bean>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="showSql" value="false" />
                <property name="generateDdl" value="true" />
                <property name="database" value="H2" />
                <property name="databasePlatform" value="org.hibernate.dialect.H2Dialect" />
            </bean>
        </property>
        <property name="jpaPropertyMap">
            <map>
                <entry key="hibernate.transaction.manager_lookup_class"
                    value="org.hibernate.transaction.JOTMTransactionManagerLookup" />
                <entry key="hibernate.transaction.auto_close_session" value="false" />
                <entry key="hibernate.current_session_context_class" value="jta" />
            </map>
        </property>

    </bean>


</beans>

当我运行测试时,test2 失败,因为它找到了 2 个实体,而我只期望一个实体(因为第一个实体应该已回滚...)

我尝试了很多不同的配置,这似乎是我能得到的最全面的配置......我没有其他想法。你 ?


当我尝试集成 JOTM 和 Hibernate 时,我最终不得不编写 ConnectionProvider 的实现代码。这是现在的样子:http://pastebin.com/f78c66e9c

然后,您将实现指定为休眠属性中的连接提供者,事务就神奇地开始工作。

问题是默认连接提供程序在数据源上调用 getConnection()。在您自己的实现中,您调用 getXAConnection().getConnection()。这就是不同之处

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring / JTA / JPA 单元测试:回滚不起作用 的相关文章

随机推荐

  • 重新排列中的自动字符串长度

    如果我以这种方式创建重新排列 In 29 np rec fromrecords 1 hello 2 world names a b 结果看起来不错 Out 29 rec array 1 hello 2 world dtype a
  • 即使指定了 WebGet 属性,WCF 代理也使用 Post(仅当从另一个 WCF 服务调用时) - 导致 405 错误

    我有一个 Restful WCF 服务位于另一台服务器上 配置了 WebGet 属性来响应 HTTP Get 方法 我知道该服务工作正常 因为我可以直接通过浏览器调用该服务 并使用 Fiddler 手动执行 Get 并收到正确的响应 我的本
  • 在 Unity3d 中将完整的相机视图 (16:9) 渲染到纹理上

    我正在研究 Unity 的渲染纹理 您可以将相机的视图渲染到纹理上 然而 我注意到它并没有渲染整个相机的视图 它仅渲染相机视图的方形切片 我正在尝试的是将相机的整个视图 16 9 长宽比 渲染到纹理 也是 16 9 长宽比 上 但现在它似乎
  • Java 中的线程数有硬性限制吗?

    Some sources比如说 你对 Java 中的线程数量有一个硬性限制 比如 15k 或 30k 即使你没有操作系统上限和无限的 RAM 我还听说 在 Java 7 中这个限制被取消了 这两种说法都属实吗 The Java虚拟机规范没有
  • MonoTouch/iOS 设备 (iPhone/iPad) 上的 protobuf-net 发生 JIT 编译错误

    我正在使用 protobuf net v2 beta r450 二进制发行版 并使用此处描述的技术提前构建序列化程序集 http www frictionpointstudios com blog 2011 3 31 using proto
  • 使用核心动画对图像进行排序,接收内存警告

    我使用 100 个动画图像收到内存警告 因此我尝试使用 Core Animation 但这给了我同样的问题 这是因为我不知道如何在当前代码中使用replaceSublayer UIView upwardView UIView alloc i
  • 异常安全和 make_unique

    只是为了澄清 使用make unique仅当表达式中有多个分配 而不仅仅是一个 时才增加异常安全性 对吗 例如 void f T f new T 是完全异常安全的 就分配和东西而言 而 void f T T f new T new T 是不
  • WCF ServiceHost basicHttpBinding 503 错误

    我正在尝试在 Windows 2007 SP1 服务器上将 WCF ServiceHost 作为 NT 服务运行 ServiceHost 负责托管单个服务端点 具有以下地址的 basicHttpBinding http localhost
  • RemoteControlReceivedWithEvent 在 iOS 7.0 设备上调用,但在 iOS 8.0 上不调用

    我有一个在后台播放音频的应用程序 我正在尝试修复主屏幕 等 上的音频控件 播放 暂停 在 iOS 8 0 上无法工作但在 iOS 7 0 上工作正常的错误 我一直在努力找出问题所在 但一无所获 任何想法将不胜感激 这是我所拥有的 在项目设置
  • 从图库中选择一张图像

    我看过很多关于此的帖子 看起来下面的代码应该可以工作 我已经创建了一个 SD 卡映像并将其添加到模拟器中 并且工作正常 Intent intent new Intent Intent ACTION PICK intent setType i
  • 将鼠标悬停在 div 上会影响外部元素[重复]

    这个问题在这里已经有答案了 我试图在 div 悬停时影响外部元素 像这样的事情 div class affected Hi div div div class hover me div div CSS hover me hover affe
  • Javascript 世界时钟仅显示在表中

    我试图显示世界各地几个不同城市的时间 我已经搜索和谷歌 甚至使用了这里找到的答案 PHP 或 JavaScript 中的世界时钟 API 但时钟只会显示在表格中 我使用了这个网站的教程 http www proglogic com code
  • 字体大小缩放问题

    我正在编写一个 C wxWidgets 计算器应用程序 我希望 wxTextCtrl 的字体和自定义按钮在调整窗口大小时能够缩放 问题是 我的按钮中的文本并不总是精确地位于中心 但有时会稍微偏离 特别是在绿色和红色按钮中 当我最大化窗口时
  • Python list 要列出的字符串

    我有一个字符串 s 7 9 41 32 67 我需要将该字符串转换为列表 l 7 9 41 32 67 问题是 当我使用列表时 我得到这个 7 9 4 1 3 2 6 7 我正在使用 python 3 2 You can完全按照您的要求使用
  • macOS Big Sur:意外的产品版本 11.1。如何修补 perlbrew?

    我刚刚将我的 Macbook Air 从 10 15 更新到 11 1 然后尝试使用 perlbrew 安装 perl 5 32 perlbrew install perl 5 32 0 Installing Users hakonhaeg
  • 轻松循环遍历 ElasticSearch 文档源数组

    我对网上商店中的产品有以下 ElasticSearch 数据结构 index vue storefront catalog 1 product 1617378559 type doc source configurable children
  • 使所有自动递增整数具有相同的位数且带有前导零

    我想知道是否有一种方法可以将我的 id 列 自动递增 设置为始终使用 mysql 表中的前导零组成 5 位数字 所以第一个值将是 00001 然后 00002 等等直到 99999 而不是 1 2 3 直到 99999 谢谢 尝试添加ZER
  • 快速更改 tableviewcell 的宽度

    我有一个使用 IB 的 tableView 以及自定义单元格和原型单元格 我试图使单元格的宽度比 tableView frame 短一点 以便在左右角之间留出一点空间 var cell tableView dequeueReusableCe
  • 如何在jquery对象上调用raphael方法?

    我正在使用拉斐尔创建一些圆圈 当用户单击按钮时 我想为这些圆圈设置动画 通过增加其半径 我该怎么做呢 例如 这是我的示例代码
  • Spring / JTA / JPA 单元测试:回滚不起作用

    我正在尝试使用 Spring 测试实体 EJB3 EJB 本身不使用 Spring 我希望尽量减少生产 JPA 配置的重复 例如 不重复 persistence xml 我的单元测试似乎有效 但即使我的单元测试应该是事务性的 数据也会在各种