在一个Service调用另一个类中的方法,引起Hibernate session关闭:CmsService.java---------------------@Service("cmsService")
public class CmsService extends SystemService implements ICmsService {
    public String getSplitButtonMenuBar(User user) {        ....        dropdownMenuItems[i] = MenuParser.getDropdownMenuItem(url,"container", item);---------------------MenuParser.java---------------------public class MenuParser extends AbstractParser {    public static String getDropdownMenuItem(String url, String target,String item) {
        ...    }---------------------
当运行至红色代码部分,引起[org.hibernate.impl.SessionFactoryImpl] - closing错误。
static 方法中,只是进行了字串处理,是一个工具类,不需要Session才是正常的。但Hibernate session从Service层调用这个工具类,Sesseion却自动关闭了。
什么原因呢?

解决方案 »

  1.   

    你应该是在调用这个方法之前有数据库的操作,方法调用之后又有数据库的操作吧。
    如果是这样的话把这个service类加上@Transactional注释统一将这个类里的所有方法都纳入事务管理器管理
      

  2.   

    1.这个服务已经有@Service注释,否则就不能进行数据库操。
    2.就是在调用这个静态方法,Hibernate Session就自动关闭了。
    3.这个方法是在一个循环中实现的,访问了一个实体类Entity,Entity要进行Lazy loading一个父对象。
      

  3.   

    @Service注释只是声明这个类交给Spring管理并没有纳入事务管理
    @Transactional才是声明将这个类纳入事务管理
      

  4.   

     public static String getDropdownMenuItem(String url, String target,String item) {
            ...
            for (int i = 0; i < menuList.size(); i++) {
    System.out.println("i: " + i);
    Menu o = menuList.get(i);
    url = o.getMenuUrl();
    item = o.getMenuNameCn();
            dropdownMenuItems[i] = MenuParser.getDropdownMenuItem(url,"container", item);
            }
            ...    }实际上是在一个循环中调用这个静态类的一个方法,进行字串处理。
      

  5.   

    我用的是AOP来管理事务。
    ---------------
    <!-- 事务配置 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

        <aop:aspectj-autoproxy expose-proxy="true"/>

        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="save*" propagation="REQUIRED" />
                <tx:method name="add*" propagation="REQUIRED" />
                <tx:method name="update*" propagation="REQUIRED" />
                <tx:method name="del*" propagation="REQUIRED" />
                <tx:method name="remove*" propagation="REQUIRED" />
                <tx:method name="put*" propagation="REQUIRED" />
                <tx:method name="use*" propagation="REQUIRED"/>
                <tx:method name="get*" propagation="REQUIRED" read-only="true" />
                <tx:method name="count*" propagation="REQUIRED" read-only="true" />
                <tx:method name="find*" propagation="REQUIRED" read-only="true" />
                <tx:method name="list*" propagation="REQUIRED" read-only="true" />
                <tx:method name="*" propagation="REQUIRED" read-only="true" />
            </tx:attributes>
        </tx:advice>
        
        <aop:config expose-proxy="true">
            <aop:pointcut id="txPointcut" expression="execution(* com.weaforce..service..*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
        </aop:config>
    -------------------
      

  6.   

    为什么Hibernate Session在@Service层之外,就失效关闭了呢?