简体   繁体   English

Spring 启动、jms 监听和数据库事务

[英]Spring boot, jms listener and database transaction

I have 2 records in database because locally created and saved entity wasn't found in a different thread.我在数据库中有 2 条记录,因为在不同的线程中没有找到本地创建和保存的实体。

My configuration is like this:我的配置是这样的:

@Configuration
@EnableJms
@EnableJpaRepositories
public class MyConfiguration {

@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory,
                                                                  DefaultJmsListenerContainerFactoryConfigurer configurer) {
    DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
    configurer.configure(factory, connectionFactory);
    factory.setRecoveryInterval(5000);
    factory.setSessionTransacted(true);
    return factory;
}

}

and this, application.properties:而这个,application.properties:

java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
spring.jms.jndi-name=jms/MyCF
spring.jms.listener.concurrency=5
spring.jms.listener.acknowledge-mode=auto
spring.jms.template.receive-timeout=5000

spring.datasource.jndi-name=jdbc/MyDataSource

spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.generate-ddl=false

And I have MyEntity entity with sequence generator我有带有序列生成器的 MyEntity 实体

@Getter
@Setter
@Entity
@Table(name = "MY_ENTITY")
public class MyEntity {
@Id
@Column(name = "ID", nullable = false, precision = 0)
@SequenceGenerator(name="SqName", sequenceName="SQ_ID", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SqName")
private long id;
@Basic
@Column(name = "NAME", nullable = true, length = 4000)
private String name;

Then I have my JMS listener:然后我有我的 JMS 监听器:

@JmsListener(destination="jms/MyQueue")
public void onMessage(Message message) throws Exception {
    myService.save(myRepository.findOneByName("name1"));
}

Then I have a MyService class:然后我有一个 MyService class:

@Service
MyServiceImpl implements MyService {
    save(MyEntity myEntity) {
        if(myEntity == null) myEntity = new MyEntity();
        myEntity.setName("name1");
        // do something
        myRepository.save(myEntity);
    }
}

What happened that in one line and one thread newly created entity was saved in myRepository (locally, not yet in DB) and in the next line in a different thread when it called again save method, it didn't find an entity with name "name1" so at the end I had 2 records in the database with "name1".发生了什么,在一行和一个线程中,新创建的实体保存在 myRepository 中(本地,尚未保存在 DB 中),而在另一个线程中的下一行,当它再次调用 save 方法时,它没有找到名称为“的实体” name1”,所以最后我在数据库中有 2 条记录为“name1”。

myRepository.findOneByName("name1") = null;

So, should I add @Transactional annotation to save method in MyServicImpl class or to JmsListener?那么,我应该在 MyServicImpl class 或 JmsListener 中添加 @Transactional 注释以保存方法吗?

I mean, I had @Transactional in save method in MyServiceImpl class and it never happened but maybe it was just by coincidence.我的意思是,我在 MyServiceImpl class 的保存方法中有@Transactional,但它从未发生过,但也许只是巧合。

Also, should I add JtaTransactionManager in configuration?另外,我应该在配置中添加 JtaTransactionManager 吗?

this is concurrent change of the same name of the entitiy,consider the optimistic locking这是实体的同名并发更改,考虑乐观锁定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM