目前,我使用 Spring 4.0.6 和 Postgresql 9.5。我正在将一个 serviceClass1
调用到另一个 serviceClass2
并获得事务异常,如下所示:
serviceClass1.class
`@Transactional(readOnly = false, propagation = Propagation.REQUIRED,rollbackFor= { Throwable.class })
public Map<String, Object> storeUsersList(Map<String, Object> mapOfParams) throws Exception {
Map<String, Object> returnMap = new HashMap<String, Object>();
if (userListToStore != null && !userListToStore.isEmpty()) {
integrationService.manangePassCodes(org,userListToStore.size());
for (Users singleUser : userListToStore) {
try {
getEm().update(singleUser);`
serviceClass2.class
@Override
@Transactional(readOnly=false,propagation=Propagation.REQUIRED
,rollbackFor{Throwable.class})
public void manangePassCodes(Organizations org,int userToRegisterCount)throws Exception{
//some Logic
我在这个地方遇到异常 -
getEm().update(singleUser);
Exception SQL state [25P02]; error code [0]; could not extract ResultSet
我刚刚读到了有关 Postgres 事务的错误,但无法了解我应该在 Hibernate 中使用什么。
最佳答案
不能 100% 确定情况是否如此,但在您的 storeUsersList
方法中,您似乎正在重用存储在实例变量 userListToStore
中的现有 User 集合。
此用户集合不是从事务内部初始化的,因此当您使用容器管理事务时,实体很可能会分离。
在我看来,您应该在执行更新之前合并每个实体,以便持久性上下文能够识别它们:
for (Users singleUser : userListToStore) {
try {
getEm().merge(singleUser);
getEm().update(singleUser);
关于java - Spring和Postgres事务错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42339978/