Spring Boot -事务管理不工作

lbsnaicq  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(181)

期望延迟加载不应该在事务范围之外工作(例如在rest控制器中),但它可以工作。

问题出在@ transmitting**中,我配置中的spring应用没有使用,怎么解决?

. Rest控制器没有任何transactional方法,它只使用specifiedServices来加载实体。如果没有在service中加载依赖集合,则应该为空。
应用程序启动器类:

  1. @SpringBootApplication
  2. @EntityScan("com.vl.pmanager.domain.model")
  3. @EnableJpaRepositories("com.vl.pmanager.domain.repository")
  4. @EnableTransactionManagement
  5. public class ProjectManagerApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(ProjectManagerApplication.class, args);
  8. }
  9. }

字符串
我知道spring Boot 会自动配置仓库和扫描实体,但我希望添加.

  1. @EntityScan("com.vl.pmanager.domain.model")
  2. @EnableJpaRepositories("com.vl.pmanager.domain.repository")


我还尝试将 *@ translation * 添加到仓库接口,但它对我不起作用

  1. package com.vl.pmanager.domain.repository;
  2. import com.vl.pmanager.domain.model.Tag;
  3. import org.springframework.data.repository.PagingAndSortingRepository;
  4. import org.springframework.stereotype.Repository;
  5. import org.springframework.transaction.annotation.Transactional;
  6. @Repository
  7. @Transactional
  8. public interface TagRepository extends PagingAndSortingRepository<Tag, Long> {
  9. }


因此,我从仓库中删除了 *@ translation *,创建了其他服务层来使用注解管理它,并将服务注入到控制器:

  1. package com.vl.pmanager.domain.db.service.api;
  2. import com.vl.pmanager.domain.model.Tag;
  3. import org.springframework.data.domain.Page;
  4. import org.springframework.data.domain.Pageable;
  5. public interface ITagManager {
  6. Page<Tag> findAll(Pageable pageable);
  7. Tag findOne(Long id);
  8. }
  9. // ======================== TAG SERVICE WRAPPED WITH TRANSACTION ==========================
  10. package com.vl.pmanager.domain.db.service;
  11. import com.vl.pmanager.domain.db.service.api.ITagManager;
  12. import com.vl.pmanager.domain.model.Tag;
  13. import com.vl.pmanager.domain.repository.TagRepository;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.data.domain.Page;
  16. import org.springframework.data.domain.Pageable;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. @Service
  20. @Transactional
  21. public class TagManager implements ITagManager {
  22. @Autowired
  23. private TagRepository tagRepository;
  24. @Override
  25. public Page<Tag> findAll(Pageable pageable) {
  26. return tagRepository.findAll(pageable);
  27. }
  28. @Override
  29. public Tag findOne(Long id) {
  30. return tagRepository.findOne(id);
  31. }
  32. }
  33. // ====================== REST CONTROLLER ============================
  34. package com.vl.pmanager.web;
  35. import com.vl.pmanager.domain.db.service.api.ITagManager;
  36. import com.vl.pmanager.domain.model.Tag;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.data.domain.Page;
  41. import org.springframework.data.domain.Pageable;
  42. import org.springframework.data.web.PageableDefault;
  43. import org.springframework.http.MediaType;
  44. import org.springframework.web.bind.annotation.PathVariable;
  45. import org.springframework.web.bind.annotation.RequestMapping;
  46. import org.springframework.web.bind.annotation.RequestMethod;
  47. import org.springframework.web.bind.annotation.RestController;
  48. @RestController
  49. @RequestMapping(value = "/tags")
  50. public class TagController {
  51. private static final int DEFAULT_PAGE_SIZE = 50;
  52. @Autowired
  53. private ITagManager tagManager;
  54. @RequestMapping(method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
  55. public Page<Tag> getTags(@PageableDefault(size = DEFAULT_PAGE_SIZE) Pageable pageable) {
  56. return tagManager.findAll(pageable);
  57. }
  58. @RequestMapping(value = "/{id}", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE)
  59. public Tag getTag(@PathVariable("id") Long id) {
  60. return tagManager.findOne(id);
  61. }
  62. }


标记实体有一个字段 Set projectInfo Map到其他实体,该实体具有 @ManyToMany 关系,带有 FetchType fetch()default LAZY;,因此返回结果不能包含依赖实体,但它确实包含。
我还监视了DB日志:

  1. // make request - load data using db service to controller level
  2. Hibernate: select count(tag0_.id) as col_0_0_ from tag tag0_
  3. Hibernate: select tag0_.id as id1_6_, tag0_.description as descript2_6_, tag0_.name as name3_6_ from tag tag0_ limit ?
  4. // converting data to JSON automatically
  5. Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
  6. Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
  7. Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
  8. Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?
  9. Hibernate: select projectinf0_.tags as tags1_6_0_, projectinf0_.project_info as project_2_7_0_, projectinf1_.id as id1_4_1_, projectinf1_.description as descript2_4_1_, projectinf1_.name as name3_4_1_ from tag_project_info projectinf0_ inner join project_info projectinf1_ on projectinf0_.project_info=projectinf1_.id where projectinf0_.tags=?


我知道只有5个额外的请求,因为我只有5个依赖于 tag**project_info。因此,作为结论,我的transaction级别不管理transaction。.我已经检查了插件和transactionManager注入beans -创建了一个。没有错误,启动时或运行时没有警告.

mrfwxfqh

mrfwxfqh1#

由JB Nizet在评论中
这不是一个事务问题。在文档中查找属性spring.jpa.open-in-view,如果不需要,将其设置为false。
如果我在视图中关闭 *jpa open *,交易就像我预期的那样工作。

相关问题