Announcement Announcement Module
Collapse
No announcement yet.
A prototype Quartz job causes Hibernate Lazy Initialization Exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • A prototype Quartz job causes Hibernate Lazy Initialization Exception

    Hi All,

    Although LIE is quite popular issue of Hibernate, Spring, I could not solve my problem when I try to proxy a prototype Quartz job object.
    The environment is Spring 2.5.2 + Hibernate 3.0 + Quartz 1.8.6, AS WebSphere 7.0.0.25 + MS SQL 2008 R2.

    I don't have many experiences of Hibernate Interceptor and I just followed some examples from Internet to configure it. Maybe there are some configuration error.
    Hope someone could help me to figure it out.

    The applicationContext.xml
    Code:
        <bean id="sessionFactory"
              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource">
                <ref bean="dataSource"/>
            </property>
            <property name="configLocation">
                <value>classpath:hibernate.cfg.xml</value>
            </property>
            <property name="cacheProvider">
                <ref local="cacheProvider"/>
            </property>
        </bean>
    
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                <ref local="sessionFactory"/>
            </property>
        </bean>
        
        <bean id="proviewHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory" ref="proviewSessionFactory"></property>
        </bean>
    	
        <bean id="pvimHibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
            <property name="sessionFactory" ref="sessionFactory"></property>
            <property name="cacheQueries" ><value>true</value></property>
        </bean>
    
        <bean id="eventHandlingJobExecutor" class="com.job.EventHandlingJobExecutor" scope="prototype">
            <constructor-arg value="event_handler" />
            <property name="quartzExpression">
                <value>0 0/1 * * * ?</value>
            </property>
            <property name="proviewHibernateTemplate">
                <ref bean="proviewHibernateTemplate"/>
            </property>
            <property name="pvimHibernateTemplate">
                <ref bean="pvimHibernateTemplate"/>
            </property>
            <property name="pventService">
                <ref bean="pvEventService"/>
            </property>
            <property name="itcClientManager">
                <ref bean="itcClientManager"/>
            </property>
            <property name="schedulerManager">
                <ref bean="schedulerManager"/>
            </property>
            <property name="systemVariableManager">
                <ref bean="systemVariableManager"/>
            </property>
            <property name="eventFailureResultManager">
                <ref bean="eventFailureResultManager"/>
            </property>
         </bean>
    
         <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager">
                <ref bean="transactionManager" />
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    
        <bean id="proxyJobExecutor" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyInterfaces">
                <list>
                    <value>com.job.JobExecutor</value>
                    <value>com.itc.event.IPvEventService</value>
                    <value>com.ticket.HierarchicalTicketManager</value>
                </list>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>transactionInterceptor</value>
                </list>
            </property>
        </bean>
    Inside the job executor implementation of execute method, it looks as following.
    Code:
            final List<EventJobMapping> result = pvimHibernateTemplate.executeFind(new HibernateCallback() {
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    List<EventJobMapping> mapping = session.createQuery(
                            " from EventJobMapping t where t.jobId = '" + execId + "'").setCacheable(false).list();
                    return mapping;
                }
            });
    
            proviewHibernateTemplate.execute(new HibernateCallback() {
                public Object doInHibernate(org.hibernate.Session session) throws HibernateException, SQLException {
                    Query q1 = session.createQuery("from Pmeventcache t where t.deviceId in " + _tempSQL
                                + " order by t.deviceId , t.agentTimestamp");
                    q1.setMaxResults(HANDLING_COUNT_EACH_TIME);
                    List<Pmeventcache> eventCacheList = q1.list();
                    for (Pmeventcache event : eventCacheList) {
                        PvEvent pvEvent = createPvEvent(event);
                        pventService.eventService(pvEvent);
                    }
                }
                return null;
            });
    
           result.clear();
    However, I got LIE as following.
    Code:
    2013-08-02 14:16:00 [PersistenceQuartzScheduler_Worker-2] ERROR [com.scheduler.TasmanTask] Error when executing scheduler
    org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.user.domain.User.roles, no session or session was closed
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
    	at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
    	at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:343)
    	at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
    	at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:163)
    	at com.util.SLMUtil.getPermissions(SLMUtil.java:94)
    	at com.ticket.impl.TicketManagerImpl.checkPrivilege(TicketManagerImpl.java:2634)
    	at com.ticket.impl.TicketManagerImpl.closeTicket(TicketManagerImpl.java:1306)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    	at java.lang.reflect.Method.invoke(Method.java:611)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
    	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    	at $Proxy22.closeTicket(Unknown Source)
    	at com.ticket.impl.HierarchicalTicketManagerImpl.closeTicket(HierarchicalTicketManagerImpl.java:142)
    	at com.ticket.impl.HierarchicalTicketManagerImpl.autoClose(HierarchicalTicketManagerImpl.java:694)
    	at com.ticket.impl.HierarchicalTicketManagerImpl.dealRuleAudit(HierarchicalTicketManagerImpl.java:398)
    	at com.job.BizRuleJobExecutor.doJob(BizRuleJobExecutor.java:181)
    	at com.job.BizRuleJobExecutor.executeTicketRuleAudit(BizRuleJobExecutor.java:113)
    	at com.job.BizRuleJobExecutor.execute(BizRuleJobExecutor.java:54)
    	at com.scheduler.TasmanTask.execute(TasmanTask.java:40)
    	at sun.reflect.GeneratedMethodAccessor235.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
    	at java.lang.reflect.Method.invoke(Method.java:611)
    	at com.monitoring.base.scheduler.util.Reflections.invoke(Reflections.java:39)
    	at com.monitoring.base.scheduler.util.Reflections.invokeAndWrap(Reflections.java:68)
    	at com.monitoring.base.scheduler.async.AsynchronousInvocation$1.process(AsynchronousInvocation.java:60)
    	at com.monitoring.base.scheduler.async.Asynchronous$ContextualAsynchronousRequest.run(Asynchronous.java:52)
    	at com.monitoring.base.scheduler.async.AsynchronousInvocation.execute(AsynchronousInvocation.java:52)
    	at com.monitoring.base.scheduler.spi.quartz.QuartzJob.execute(QuartzJob.java:242)
    	at org.quartz.core.JobRunShell.run(JobRunShell.java:223)
    	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:549)
    Thanks a lot.
Working...
X