錯誤的內容是這樣

No Hibernate Session bound to thread,and configuration does not allow creation of non-transactional one here

在我的xml配置方面原本的方式 :

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
        <property name="url" value="jdbc:sqlserver://10.225.10.76:1433;DatabaseName=WWEIP" />
        <property name="username" value="sa" />
        <property name="password" value="eiptest" />
    </bean>
    
    <bean id="hibernateProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="properties">
            <props>
                <prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.transaction.flush_before_completion">false</prop>
                <prop key="hibernate.transaction.auto_close_session">true</prop>
                <prop key="hibernate.connection.release_mode">auto</prop>
            </props>
        </property>
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="mappingResources">
            <list>
                <value>com/wantwant/database/dao/Account.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <ref local="hibernateProperties" />
        </property>
    </bean>
    
    <bean id="loginFactory" class="com.wantwant.web.factory.impl.LoginFactory">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    
    <bean id="transactionInterceptor"
        class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="save*">PROPAGATION_REQUIRED</prop>
                <prop key="update*">PROPAGATION_REQUIRED</prop>
                <prop key="delete*">PROPAGATION_REQUIRED</prop>
                <prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <!-- default transaction type -->
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>

    <bean id="beanProxy"
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>*Service</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>

 

因為設定檔紅色部分*Service要透過Proxy來操作,但是我的SessionFactory的注入是以localFactory的項目裡面,

因此設定需要改變成*Factory。

以下是改後的xml設定檔

 

<bean id="beanProxy"
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>*Factory</value>
            </list>
        </property>
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value>
            </list>
        </property>
    </bean>

 

透過proxy之後此錯誤就可以解決。

 


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 werwolf 的頭像
    werwolf

    等待著真實的自由

    werwolf 發表在 痞客邦 留言(0) 人氣()