【发布时间】:2016-08-23 19:02:23
【问题描述】:
我有一个 RestController 包含两个方法:
- 方法
beat1和@MessageMapping注解从客户端接收websocket消息(在STOMP协议中) - 方法
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>
{
}
这是我的问题:
当我使用 websocket 发送 STOMP 消息时,控制器中的
beat1方法确实执行了,但是没有数据插入到 DB 表中。当我发送http请求时,控制器中的
beat2方法被执行,心跳域对象被成功插入到DB表中。
我正在实现 websocket,所以我需要的是让beat1 工作,我实际上根本不需要beat2。 (beat2 仅用于测试)
由于beat1 和beat2 方法都在memberSevice 中调用相同的Service 方法,并且beat2 有效,我认为我对MemberService 和HeartbeatRepository 的实现没有问题。
作为beat1 和beat2 的休眠日志,
-
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