多线程环境中的Spring Hibernate SessionFactory.getCurrentSession

时间:2015-01-07 20:00:44

标签: multithreading spring hibernate sessionfactory

我编写了一个批处理应用程序,它生成多个线程来读取分配的文件并将记录保存到数据库。该架构使用Spring上下文和Hibernate。

事务由Spring管理,我使用SessionFactory.getCurrentSession来获取会话以对每个线程执行保存操作。

考虑到我有一个处理get,save,update操作的通用DAO和隐藏Hibernate实现的Facade,如何确保调用SessionFactory.getCurrentSession()时两个线程获取其专用Session对象来执行DB操作

我在StackOverflow中发现了一个帖子,有人建议在使用spring管理事务时不要使用current_session_context_class=thread。 Spring为current_session_context_class属性使用的默认实现是什么?

提前致谢!

1 个答案:

答案 0 :(得分:1)

从Spring 2.0开始,Spring通过自己的hibernate提供的CurrentSessionContext接口实现与hibernate集成。

默认情况下,spring将此设置为SpringSessionContext以正确集成。一般来说,除非你使用JTA,否则你不想要或者需要弄乱current_session_context_class(尽管当使用Hibernate 4和最近的Spring版本时它应该也可以工作)。