在 Spring 事务系统中使用 Hibernate 会话工厂映射
我正在升级运行 postgres 的现有系统。
以前的架构师认为,提高系统性能的最佳方法是每年将前几年的数据移动到数据库中的新模式,而不是简单地对主模式建立索引。 (说真的,零索引,9 个相同的模式)。
现在来解决这个问题。我去掉了他们正在使用的疯狂的数据库连接逻辑(看起来他们试图从头开始构建一个事务管理器)并用 Spring Transactions 替换它。所以现在我被迫处理这样的循环:
for(archive a : yearsArchived){
session s = sessionfactorymap.get(a).getcurrentsession();
(find data and copy to temporary table for report)
}
我可以很容易地为不同的模式创建各种会话工厂,问题是将它们关联到事务管理器,以便它们可以打开会话。当我转换所有内容时,我得到这个异常:(
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:685)
at parity.model.ReportThread.generate(ReportThread.java:47)
at parity.model.ReportThread.run(ReportThread.java:31)
是的,我知道,它是一个多线程Web应用程序,以前的开发人员是迟钝的)
在我正常的Spring事务类中,我不需要设置当前会话上下文,因为我假设它由 Spring 事务管理器完成。我到底该如何解决这个烂摊子?
i am doing an upgrade to an existing system running postgres.
the previous architect felt that the best way to get improved performance out of the system was after each year to move the previous years data to a new schema in the database rather than simply indexing the primary schema. (seriously, zero indexes, 9 identical schemas).
now for the problem. ive stripped out the insane database connection logic they were using (it looked like they were trying to build a transaction manager from scratch) and replaced it with Spring Transactions. so now im forced to deal with loops like this:
for(archive a : yearsArchived){
session s = sessionfactorymap.get(a).getcurrentsession();
(find data and copy to temporary table for report)
}
i can create the various session factories for the different schemas easy enough, the problem is associating them to a transaction manager so they can open the session. when i have everything converted i get this exception:
org.hibernate.HibernateException: No CurrentSessionContext configured!
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:685)
at parity.model.ReportThread.generate(ReportThread.java:47)
at parity.model.ReportThread.run(ReportThread.java:31)
(yes, i know, it is a multi-threaded web app, previous developers were retards)
in my normal spring transaction classes i don't need to set a currentsession context because im assuming its done by the spring transaction manager. how in the world do i fix this mess?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Spring 的 HibernateTransactionManager 不支持开箱即用的情况,因为它只能管理单个 SessionFactory 。
因此,您可以为不同的会话工厂使用不同的事务管理器,但它们不能参与同一事务。如果可以的话,请参阅 10.5.6.2 使用 @Transactional 的多个事务管理器。
否则,也许让
SessionFactories
参与单个事务的最简单方法是使用JTATransactionManager
和一些独立的 JTA 实现(Atomikos, Bitronix, <一href="http://jotm.ow2.org/xwiki/bin/view/Main/WebHome" rel="nofollow">JOTM)。另外,请注意单次优化的概念。我认为 JTA 实现应该能够将其应用于您的情况,以减少 JTA 引入的开销。
Spring's
HibernateTransactionManager
doesn't support your case out of the box, because it can only manage a singleSessionFactory
.So, you can have different transaction managers for different session factories, but then they cannot participate in the same transaction. If it's okay, see 10.5.6.2 Multiple Transaction Managers with @Transactional.
Otherwise, perhaps the easiest way to make your
SessionFactories
participate in a single transaction is to useJTATransactionManager
with some standalone JTA implementation (Atomikos, Bitronix, JOTM).Also, note the concept of 1PC Optimization. I think JTA implementation should be able to apply it in your case in order to reduce overhead introduced by JTA.
谢谢你们的信息。我最终通过将这些会话工厂与事务管理器分离并手动管理事务来解决这个问题。多一点努力,但它有效。
谢谢
thanks for the info guys. i ended up solving it by detaching those sessionfactories from the transaction manager and managing the transactions manually. bit more effort but it works.
thanks