java - @Scheduled 和 @Transactional 方法中的 Spring-data-jpa 延迟加载
问题描述
在我的配置类中,我需要将方法作为 cronjob 运行。所以我使用@Scheduled
注解创建了一个方法。
@Scheduled(initialDelay = 10 * 1000, fixedRate = 1000 * 1000)
public void ThemeUpdate() {
List<ThemeIndex> indices = getServices();
...
}
该ThemeUpdate()
方法现在在它自己的线程中运行,我将丢失我的事务。所以我使用@Transactional
注释创建了另一种方法。
@Transactional
public List<ThemeIndex> getServices() {
List<Service> services = serviceRepository.findServices();
Section section = services.get(0).getSections().iterator().next();
return null;
}
我List<Service> services
从我的serviceRepository
. 但是,如果我想访问通过延迟加载获取的 a Section
,Entity
为什么我会得到 a LazyInitializationException
?
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.example.myPorject.db.model.Service.sections, could not initialize proxy - no Session
我在这里想念什么?
编辑:
预定:
@Scheduled(initialDelay = 10 * 1000, fixedRate = 10000 * 1000)
@Transactional
public void ThemeUpdate() {
List<ThemeIndex> indices = themeUpdateServiceImpl.getIndices();
}
获取索引():
@Override
public List<ThemeIndex> getIndices() {
return getIndices(serviceRepository
.findServices());
}
@Override
public List<ThemeIndex> getIndices(List<Service> services) {
return themeIndexServiceImpl.getThemeIndexes(services);
}
获取主题索引():
@Override
public List<ThemeIndex> getThemeIndexes(List<Service> services) {
List<ThemeIndex> themeIndexs = new ArrayList<>();
for (Service s : services) {
ThemeIndex themeIndex = getThemeIndex(s);
if (themeIndex != null) {
themeIndexs.add(themeIndex);
}
}
return themeIndexs;
}
@Override
public ThemeIndex getThemeIndex(Service service) {
//SQL which is slow
if (serviceRepository.isEpisService(service.getSvno())) {
...
}
解决方案
您在本地调用 getServices() 因此本地方法调用没有事务代理。
您应该将计划的方法移动到其自己的组件中,并使用 getServices() 方法注入该组件。
推荐阅读
- ruby-on-rails - Heroku 应用程序不会部署并且不断崩溃不确定它是否正在使用我的 procfile
- cassandra - 无法使用 cassandra 帐户创建角色
- api - 是否可以让每个 MediaWiki 修订页面都独一无二?
- r - 我遇到了行名中包含 .z 的问题。(不允许绘制)
- python - 用 quiver 和 meshgrid 绘制离散 3D 矢量场
- iis - IIS 出现问题,我在另一台计算机上看不到我的网站
- sql - 格式化时间戳
- angular - 路由器插座无法呈现特定组件内容
- python - 是否可以在 matplotlib/seaborn 中将 svg 宽度和高度设置为 100%
- azure - 我想拆分字符串并作为复杂的 json 字符串传递以使用逻辑应用程序形成自定义字段