Problem with spring quartz
java, quartz-scheduler, spring
Solution
For the second error ("could not execute the query"), I don't know and I'm really wondering what the session looks like.
In deed, AFAIK, the persistent context is not available to Quartz Jobs as nothing take care of establishing a Hibernate Session for them (Quartz runs outside the context of Servlets and the open session in view pattern doesn't apply here). This is why you get the first error ("No hibernate session bound to thread").
One solution for this is described in AOP – Spring – Hibernate Sessions for background threads / jobs. In this post, the author shows how you can use Spring AOP proxies to wire a hibernate interceptor that gives you access to the persistence context and it takes cares of closing and opening the sessions for you.
Didn't test it myself though, but it should work.
Problem
I'm trying to invoke method based on some interval time, here are some beans inside applicationContext.xml ``` <bean id="MngtTarget" class="com.management.engine.Implementation" abstract="false" lazy-init="true" autowire="default" dependency-check="default"> <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="MngtTarget" /> <property name="targetMethod" value="findItemByPIdEndDate"/> </bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail" ref="jobDetail" /> <!-- 10 seconds --> <property name="startDelay" value="10000" /> <!-- repeat every 50 seconds --> <property name="repeatInterval" value="20000" /> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="simpleTrigger" /> </list> </property> </bean> ``` Here is the method I'm trying to invoke : ``` public List<Long> I need findItemByPIdEndDate() throws Exception { List<Long> list = null; try{ Session session = sessionFactory.getCurrentSession(); Query query = session.getNamedQuery("endDateChecker"); list = query.list(); for(int i=0; i<list.size(); i++) { System.out.println(list.get(i)); } System.out.println("Total " + list.size()); }catch (HibernateException e){ throw new DataAccessException(e.getMessage()); } return list; } ``` Here is the exception message that I get : ``` Invocation of method 'findItemByPIdEndDate' on target class [class com.management.engine.Implementation] failed; nested exception is No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here ``` I've spent time googling alot so far also I've tried to modify my method like this : ``` public List<Long> I need findItemByPIdEndDate() throws Exception { List<Long> list = null; try{ Session session = sessionFactory.openSession(); Query query = session.getNamedQuery("endDateChecker"); list = query.list(); for(int i=0; i<list.size(); i++) { System.out.println(list.get(i)); } System.out.println("Total " + list.size()); session.close(); }catch (HibernateException e){ throw new DataAccessException(e.getMessage()); } return list; } ``` And I get different error msg, I get : `Invocation of method 'findItemByPIdEndDate' on target class [class com.management.engine.Implementation] failed; nested exception is could not execute query]` , anyone knows what is this all about, any suggestions ? thank you Also my queries.hbm.xml ``` <hibernate-mapping> <sql-query name="endDateChecker"> <return-scalar column="PId" type="java.lang.Long"/> <![CDATA[select item_pid as PId from item where end_date < trunc(sysdate)]]> </sql-query> </hibernate-mapping> ```