org.hibernate.HibernateException:没有Hibernate会话绑定到线程,配置不允许在这里创建非事务性的

时间:2015-06-16 14:14:48

标签: spring hibernate transactions

我在hibernate中遇到会话问题,下面是错误跟踪

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
at org.springframework.orm.hibernate3.SpringSessionContext.currentSession(SpringSessionContext.java:63)
at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:574)
at ptv.generic.dao.AbstractGenericSearchDaoHibernateImpl.findByQuery(AbstractGenericSearchDaoHibernateImpl.java:327)
at ptv.generic.dao.AbstractGenericSearchDaoHibernateImpl.findByQuery(AbstractGenericSearchDaoHibernateImpl.java:311)
at ptv.drm.dao.impl.RightsProfileHibernateDao.findByLookupName(RightsProfileHibernateDao.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:301)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy50.findByLookupName(Unknown Source)
at ptv.media.ingest.processor.CmsXMLIngestUpdateServiceImpl.verifyProfilesRightsRelation(CmsXMLIngestUpdateServiceImpl.java:1104)
at ptv.media.ingest.processor.CmsXMLIngestUpdateServiceImpl.addNewMediaContent(CmsXMLIngestUpdateServiceImpl.java:659)
at ptv.media.ingest.processor.CmsXMLIngestUpdateServiceImpl.insert(CmsXMLIngestUpdateServiceImpl.java:296)
at ptv.media.ingest.processor.AbstractCmsXMLIngestFileStrategy.insertNewMediaContent(AbstractCmsXMLIngestFileStrategy.java:251)
at ptv.media.ingest.processor.CmsXMLIngestFileUnencodedStrategy.insert(CmsXMLIngestFileUnencodedStrategy.java:74)
at ptv.media.autoingest.service.AutoIngestServiceImpl.ingestObtainedFiles(AutoIngestServiceImpl.java:1661)
at ptv.media.autoingest.service.AutoIngestServiceImpl.process(AutoIngestServiceImpl.java:138)
at ptv.media.TestAutoIngest.main(TestAutoIngest.java:19)

的applicationContext

<bean id="drmDaoProxyCreator"
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <description>Automatically wraps all the specified bean(s) with a
            transaction layer
        </description>
        <property name="proxyTargetClass" value="false" />
        <property name="beanNames">
            <list>
                <value>rightsProfileDAO</value>

            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>drmTxInterceptor</value>
            </list>
        </property>
    </bean>


    <bean id="drmTransactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <description>DRM transaction manager</description>
      <property name="sessionFactory" ref="drmOwnerSessionFactory" /> 
     </bean>

     <bean id="drmTxInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <description>DRM transaction interceptor</description>
        <property name="transactionManager" ref="drmTransactionManager" />
        <property name="transactionAttributeSource">
            <bean
                class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
        </property>
    </bean>

    <bean id="rightsProfileDAO" class="ptv.drm.dao.impl.RightsProfileHibernateDao">
        <description>Data access object for accessing RIGHTS_PROFILE table
        </description>
        <property name="sessionFactory" ref="drmOwnerSessionFactory" />
    </bean>

RightsProfileHibernateDao

/**
     * {@inheritDoc}
     */
    @Override
    public RightsProfile findByLookupName(final String lookupName) {
        final List<RightsProfile> result = findByQuery("from RightsProfile where lookupName = :lookupName", new String[] { "lookupName" },
                new Object[] { lookupName });
        return CollectionUtils.isEmpty(result) ? null : result.get(0);
    }

主要方法

   @Transactional
        private Pair<String, Boolean> ingestObtainedFiles(final Integer orgnId) {

            try {
                folder = ftpAccountService.getMonitoringFolderByOrgnId(orgnId);
                final List<File> clientsXMLFiles = scanFTPFolderForXmlFiles(new File(
                        folder.getProcessingDirectory()));
                // Sort files, so they will be processed from oldest to newest.
                // Added for AutoIngestService to work properly.
                Collections.sort(clientsXMLFiles, new Comparator<File>() {
                    @Override
                    public int compare(final File file1, final File file2) {
                        return Long.valueOf(file1.lastModified()).compareTo(
                                file2.lastModified());
                    }
                });

                for (final File messageFile : clientsXMLFiles) {
                    LOGGER.debug("Processing file " + messageFile.getAbsolutePath());

                    final Assets assets = getIngestXMLUnMarshaler(messageFile);
                    // process it. Intercept it so we begin transaction.
                    getCmsXMLIngestFileUnencodedStrategy().insert(assets, folder, messageFile);

                }
            } catch (final Exception e) {
                LOGGER.error("Error when ingesting files" ,e);
            }
            return new Pair<String, Boolean>(folder.toString(), true);


      }

我已经尝试将@Transactional注释添加到服务中,几乎到处都是dao。

1 个答案:

答案 0 :(得分:1)

通过添加Annotation based Transaction Management在Spring配置文件中启用<tx:annotation-driven/>

Here是一个很好的链接,您可以查看。