如何在创建时截获所有Hibernate会话(Spring / Grails环境) [英] How to intercept all Hibernate sessions when they're created (Spring / Grails environment)

查看:127
本文介绍了如何在创建时截获所有Hibernate会话(Spring / Grails环境)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建新Hibernate会话时是否有拦截所有新Hibernate会话的方法?我需要访问每个Session实例以启用带有参数的Hibernate过滤器。

我工作的唯一解决方案是包装SessionFactory,但这涉及到许多半讨厌的黑客行为,并且需要我实施大约60种方法,只有少数人感兴趣。

Hibernate的SessionFactory实现是针对最终声明的一些恼人的原因,所以扩展它不是一个选项。我也尝试过方面和Java代理没有任何运气。

解决方案

我解决了这个问题(至少在Hibernate为这样的事情提供一个合适的API之前)。短版本的解决方案:


  1. 为会话工厂代理

  2. 拦截方法调用getCurrentSession并使用我们初始化的CurrentSessionContext实现(而不是Hibernate)。

更长的版本:
http://www.developer-b.com/blog/entry / 1635/2010 / oct / 07 / intercepting-hibernate-sessions

来源/ Github:
http://github.com/multi-tenant/grails-hibernate-hijacker (仍然很实验)



感谢您的输入!

Is there a way of intercepting all new Hibernate sessions when they're created? I need to access each Session instance to enable a Hibernate filter with a parameter.

The only solution I've gotten working has involved wrapping the SessionFactory, but this involved a lot of semi nasty hacks as well as it required me to implement around 60 methods, where only a few are interesting.

Hibernate's SessionFactory implementation is for some annoying reason declared final so extending it is not an option. I've also tried aspects and Java proxies without any luck.

解决方案

I've solved this problem (at least until Hibernate provides a proper API for things like this). Short version of the solution:

  1. Proxy the session factory
  2. Intercept method invocations to getCurrentSession and use a CurrentSessionContext implementation we've initialized (not Hibernate).

Longer version: http://www.developer-b.com/blog/entry/1635/2010/oct/07/intercepting-hibernate-sessions

Sources / Github: http://github.com/multi-tenant/grails-hibernate-hijacker (still very experimental)

Thanks for the input!

这篇关于如何在创建时截获所有Hibernate会话(Spring / Grails环境)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆