Thursday, July 7, 2011

AOP – Spring – JPA for background threads / jobs

[See this post in the new blog]

I've recently come up to a very wicked problem in Syncope, and a saving blog post pointed me in the right direction:

Getting your persistence access right when working with background jobs in Spring can be tricky. Most people rely on the Open Session In View pattern using Filters or Interceptors that act on the regular app server threads and close and open sessions for each request.

Nevertheless, I had to refactor a bit the source code to be JPA 2.0 (and not strictly Hibernate) compliant: the result is available here. I have also added some @Transactional support.

2 comments:

  1. You solved my problem. I spent the whole weekend and eventually I found your post and able to use entityManager in my DAO in Spring's quartz job. I have one query for you:

    Is that possible if instead of DAO class I can inject Service class that actually holds a reference to DAO class using this process? I tried this way but no success so I moved my DAO object from Service layer to job bean.

    ReplyDelete
  2. Hi Taimuri, glad to be useful :-)

    FYI, this blog moved a while ago to [1] (and this post is available at [2]).

    Regarding your question, there shouldn't be in principle any issue in doing what you describe above: only consider that the Service class must 1) autowire the reference to the DAO class 2) be declared as Spring bean and autowired in the Service class.

    [1] http://blog.tirasa.net/blogs/index.php/ilgrosso
    [2] http://blog.tirasa.net/blogs/index.php/ilgrosso/aop-n-spring-n-jpa

    ReplyDelete