在我们的一个项目中,我们遇到了一个问题:Spring忽略@Transactional
注释,然后失败,并出现以下错误。
启动ApplicationContext时出错。要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。2018-09-13 15:05:18,406错误[main] org.springframework.boot.SpringApplication应用程序运行失败org.springframework.dao.InvalidDataAccessApiUsageException:没有用于当前线程的具有实际事务的EntityManager-无法可靠地处理“删除”调用;嵌套的异常是javax.persistence.TransactionRequiredException:没有可用于当前线程进行实际事务处理的EntityManager-无法可靠地处理com.my.service.CacheAService.deleteShortTermCache(CacheAService.java:70)的“删除”调用。〜[classes /:na]
我发现了类似的问题,但没有解决方案适用于此案例。
@EnableTransactionManagement
存在当我用注释CacheService
时@Transactional
,一切都会再次起作用。但是我试图理解为什么Spring会忽略@Transactional
它CacheAService
。
我尝试记录Spring的事务拦截器,但没有提及CacheA
。这是唯一被记录的相关内容。
2018-09-13 15:05:18,242 TRACE [main] org.springframework.transaction.interceptor.TransactionInterceptor不需要为[org.springframework.data.jpa.repository.support.SimpleJpaRepository.deleteByValidity]创建事务方法不是事务性的。
这是简化的代码。在Spring的应用程序启动期间调用代码ContextRefreshedEvent
。
@Service
public class CacheService implements Cache {
@Autowired
private CacheA cacheAService;
@Autowired
private CacheB cacheBService;
@Override
public void clearCache() {
cacheAService.deleteShortTermCache();
cacheBService.deleteAll();
}
}
public interface CacheA {
void deleteShortTermCache();
}
@Service
@Transactional(readOnly = true)
public class CacheAService implements CacheA {
@Autowired
private CacheARepository cacheARepository;
@Override
@Transactional
public void deleteShortTermCache() {
cacheARepository.deleteByValidity(CacheValidity.SHORT_TERM);
}
}
public interface CacheB {
void deleteAll();
}
@Service
@Transactional(readOnly = true)
public class CacheBService implements CacheB {
@Autowired
private CacheBRepository cacheBRepository;
@Override
@Transactional
public void deleteAll {
cacheBRepository.deleteAll();
}
}
public enum CacheValidity {
SHORT_TERM,
LONG_TERM
}
@Repository
public interface CacheARepository extends JpaRepository<CacheItem, Integer> {
void deleteByValidity(CacheValidity validity);
}
public enum CacheItemKey {
AVAILABLE,
FUTURE,
AVAILABLE_UTM,
FUTURE_UTM,
REGION
}
@Entity
@Table(name = "cache_item")
public class CacheItem {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cache_item_id_seq")
@SequenceGenerator(name = "cache_item_id_seq", sequenceName = "cache_item_id_seq", allocationSize = 1)
private Integer id;
@Column(nullable = false, unique = true)
@Enumerated(EnumType.STRING)
private CacheItemKey key;
@Column(nullable = false)
private String value;
@Column(name = "date_modified", nullable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date dateModified;
@Column(nullable = false)
@Enumerated(EnumType.STRING)
private CacheValidity validity;
public Integer getId() {
return id;
}
public void setId(final Integer id) {
this.id = id;
}
public CacheItemKey getKey() {
return key;
}
public void setKey(final CacheItemKey key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
public Date getDateModified() {
return dateModified;
}
public void setDateModified(final Date dateModified) {
this.dateModified = dateModified;
}
public CacheValidity getValidity() {
return validity;
}
public void setValidity(final CacheValidity validity) {
this.validity = validity;
}
}
编辑:经过一些挖掘,我在日志中找到了这个。
2018-09-14 06:24:11,174信息[localhost-startStop-1] org.springframework.context.support.PostProcessorRegistrationDelegate $ BeanPostProcessorChecker类型为[com.my.service.CacheAService]的Bean'cacheAService'不符合获得处理的条件由所有BeanPostProcessor提供(例如:不符合自动代理资格)
我们发现此问题是由Spring Boot的自动配置引起的。由于自动配置已经设置了事务管理,因此我们的自定义配置@EnableTransactionManagement
中断了事务顾问的实例化。@EnableTransactionManagement
从我们的配置中删除可解决此问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句