开发者

Spring.NET, Quartz & Transactions

开发者 https://www.devze.com 2023-04-12 07:35 出处:网络
I\'ve just run into a problem w开发者_如何学编程ith 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 whe

I've just run into a problem w开发者_如何学编程ith 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号