[英]NHibernate GenericADOException
sesion.Get(id); session.Get(id); produces sometimes a GenericADOException:有时会产生 GenericADOException:
could not load an entity: [PFC__ Servidor.Entidades.Usuario#10][SQL: SELECT usuario0 .us_id as us1_1_0_, usuario0_.us_email as us2_1_0_, usuario0_.us_contrasena as us3_1_0_ FROM public.usuario usuario0_ WHERE usuario0_.us_id=?]无法加载实体:[PFC__ Servidor.Entidades.Usuario #10][SQL: SELECT usuario0 .us_id as us1_1_0_, usuario0_.us_email as us2_1_0_, usuario0_.us_contrasena as us3_1_0_ FROM usuario_ usuario0_?]
Any known where is the problem?任何已知的问题出在哪里? Why not always fails?为什么不总是失败?
PD: Sorry for my english. PD:对不起我的英语。
ACT1: Inner exception: [System.NullReferenceException] "Object reference not set to an instance of an object" ACT1:内部异常:[System.NullReferenceException]“对象引用未设置为对象的实例”
Can you post the innerException?你可以发布innerException吗?
update更新
I also found this:我还发现了这个:
sometimes有时
Is your session shared among threads?您的 session 是否在线程之间共享? If so, death.如果是这样,死亡。 Sessions are not thread safe.会话不是线程安全的。
I had this issue when I started implementing the current framework.当我开始实施当前框架时,我遇到了这个问题。 It is in fact as Jason is suggesting - you need to set your NHibernate session scope to per-request.事实上,正如 Jason 所建议的那样 - 您需要将 NHibernate session scope 设置为每个请求。 This means a new session will be opened for each request and closed at end of request.这意味着将为每个请求打开一个新的 session 并在请求结束时关闭。 However, you only create the session factory once per application start since this is the most expensive part.但是,您只需在每个应用程序启动时创建一次 session 工厂,因为这是最昂贵的部分。
Are you using any dependency injection?您是否使用任何依赖注入? You can find great examples on how to achieve per-request session scoping on Windsor Castle NHibernate facility .您可以在温莎城堡 NHibernate 设施上找到有关如何实现按请求 session 范围界定的很好的示例。
If you are doing it manually however, you can open an NHibernate session in your Global.asax Request_begin and ensure it is closed and disposed in Request_End method.但是,如果您手动执行此操作,则可以在 Global.asax Request_begin 中打开 NHibernate session 并确保它在 Request_End 方法中关闭和处置。
A neat way to work with NHIbernate is:使用 NHIbernate 的一种简洁方法是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.