Spring.NET、Quartz 和 Spring.NET交易
我刚刚遇到了通过 Spring 调用的 Quartz 作业的问题。我的 ExecuteInternal 方法有一个 [Transaction] 属性(因为它执行大量数据库调用),但是当它运行时,我收到“No NHibernate sessionbound to thread”错误。
只是想知道这是否是因为 Spring.NET 不支持 Quartz 对象中的 [Transaction]
属性?
如果没有,那很好......我可以手动启动事务,但想检查情况是否如此,而不是我的配置中的某个地方出现愚蠢的错误。
[更新] 我实际上已经弄清楚了。在 API 文档中,它说最好的方法是在服务层上使用事务。我的工作是使用 DAO 来完成其工作,但我的事务位于我的服务层上,因此我只是从工作中调用服务方法来执行相同的工作(保存、更新记录等),因为它们已经存在。
它还建议,如果您为 SchedulerFactoryObject 提供 DbProvider,则可以在作业本身中使用事务,但是当我这样做时,它似乎想要找到在数据库中的特殊表中配置的触发器(我尚未设置因为我的触发器都是 XML 格式的)但这可能是另一种方法。
不过,调用服务方法对我来说效果很好。
I've just run into a problem with a Quartz job that I'm invoking through Spring. My ExecuteInternal
method had a [Transaction]
attribute (because it does a load of DB calls) but when it runs, I get the 'No NHibernate session bound to thread' error.
Just wondering if that's because Spring.NET doesn't support the [Transaction]
attribute in Quartz objects?
If not, that's fine... I can start a transaction manually, but wanted to check that it was the case, and not a silly error in my config somewhere.
[Update]
I figured it out actually. In the API docs, it says the preferable way to do this is use transactions on the service layer. My job was using DAOs to do its work, but my transactions are on my service layer, so I just called service methods from my job instead to do the same work (saving, updating records etc) since they already existed.
It also suggests that if you give the SchedulerFactoryObject a DbProvider, you can use transactions in the job itself, but when I did that it seemed to want to find my triggers configured in a special table in the DB (which I haven't set up since my triggers are all in XML) but that's possibly another way to do it.
Calling service methods works fine for me though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事务属性使用aop 来工作。 Spring.NET 为装饰对象创建一个 aop 代理。该代理创建会话并启动事务。
在 ExecuteInternal 方法中,您不会在代理上调用该方法,而是在目标本身上调用该方法。因此 spring 无法拦截调用并执行其事务魔法。
您的服务被注入,因此事务属性适用于它们。
关于这个主题,spring 文档中有一个很好的解释: http ://www.springframework.net/doc-latest/reference/html/transaction.html#tx-understandingimpl
The transaction attribute works using aop. Spring.NET creates an aop proxy for the decorated object. This proxy creates the session and starts the transaction.
In the
ExecuteInternal
method, you don't call the method on a proxy, but on the target itself. Therefore spring cannot intercept the call and do its transaction magic.Your services are injected and therefore the transaction attribute works for them.
There's a good explanation in the spring docs on this subject: http://www.springframework.net/doc-latest/reference/html/transaction.html#tx-understandingimpl