fix: No EntityManager with actual transaction available for current thread when remove method called (#1716)

pull/1730/head
guqing 2022-03-09 12:15:34 +08:00 committed by GitHub
parent a450868140
commit f733fbea7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 8 deletions

View File

@ -27,6 +27,7 @@ import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.lang.NonNull; import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import run.halo.app.event.comment.CommentNewEvent; import run.halo.app.event.comment.CommentNewEvent;
@ -304,6 +305,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
@Override @Override
@NonNull @NonNull
@Transactional(rollbackFor = Exception.class)
public COMMENT create(@NonNull COMMENT comment) { public COMMENT create(@NonNull COMMENT comment) {
Assert.notNull(comment, "Domain must not be null"); Assert.notNull(comment, "Domain must not be null");
@ -369,6 +371,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
@Override @Override
@NonNull @NonNull
@Transactional(rollbackFor = Exception.class)
public COMMENT createBy(@NonNull BaseCommentParam<COMMENT> commentParam) { public COMMENT createBy(@NonNull BaseCommentParam<COMMENT> commentParam) {
Assert.notNull(commentParam, "Comment param must not be null"); Assert.notNull(commentParam, "Comment param must not be null");
@ -402,6 +405,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
@Override @Override
@NonNull @NonNull
@Transactional(rollbackFor = Exception.class)
public COMMENT updateStatus(@NonNull Long commentId, @NonNull CommentStatus status) { public COMMENT updateStatus(@NonNull Long commentId, @NonNull CommentStatus status) {
Assert.notNull(commentId, "Comment id must not be null"); Assert.notNull(commentId, "Comment id must not be null");
Assert.notNull(status, "Comment status must not be null"); Assert.notNull(status, "Comment status must not be null");
@ -418,6 +422,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
@Override @Override
@NonNull @NonNull
@Transactional(rollbackFor = Exception.class)
public List<COMMENT> updateStatusByIds(@NonNull List<Long> ids, @NonNull CommentStatus status) { public List<COMMENT> updateStatusByIds(@NonNull List<Long> ids, @NonNull CommentStatus status) {
if (CollectionUtils.isEmpty(ids)) { if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList(); return Collections.emptyList();
@ -428,6 +433,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public List<COMMENT> removeByPostId(@NonNull Integer postId) { public List<COMMENT> removeByPostId(@NonNull Integer postId) {
Assert.notNull(postId, "Post id must not be null"); Assert.notNull(postId, "Post id must not be null");
return baseCommentRepository.deleteByPostId(postId); return baseCommentRepository.deleteByPostId(postId);
@ -435,6 +441,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
@Override @Override
@NonNull @NonNull
@Transactional(rollbackFor = Exception.class)
public COMMENT removeById(@NonNull Long id) { public COMMENT removeById(@NonNull Long id) {
Assert.notNull(id, "Comment id must not be null"); Assert.notNull(id, "Comment id must not be null");
@ -455,6 +462,7 @@ public abstract class BaseCommentServiceImpl<COMMENT extends BaseComment>
@Override @Override
@NonNull @NonNull
@Transactional(rollbackFor = Exception.class)
public List<COMMENT> removeByIds(@NonNull Collection<Long> ids) { public List<COMMENT> removeByIds(@NonNull Collection<Long> ids) {
if (CollectionUtils.isEmpty(ids)) { if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList(); return Collections.emptyList();

View File

@ -264,13 +264,13 @@ public abstract class BasePostServiceImpl<POST extends BasePost>
} }
@Override @Override
@Transactional @Transactional(rollbackFor = Exception.class)
public void increaseVisit(Integer postId) { public void increaseVisit(Integer postId) {
increaseVisit(1L, postId); increaseVisit(1L, postId);
} }
@Override @Override
@Transactional @Transactional(rollbackFor = Exception.class)
public void increaseLike(long likes, Integer postId) { public void increaseLike(long likes, Integer postId) {
Assert.isTrue(likes > 0, "Likes to increase must not be less than 1"); Assert.isTrue(likes > 0, "Likes to increase must not be less than 1");
Assert.notNull(postId, "Post id must not be null"); Assert.notNull(postId, "Post id must not be null");
@ -285,7 +285,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost>
} }
@Override @Override
@Transactional @Transactional(rollbackFor = Exception.class)
public void increaseLike(Integer postId) { public void increaseLike(Integer postId) {
increaseLike(1L, postId); increaseLike(1L, postId);
} }
@ -295,7 +295,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost>
* @return post with handled data * @return post with handled data
*/ */
@Override @Override
@Transactional @Transactional(rollbackFor = Exception.class)
public POST createOrUpdateBy(POST post) { public POST createOrUpdateBy(POST post) {
Assert.notNull(post, "Post must not be null"); Assert.notNull(post, "Post must not be null");
PatchedContent postContent = post.getContent(); PatchedContent postContent = post.getContent();
@ -344,7 +344,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost>
} }
@Override @Override
@Transactional @Transactional(rollbackFor = Exception.class)
public POST updateStatus(PostStatus status, Integer postId) { public POST updateStatus(PostStatus status, Integer postId) {
Assert.notNull(status, "Post status must not be null"); Assert.notNull(status, "Post status must not be null");
Assert.isTrue(!ServiceUtils.isEmptyId(postId), "Post id must not be empty"); Assert.isTrue(!ServiceUtils.isEmptyId(postId), "Post id must not be empty");
@ -374,7 +374,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost>
} }
@Override @Override
@Transactional @Transactional(rollbackFor = Exception.class)
public List<POST> updateStatusByIds(List<Integer> ids, PostStatus status) { public List<POST> updateStatusByIds(List<Integer> ids, PostStatus status) {
if (CollectionUtils.isEmpty(ids)) { if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList(); return Collections.emptyList();
@ -403,6 +403,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost>
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public POST create(POST post) { public POST create(POST post) {
// Check title // Check title
slugMustNotExist(post); slugMustNotExist(post);
@ -411,6 +412,7 @@ public abstract class BasePostServiceImpl<POST extends BasePost>
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public POST update(POST post) { public POST update(POST post) {
// Check title // Check title
slugMustNotExist(post); slugMustNotExist(post);

View File

@ -163,7 +163,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
} }
@Override @Override
@Transactional @Transactional(rollbackFor = Exception.class)
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds, public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds,
Set<PostMeta> metas, boolean autoSave) { Set<PostMeta> metas, boolean autoSave) {
PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds, metas); PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds, metas);
@ -177,6 +177,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds, public PostDetailVO createBy(Post postToCreate, Set<Integer> tagIds, Set<Integer> categoryIds,
boolean autoSave) { boolean autoSave) {
PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds, null); PostDetailVO createdPost = createOrUpdate(postToCreate, tagIds, categoryIds, null);
@ -190,7 +191,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
} }
@Override @Override
@Transactional @Transactional(rollbackFor = Exception.class)
public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds, public PostDetailVO updateBy(Post postToUpdate, Set<Integer> tagIds, Set<Integer> categoryIds,
Set<PostMeta> metas, boolean autoSave) { Set<PostMeta> metas, boolean autoSave) {
// Set edit time // Set edit time
@ -280,6 +281,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public List<Post> removeByIds(Collection<Integer> ids) { public List<Post> removeByIds(Collection<Integer> ids) {
if (CollectionUtils.isEmpty(ids)) { if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList(); return Collections.emptyList();
@ -490,6 +492,7 @@ public class PostServiceImpl extends BasePostServiceImpl<Post> implements PostSe
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Post removeById(Integer postId) { public Post removeById(Integer postId) {
Assert.notNull(postId, "Post id must not be null"); Assert.notNull(postId, "Post id must not be null");

View File

@ -10,6 +10,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import run.halo.app.event.logger.LogEvent; import run.halo.app.event.logger.LogEvent;
import run.halo.app.event.post.SheetVisitEvent; import run.halo.app.event.post.SheetVisitEvent;
@ -83,6 +84,7 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet>
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Sheet createBy(Sheet sheet, boolean autoSave) { public Sheet createBy(Sheet sheet, boolean autoSave) {
Sheet createdSheet = createOrUpdateBy(sheet); Sheet createdSheet = createOrUpdateBy(sheet);
if (!autoSave) { if (!autoSave) {
@ -96,6 +98,7 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet>
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Sheet createBy(Sheet sheet, Set<SheetMeta> metas, boolean autoSave) { public Sheet createBy(Sheet sheet, Set<SheetMeta> metas, boolean autoSave) {
Sheet createdSheet = createOrUpdateBy(sheet); Sheet createdSheet = createOrUpdateBy(sheet);
@ -115,6 +118,7 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet>
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Sheet updateBy(Sheet sheet, boolean autoSave) { public Sheet updateBy(Sheet sheet, boolean autoSave) {
Sheet updatedSheet = createOrUpdateBy(sheet); Sheet updatedSheet = createOrUpdateBy(sheet);
if (!autoSave) { if (!autoSave) {
@ -128,6 +132,7 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet>
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Sheet updateBy(Sheet sheet, Set<SheetMeta> metas, boolean autoSave) { public Sheet updateBy(Sheet sheet, Set<SheetMeta> metas, boolean autoSave) {
Sheet updatedSheet = createOrUpdateBy(sheet); Sheet updatedSheet = createOrUpdateBy(sheet);
@ -260,6 +265,7 @@ public class SheetServiceImpl extends BasePostServiceImpl<Sheet>
} }
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Sheet removeById(Integer id) { public Sheet removeById(Integer id) {
// Remove sheet metas // Remove sheet metas