【问题标题】:Spring Boot+WebSocket+Stomp+JPA: data not inserted even with @Transactional annotationSpring Boot+WebSocket+Stomp+JPA:即使有@Transactional注解也没有插入数据
【发布时间】:2016-08-23 19:02:23
【问题描述】:

我有一个 RestController 包含两个方法:

  1. 方法beat1和@MessageMapping注解从客户端接收websocket消息(在STOMP协议中)
  2. 方法beat2带有@RequestMapping注解来接收来自客户端的HTTP请求

这两个方法里面的代码是一样的:

@RestController
@RequestMapping("/api/member")
@MessageMapping("member")
public class MemberCtrl
{
  @MessageMapping("beat")
  public Heartbeat beat1(@Payload Heartbeat heartbeat)
  {
    return memberService.beat(heartbeat);
  }

  @RequestMapping(value = "/beat", method = RequestMethod.POST)
  public Heartbeat beat2(@RequestBody Heartbeat heartbeat)
  {
    return memberService.beat(heartbeat);
  }
}

memberService 只是一个普通的 Spring 服务,其中包含将使用 Spring JPARepository 进行数据库数据插入的业务逻辑:

@Service
public class MemberServiceImpl implements MemberService, UserDetailsService
{

    @Autowired
    private HeartbeatRepository heartbeatRepository;

    @Transactional
    @Override
    public Heartbeat beat(Heartbeat heartbeat)
    {
        heartbeat = heartbeatRepository.save(heartbeat);
        return heartbeat;
    }
}

至于我HeartbeatRepository的代码:

@Repository
public interface HeatbeatRepository extends JpaRepository<Heartbeat, Integer>
{

}

这是我的问题:

  1. 当我使用 websocket 发送 STOMP 消息时,控制器中的 beat1 方法确实执行了,但是没有数据插入到 DB 表中。

  2. 当我发送http请求时,控制器中的beat2方法被执行,心跳域对象被成功插入到DB表中。

我正在实现 websocket,所以我需要的是让beat1 工作,我实际上根本不需要beat2。 (beat2 仅用于测试)

由于beat1beat2 方法都在memberSevice 中调用相同的Service 方法,并且beat2 有效,我认为我对MemberServiceHeartbeatRepository 的实现没有问题。

作为beat1beat2 的休眠日志,

  1. baat1只打印获取next id的SQL,并没有看到插入和事务提交的SQL:

    2016-08-23 18:32:56.227 DEBUG 43874 --- [nboundChannel-4] org.hibernate.SQL                        : select nextval ('heartbeat_id_seq')
    2016-08-23 18:32:56.227 DEBUG 43874 --- [nboundChannel-4] o.h.e.j.internal.LogicalConnectionImpl   : Obtaining JDBC connection
    2016-08-23 18:32:56.242 DEBUG 43874 --- [nboundChannel-4] o.h.e.j.internal.LogicalConnectionImpl   : Obtained JDBC connection
    2016-08-23 18:32:56.261 DEBUG 43874 --- [nboundChannel-4] org.hibernate.id.SequenceGenerator       : Sequence identifier generated: BasicHolder[java.lang.Integer[20]]
    2016-08-23 18:32:56.261 DEBUG 43874 --- [nboundChannel-4] o.h.e.i.AbstractSaveEventListener        : Generated identifier: 20, using strategy: org.hibernate.id.SequenceHiLoGenerator
    2016-08-23 18:32:56.266 DEBUG 43874 --- [nboundChannel-4] o.h.e.j.internal.LogicalConnectionImpl   : Releasing JDBC connection
    2016-08-23 18:32:56.266 DEBUG 43874 --- [nboundChannel-4] o.h.e.j.internal.LogicalConnectionImpl   : Released JDBC connection
    

2.beat2打印完成的insetaion SQL并提交事务,这就是它成功插入DB表的原因:

2016-08-23 18:20:29.937 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.t.spi.AbstractTransactionImpl      : begin
2016-08-23 18:20:29.937 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.j.internal.LogicalConnectionImpl   : Obtaining JDBC connection
2016-08-23 18:20:29.951 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.j.internal.LogicalConnectionImpl   : Obtained JDBC connection
2016-08-23 18:20:29.951 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.t.internal.jdbc.JdbcTransaction    : initial autocommit status: true
2016-08-23 18:20:29.951 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.t.internal.jdbc.JdbcTransaction    : disabling autocommit
2016-08-23 18:20:29.966 DEBUG 43846 --- [nio-8989-exec-9] org.hibernate.SQL                        : select nextval ('heartbeat_id_seq')
2016-08-23 18:20:29.986 DEBUG 43846 --- [nio-8989-exec-9] org.hibernate.id.SequenceGenerator       : Sequence identifier generated: BasicHolder[java.lang.Integer[19]]
2016-08-23 18:20:29.986 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.i.AbstractSaveEventListener        : Generated identifier: 19, using strategy: org.hibernate.id.SequenceHiLoGenerator
2016-08-23 18:20:29.992 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.t.spi.AbstractTransactionImpl      : committing
2016-08-23 18:20:29.993 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.i.AbstractFlushingEventListener    : Processing flush-time cascades
2016-08-23 18:20:29.993 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.i.AbstractFlushingEventListener    : Dirty checking collections
2016-08-23 18:20:29.995 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.i.AbstractFlushingEventListener    : Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
2016-08-23 18:20:29.995 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.i.AbstractFlushingEventListener    : Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2016-08-23 18:20:29.996 DEBUG 43846 --- [nio-8989-exec-9] o.hibernate.internal.util.EntityPrinter  : Listing entities:
2016-08-23 18:20:29.998 DEBUG 43846 --- [nio-8989-exec-9] o.hibernate.internal.util.EntityPrinter  : com.yamk.api.domain.orm.Heartbeat{lifespanSec=0, lostedSec=0, lastHeartbeatTime=Tue Aug 23 18:19:35 UTC 2016, createdTime=Tue Aug 23 18:20:29 UTC 2016, location=POINT (121.56818389892578 25.033194472364688), id=19}
2016-08-23 18:20:30.006 DEBUG 43846 --- [nio-8989-exec-9] org.hibernate.SQL                        : insert into heartbeat (created_time, last_heartbeat_time, lifespan_sec, location, losted_sec, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into heartbeat (created_time, last_heartbeat_time, lifespan_sec, location, losted_sec, id) values (?, ?, ?, ?, ?, ?)
2016-08-23 18:20:30.088 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.t.internal.jdbc.JdbcTransaction    : committed JDBC Connection
2016-08-23 18:20:30.088 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.t.internal.jdbc.JdbcTransaction    : re-enabling autocommit
2016-08-23 18:20:30.091 DEBUG 43846 --- [nio-8989-exec-9] m.m.a.RequestResponseBodyMethodProcessor : Written [com.yamk.api.domain.orm.Heartbeat@32] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@69f8302c]
2016-08-23 18:20:30.091 DEBUG 43846 --- [nio-8989-exec-9] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2016-08-23 18:20:30.091 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.jdbc.internal.JdbcCoordinatorImpl  : HHH000420: Closing un-released batch
2016-08-23 18:20:30.091 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.j.internal.LogicalConnectionImpl   : Releasing JDBC connection
2016-08-23 18:20:30.091 DEBUG 43846 --- [nio-8989-exec-9] o.h.e.j.internal.LogicalConnectionImpl   : Released JDBC connection

那么我的代码有什么问题导致beat1(WebSocket)和beat2(HTTP 请求)产生如此不同的结果?

似乎Spring不会自动给我一个事务,即使在调用MessageMapping方法beat1时使用@Transactional注释,这很奇怪。

【问题讨论】:

  • 我在here 发现了一个非常相似的问题。但是,该解决方案对我的情况没有意义,因为我使用 application.properties 文件的自动配置来配置数据库连接、JPA 和 Hibernate

标签: hibernate jpa spring-boot stomp spring-websocket


【解决方案1】:

最后我自己找到了解决方案。 事实证明,我必须使用@Bean 手动配置PlatformTransactionManager 并使用JpaTransactionManager 作为实现:

    @ComponentScan
    @Configuration
    @EnableSwagger2
    @EnableCaching
    @EnableJpaRepositories
    @EnableTransactionManagement
    @EnableJpaAuditing
    @SpringBootApplication
    public class App
    {
      public static void main(String[] args)
      {
          SpringApplication.run(App.class, args);
      }

      @Autowired
      private EntityManagerFactory entityManagerFactory;

      @Bean
      public PlatformTransactionManager transactionManager() 
      {
        return new JpaTransactionManager(entityManagerFactory);
      }

    }

就是这样,然后一切正常。

但我仍然不明白为什么 Spring Boot 没有使用我的 applicaion.properites 文件自动配置 PlatformTransactionManager。 也许我在 application.properites 中误解了某些内容或遗漏了某些内容,这就是我的 applicaion.properties 的样子:

    # data source
    spring.datasource.driver-class-name=org.postgresql.Driver
    spring.datasource.url=jdbc:postgresql://xxx.xxx.xxx.xxxx:####/xxxx
    spring.datasource.username=my-user-name
    spring.datasource.password=my-password
    spring.datasource.validationQuery=SELECT 1
    spring.datasource.test-on-borrow=true

    # jpa / hibernate
    spring.jpa.hibernate.ddl-auto:validate
    spring.jpa.show-sql:true
    spring.jpa.properties.hibernate.dialect = org.hibernate.spatial.dialect.postgis.PostgisDialect
    spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
    logging.level.org.hibernate=DEBUG

【讨论】:

  • 从 DataSourceTransactionManager(遗留代码)更改为 JpaTransactionManager 使其工作,感谢您的解决方案!
猜你喜欢
  • 2016-05-06
  • 2020-06-17
  • 2014-12-24
  • 1970-01-01
  • 2016-08-08
  • 2015-11-22
  • 2021-12-24
  • 2015-08-11
  • 2021-08-28
相关资源
最近更新 更多