- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Spring Boot Application 中的服务方法被多个线程访问,一个线程正在修改 DB 中的数据。
当其他线程在第一次提交之前访问数据时,它不会获得较早线程的数据库更新。
在 stackoverflow 上长时间搜索后,我对相关方法使用了以下注释。
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED)
public void accept(Event<String> event) {}
但是没有达到预期的效果。未提交的更改不会在第二个线程中检索。
请帮我解决这个问题。
我使用了实体管理器的 flush() 方法,但没有成功。
最佳答案
您混淆了隔离级别和锁。
READ_UNCOMMITTED
隔离级别允许其他事务在提交之前查看您的第一个事务写入的内容。它不需要发生这种情况,也不会影响锁。但根据您的评论,您真正想要实现的是以某种方式正确锁定数据库行,以便其他人无法访问它们。
执行此操作的 JPA 方法是使用适当的锁定模式查询数据:
Lock modes can be specified by means of the EntityManager lock method, the methods of the EntityManager, Query, and TypedQuery interfaces that allow lock modes to be specified, and the NamedQuery annotation.
(来自 JPA Specification 的 3.4.4)
您可能对“悲观”变体感兴趣,它会阻止另一个事务在锁定的行被提交之前读取它们(因此锁定被释放)
If transaction T1 calls lock(entity, LockModeType.PESSIMISTIC_READ) or lock(entity, LockModeType.PESSIMISTIC_WRITE)onanobject,the entitymanager must ensure that neither of the following phenomena can occur:
- P1 (Dirty read): Transaction T1 modifies a row. Another transaction T2 then reads that row and obtains the modified value, before T1 has committed or rolled back.
- P2 (Non-repeatable read): Transaction T1 reads a row. Another transaction T2 then modifies or deletes that row, before T1 has committed or rolled back.
(来自 JPA Specification 的 3.4.4.2)
您可以通过使用 @Lock
将这些锁定模式与 Spring Data JPA 一起使用注释。
关于java - 读取 Spring JPA Repository 中未提交的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46067867/
在过去的几个月里,我一直在使用 Bzr 对我的项目进行版本控制。我是唯一的开发人员,目前我只有一个本地项目目录中的所有内容,我提交并同步到 DriveHQ。 我现在想到了一些可能会打破这条主线的大规模
我在一个多模块项目中使用 Maven 3.2.3。我想生成一个 checkstyle 和 findbugs 报告,所以我配置了以下内容:
我注意到 Repository 通常通过以下任一方式实现: 方法一 void Add(object obj); void Remove(object obj); object GetBy(int id
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 5年前关闭。 Improve thi
这是我的设置的详细信息: Gitlab 版本:5.2操作系统:Centos 6.3我在创建新项目 (/projects/new) 时导入现有存储库。 创建了一个新的 EMPTY 项目,但是没有导入存储
我的文件夹结构如下: repo1 | |---file1 |---fold1 | |---file2 |---repo2 | | |---file3 假设我有两
假设我有一个 TeacherRepository,需要根据下面的代码使用 CourseRepository。 Teacher 和 Course 形成多对多的关系。教师和类(class)不形成聚合。您会
我要同样的 repositories我在 buildscript.repositories 中指定的阻止与依赖项相同的存储库 repositories堵塞。请看我的例子: 正常 buidldscrip
是否使用 @EnableJpaRepositories 或 jpa:repositories(在 xml 上)让开发人员不要使用 Spring 的 @Repository 标签?当我查看 Spring
我是 git 和 Github 的新手。我已经了解了很多术语(推送、 pull 、提交、分支等),但我将主要使用通俗的说法来解释我最初的期望。 我假设过程是: 1.) Create a git rep
安装 Nexus Repository Manager OSS 3 后,我没有看到用于通过网页上传工件的选项Artifact Upload。 在 Nexus Repository Manager OS
CMS 和 DMS 有什么区别?两者都存储日期,可以访问数据,它们有什么不同?可以使用 apache Jack Rabbit 代替 Alfresco 吗? 最佳答案 我会根据管理数据的可变性来区分这两
在我的earlier question我问的是如何为使用 EF 等 ORM 框架构建的大型应用程序实现存储库/工作单元模式。 我现在无法解决的一个后续问题是将包含业务逻辑的代码放在哪里,但仍然足够低级
我正在尝试为 nuget git 存储库(我刚刚从中克隆)创建 pull 请求。我已经进行了本地提交。 但是当我尝试创建 pull 请求时,出现以下错误: Could not find any rel
我已经看到了下面的问题。 git diff between cloned and original remote repository 我的问题是如何在 SourceTree 中看到这个差异结果(而不
我在通过 Subversion (SVN) 中的 checkin 自动在 Review Board 中创建新的评论条目时遇到了困难。我创建了一个提交后 Hook ,当手动运行时会出现以下异常: Fai
我在尝试集成 Spring Data 时遇到此错误。完整的堆栈跟踪是 nested exception is org.xml.sax.SAXParseException; systemId: http
通知:https://docs.aws.amazon.com/codecommit/latest/userguide/how-to-repository-email.html 触发器:https://
我正在学习 Laravel 中的存储库设计模式,我正在使用 https://github.com/andersao/l5-repository去做这件事。 但是在我将文件夹 prettus 复制/粘贴
我最近开始了一个使用现有数据库(Oracle)和 MVC 4 的项目。已经进行了很多编码..但是代码中没有“策略”..只有 DB -> ORM -> Controller。因此,我正在尝试为开发添加一
我是一名优秀的程序员,十分优秀!