我有一个基于 Spring 4.0 和 Hibernate 4 的项目,特别是 Spring MVC。
Hibernate 的 session 由 OpenSessionInViewFilter
为 Controller 中的每个请求创建。
现在,我正在尝试在 Controller 的方法中启动一个新线程(以执行一个漫长的过程)。显然,OpenSessionInViewFilter 在请求完成后关闭 session 。然后,当我的线程启动时,不再有 session ,我收到此错误:
org.hibernate.HibernateException:找不到当前线程的 session
这是类的基本结构,从 Controller 到我的 Callable 组件。 IReportService 扩展了 Callable。
OBS:我已经尝试使用 spring 的 @Async
注释,但它仍然无法正常工作。我将 REQUIRES_NEW
放在 Service 上以尝试获取新事务,但即使更改为 NESTED 也失败了。
@Controller
@RequestMapping(value = "/action/report")
@Transactional(propagation = Propagation.REQUIRED)
public class ReportController {
@Autowired
private IReportService service;
private final Map<Long, Future> tasks = new HashMap();
@RequestMapping(value = "/build")
public String build(@RequestParam Long id) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<StatusProcesso> future = executor.submit(service);
tasks.put(id, future);
return "wait-view";
}
@RequestMapping(value = "/check", method = RequestMethod.GET)
public @ResponseBody Map<String, Object> check(@RequestParam Long id) {
String status = null;
try {
Future foo = this.processos.get(id);
status = foo.isDone() ? "complete" : "building";
} catch (Exception e) {
status = "failed";
}
return new ResponseBuilder()
.add("status", status)
.toSuccessResponse();
}
// Another operations...
}
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class ReportService implements IReportService {
@Autowired
private IReportDAO dao;
@Override
public Status call() {
Status status = new Status();
// do long operation and use DAO...
return status;
}
}
最佳答案
您不能在不同的线程中使用相同的 hibernate session - 它不是线程安全的。所以你不应该担心 OpenSessionInViewFilter,即使它不会关闭 session ,它们仍然无法从其他线程使用(并且隐式延迟加载将使其绝对不可预测)。
session 绑定(bind)到线程 - 当您尝试从另一个线程访问它时,问题不是不再有 session ,而是因为 session 从未存在过。
您可以将@Transactional
与服务方法一起使用,然后从其他线程中的长时间运行的进程中调用这些方法。
P. S. 还要避免使用 (propagation = Propagation.REQUIRES_NEW)
它们用于非常罕见的情况,当您需要回滚内部事务并且您的数据库(它应该是一个非常复杂的 RDBMS)支持这种行为时 - 它是而不是复杂的事务脚本架构,而不是经典的 POJO 驱动的 Spring 应用程序。
更新 对于那些认为 hibernate session 是线程安全的非信徒 - 来自 Hibernate Session java doc 的引述:
It is not intended that implementors be threadsafe. Instead each thread/transaction should obtain its own instance from a SessionFactory.
有人发布了关于 hibernate session long conversations 的答案 - 是的,这个东西存在,但它只能在单线程环境中工作(比如带有事件循环的 SWT 应用程序) - 所以你可以在桌面应用程序中使用单个 session ,因为它使用单线程来处理用户输入,但它永远不会在服务器环境中工作。
其他一些答案警告您有关从不同线程访问同一 session
另请注意著名的Java Persistence with Hibernate警告您不要在不同的线程中使用相同的 session (与 session 工厂对比):
在 Hibernate p 的上下文中。 56:
In most Hibernate applications, the SessionFactory should be instantiated once during application initialization. The single instance should then be used by all code in a particular process, and any Session should be created using this single SessionFactory. The SessionFactory is thread-safe and can be shared; a Session is a single-threaded object.
在 JPA p 的上下文中。 74:
javax.persistence.EntityManagerFactory—The equivalent to a Hibernate SessionFactory. This runtime object represents a particular persistence unit. It’s thread-safe, is usually handled as a singleton, and provides methods for the creation of EntityManager instances.
javax.persistence.EntityManager—The equivalent to a Hibernate Session. This single-threaded, nonshared object represents a
particular unit of work for data access.
关于java - Spring - 使用 OpenSessionInViewFilter 为新线程提供 Hibernate session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31439644/