RT,,没有配置任何事务,包括包括编程式和声明式的,请我我该手动去关闭session吗?假如要关,又该如何去关.我怕假如应该关,而我又没关,会导致session过多而使程序变慢甚至使服务器崩掉.

解决方案 »

  1.   

    在applicationContext.xml上定义:
    <!--定义代理方式是什么,在哪个类中进行事务代理,以什么方式进行事务控制 -->
    <bean id="baseProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" >
    <property name="transactionManager" ref="transactionManager" />
    <property name="target">
    <ref bean="baseDao"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="modify*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>在service层上实用代理(如在applicationContext_service.xml上):
    <bean id="departmentService" class="com.oneal.edusystem.service.impl.DepartmentServiceImpl">
    <property name="departmentDao" ref="departmentDao"/>
    </bean>
    <bean id="departmentProxy" parent="baseProxy">
    <property name="target">
    <ref bean="departmentService"/>
    </property>在action层上就可以用代理方式注入了(如在applicationContext_action.xml上):<bean id="departmentAction" class="com.oneal.edusystem.web.DepartmentAction">
    <property name="departmentService" ref="departmentProxy"/>
    <property name="excelService" ref="excelService"/>
    </bean>
      

  2.   

    回1楼,为了方便,我配置文件中并没有去配置事务,我想问的是,该不该手动去关闭session.
      

  3.   

    需要,你可以这么理解:session.close是把数据库连接放回连接池,如果使用Dao,hibernate会自动进行此操作
      

  4.   

    方法如下:
    public OperatingLog getOperatingLogById(String id){
    try {
    long iid = Integer.parseInt(id);
    OperatingLog ol = (OperatingLog) getHibernateTemplate().get(OperatingLog.class, iid);
    return ol;
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    如果需要,我该如何去关session呢?
      

  5.   

    在web.xml加入如下,spring就会自动为你关闭
    <filter>
    <filter-name>lazyLoadingFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
    <param-name>singleSession</param-name>
    <param-value>false</param-value> 
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
      

  6.   

    你这里应该属于Dao层的吧,在这里你不用去关它,在业务层时再进行事务配制,然后在action层以有事务管理的业务类来注入。
      

  7.   

    你不用关。那个getHibernateTemplate() 不是hibernate的,是spring的。
      

  8.   

    当初为了图方便,是直接通过action调用dao层的该方法的,事务都配置在业务层.而这个方法根本就没经过业务层.
      

  9.   

    在web.xml加入如下,spring就会自动为你关闭
    <filter>
    <filter-name>lazyLoadingFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    <init-param>
    <param-name>singleSession</param-name>
    <param-value>false</param-value>  
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
    这是一种好的方法,但有一个缺点,就是网络速度很慢的时候,如果页面还没显示完,就被用户关掉了,那这个session就没有关了。
      

  10.   

    还有,通过OpenSessionInViewFilter本身就会影响速度.