gpt4 book ai didi

java - 在 @Transactional 方法之前创建的 Hibernate 对象不能在之后使用(获取 LazyInitializationException)

转载 作者:行者123 更新时间:2023-12-01 12:42:47 25 4
gpt4 key购买 nike

我查看了很多有关 LazyInitializationException@Transactional 的其他问题,但没有看到任何似乎对我们的案例有帮助的内容。

对于没有系统地调查此事,我深表歉意。这不是我的代码,但它暂时落入我的手中......

我们的代码如下所示:

@Entity
class TaskSchedule {
// fields that make up a task schedule
Date nextRunTime;
}
<小时/>
@Entity
class Task {
@OneToMany(fetch = FetchType.LAZY)
List<TaskSchedule> schedules;

@Transactional
public void doTask() {
// run the task

for (TaskSchedule schedule : schedules) { /* CRASH HAPPENS HERE */
// set the schedule's next run time
}
}
}
<小时/>
@Component
public class TaskDao {
@Transactional(readOnly = true)
public List<Task> getScheduledTasks() {

Calendar calendar = Calendar.getInstance();

Criteria criteria = getSession().createCriteria(Task.class);
criteria.add(Restrictions.le("runTime", calendar.getTime()));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

List<Task> results = criteria.list();
return results;
}
}
<小时/>
public class MonitorJob {
TaskDao taskDao;

public void runScheduledTasks() {
// start a read only transaction
List<Task> tasks = taskDao.getScheduledTasks(); // query database
// finish the transaction

for (Task task : tasks) {
// start a transaction
task.doTask(); // populate lazy list
// finish transaction
}
}
}

当惰性列表 schedules 被迭代时,我们得到一个 LazyInitializationException 异常:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: Task.schedules, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentBag.iterator(PersistentBag.java:272)
at Task.doTask(Task.java:366)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:197)
at Task_$$_javassist_43.doTask(Task_$$_javassist_43.java)
at MonitorJob.runScheduledTasks(MonitorJob.java:106)
at sun.reflect.GeneratedMethodAccessor68.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:273)
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:299)
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:111)
at org.quartz.core.JobRunShell.run(JobRunShell.java:203)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:520)

据我所知,在为 tasks 中的第一项调用 @Transactional 方法 doTask 后,一大堆对象(包括 tasks 中剩余项目的所有其他延迟 schedules 列表)通过调用 unsetSession 来从 session 中删除。这意味着任务的后续迭代会崩溃,因为它们的惰性列表不再具有关联的 session 。

我实际上并没有研究过这段代码,但它最近开始失败(在我们的数据库重新加载之后)。如果对象从 session 中删除,我不明白为什么它首先会起作用。

最佳答案

好的,现在你已经得到了自调用。请阅读我之前发布的链接中的所有答案(请注意,这还取决于您是否使用 AOP 代理 - 但我假设您正在使用)。在当前的代码片段中,您需要使用 @Transactional 注释 runScheduledTasks() 并从 getScheduledTasks() 中删除 @Transactional,因为它是多余的,并且您不需要事务来读取数据库。另外,正如 Nyar Thar 提到的,您不需要使用 @Transactional 注释您的实体方法。

根据我的经验,@Transactional 注释通常用于服务层。如果您希望在需要创建始终打开新事务的服务方法时更新新事务中的每个任务。

public class TaskService {

@Transactional(propagation = REQUIRES_NEW)
public void updateTask(Task task) {
task.doTask();
}

}

然后在MonitorJob中

//in this case you don't need @Transactional here
public void runScheduledTasks() {
....
List<Task> tasks = getScheduledTasks();
for (Task task : tasks) {
taskService.updateTask(task);
}
}

关于java - 在 @Transactional 方法之前创建的 Hibernate 对象不能在之后使用(获取 LazyInitializationException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24944989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com