在尝试解决以下问题时,过去几天白发的数量急剧增加。我在使用简单 Spring 3.2 事件机制的自定义事件侦听器中使用 Spring Data JPA 存储库。我遇到的问题是,如果ListenerA
创建一个实体并调用assetRepository.save(entity)
或assetRepository.saveAndFlash(entity)
后续调用以从另一个侦听器检索同一实体,则会失败。原因似乎是ListenerB
在数据库中找不到原始实体,它似乎仍在Hibernate的缓存中。ListenerB 锁定实体的触发器是由于线程池中的可运行任务执行而触发的事件。这是我的配置:
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="spring-jpa" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="false" />
<property name="database" value="#{appProps.database}" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.hbm2ddl.auto">#{appProps['hibernate.hbm2ddl.auto']}</prop>
<prop key="hibernate.show_sql">#{appProps['hibernate.show_sql']}</prop>
<prop key="hibernate.format_sql">#{appProps['hibernate.format_sql']}</prop>
<prop key="hibernate.search.default.directory_provider">org.hibernate.search.store.impl.FSDirectoryProvider</prop>
<prop key="hibernate.search.default.indexBase">#{appProps.indexLocation}</prop>
<prop key="hibernate.search.lucene_version">#{appProps['hibernate.search.lucene_version']}</prop>
</props>
</property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
我省略了定义连接到 Oracle 数据库dataSource
的实例的配置。ComboPooledDataSource
附带说明一下,使用了组件扫描,并且项目是 Spring MVC。现在是 Java 类。
听众A
@Sevice
public class ListenerA implements ApplicationListener<FileUploadedEvent> {
@Autowired
private AssetRepository assetRepository;
@Autowired
private ExecutorService executor; // Triggers runnable task on a Job in Spring's TaskExecutor
@Override
@Transactional
public void onApplicationEvent(FileUploadedEvent event) {
Asset target = event.getTarget();
Job job = new Job(target);
assetRepository.save(job);
executor.execute(job);
}
听者B
@Sevice
public class ListenerB implements ApplicationListener<JobStartedEvent> {
@Autowired
private AssetRepository assetRepository;
@Override
@Transactional
public void onApplicationEvent(JobStartedEvent event) {
String id = event.getJobId();
Job job = assetRepository.findOne(id); // at this point we can not find the job, returns null
job.setStartTime(new DateTime());
job.setStatus(Status.PROCESSING);
assetRepository.save(job);
}
JobStartedEvent
从TaskExecutor
. 我在这里做错了什么?我曾尝试使用具有事务意识的自定义事件发布者,但这似乎并不能解决问题。我还尝试连接适当的服务而不是数据存储库,并@Transactional
从侦听器中删除注释,这也失败了。欢迎任何关于如何解决问题的合理建议。