Spring 兔子和 JDBC 交易问题
Spring rabbit and JDBC transaction issue
我有两个 spring 兔子消费者 A 和 B。
消费者 A:
protected void process(Message amqpMessage, Channel channel)
throws Exception {
Session mysession=sessionRepository.findOne(5);
mysession.setMyField("bla");
sessionRepository.save(mysession);
ServicesHelper.convertAndSend(<Consumer B token>, mysession.getId());
}
消费者 B:
protected void process(Message amqpMessage, Channel channel)
throws Exception {
//getting event from message
Long sessionId=5;
Session mysession=sessionRepository.findOne(sessionId);
mysession.setMyField("bla-bla");
//And I get there unpredictable optimistic locking exception.
sessionRepository.save(mysession);
}
似乎 jdbc 消费者 A 中的事务在退出侦听器方法后正在提交,这就是为什么重叠是可能的。我的 'session' 条目有一个 @Version 列,这就是我发现这个问题的原因。如何避免它,这是这样做的好方法吗?我只需要在消费者 A 中处理一个会话,然后传递给消费者 B。
我认为您可能在这里谈论 spring-data
而不是 spring-jdbc
。如果是这种情况(抱歉,如果不是)并且您正在使用 CrudRepository
,那么可能是因为与存储库的交互都是 @Transactional
。
例如,这里是保存的实际签名
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
我建议重构为以下内容并重试:-
protected void process(Message amqpMessage, Channel channel)
throws Exception {
ServicesHelper.convertAndSend(<Consumer B token>,adaptSession().getId()));
}
@Transactional
protected Session adaptSession(){
Session mysession=sessionRepository.findOne(5);
mysession.setMyField("bla");
sessionRepository.save(mysession);
return mysession;
}
和
@Transactional
protected void process(Message amqpMessage, Channel channel)
throws Exception {
//getting event from message
Long sessionId=5;
Session mysession=sessionRepository.findOne(sessionId);
mysession.setMyField("bla-bla");
//And I get there unpredictable optimistic locking exception.
sessionRepository.save(mysession);
}
这应该使交易单位匹配。试试看,我就知道了。
我有两个 spring 兔子消费者 A 和 B。
消费者 A:
protected void process(Message amqpMessage, Channel channel)
throws Exception {
Session mysession=sessionRepository.findOne(5);
mysession.setMyField("bla");
sessionRepository.save(mysession);
ServicesHelper.convertAndSend(<Consumer B token>, mysession.getId());
}
消费者 B:
protected void process(Message amqpMessage, Channel channel)
throws Exception {
//getting event from message
Long sessionId=5;
Session mysession=sessionRepository.findOne(sessionId);
mysession.setMyField("bla-bla");
//And I get there unpredictable optimistic locking exception.
sessionRepository.save(mysession);
}
似乎 jdbc 消费者 A 中的事务在退出侦听器方法后正在提交,这就是为什么重叠是可能的。我的 'session' 条目有一个 @Version 列,这就是我发现这个问题的原因。如何避免它,这是这样做的好方法吗?我只需要在消费者 A 中处理一个会话,然后传递给消费者 B。
我认为您可能在这里谈论 spring-data
而不是 spring-jdbc
。如果是这种情况(抱歉,如果不是)并且您正在使用 CrudRepository
,那么可能是因为与存储库的交互都是 @Transactional
。
例如,这里是保存的实际签名
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
我建议重构为以下内容并重试:-
protected void process(Message amqpMessage, Channel channel)
throws Exception {
ServicesHelper.convertAndSend(<Consumer B token>,adaptSession().getId()));
}
@Transactional
protected Session adaptSession(){
Session mysession=sessionRepository.findOne(5);
mysession.setMyField("bla");
sessionRepository.save(mysession);
return mysession;
}
和
@Transactional
protected void process(Message amqpMessage, Channel channel)
throws Exception {
//getting event from message
Long sessionId=5;
Session mysession=sessionRepository.findOne(sessionId);
mysession.setMyField("bla-bla");
//And I get there unpredictable optimistic locking exception.
sessionRepository.save(mysession);
}
这应该使交易单位匹配。试试看,我就知道了。