ssh框架采用了事务控制,用spring来管理hibernate的session,从而导致了很多问题,配置文件如下
web.xml
  <filter>
    <filter-name>hibernateFilter</filter-name>
    <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
   <init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value> 
</init-param>
  </filter>

applicationcontext.xml
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>          

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>

<!-- 配置事务的传播特性  其他开头的方法* true 只读  脏数据不判断 速度快点-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>

<!-- 那些类的哪些方法参与事务 --> 
<aop:config>
<aop:pointcut id="allServicesMethod" expression="(execution(* cn.gov.pbc.gwjd.common.services.*.*(..)))
or (execution(* cn.gov.pbc.gwjd.supervise.services.*.*(..)))
or (execution(* cn.gov.pbc.gwjd.organization.services.*.*(..)))
"/>
<aop:advisor pointcut-ref="allServicesMethod" advice-ref="txAdvice"/>
</aop:config>主要是有以下两个问题:1、在表的hibernate配置文件中<hibernate-mapping>
    <class name="cn.gov.pbc.gwjd.common.pojo.DataDictionary" table="DATA_DICTIONARY" lazy="false" schema="DB2ADMIN">如果不加lazy="false" 则保存一条新数据时没问题,但调用 dataDictionaryService.getDataDictionary(“123123123”)方法时返回错误:
16:35:52,225 ERROR LazyInitializationException:19 - could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:60)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:172)
at cn.gov.pbc.gwjd.common.pojo.DataDictionary$$EnhancerByCGLIB$$1a309c4f.getValue(<generated>)
at cn.gov.pbc.gwjd.actions.TestServices.main(TestServices.java:57)加了lazy="false"就没事,但是如果加了 OpenSessionInViewFilter不就没有作用了吗?
而且<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
这几个中都行 就是get开头的方法不行,请问如何解决?
 与事物控制有关吗?
2、执行保存操作的时候可以保存数据 但会出现warning:16:35:52,225  WARN JDBCExceptionReporter:48 - SQL Warning: 0, SQLState: null
16:35:52,225  WARN JDBCExceptionReporter:49 - Connection readOnly mode is not enforcable after the connection has been established. To enforce a read only connection, set the readOnly data source or connection property.请问如何解决?我在javaweb模块 也发了一个相似的帖子 大家也可以去回答?同样给分,同时我马上要在此版块 还开了一个ssh如何处理异常的求助帖,大家也请帮忙。

解决方案 »

  1.   

    你的 OpenSessionInviewFilter 在web。xml中的配置顺序是怎么样的可能配置顺序导致没起到应该的作用
      

  2.   

    放在倒数第三个:
      <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
       <init-param>
    <param-name>singleSession</param-name>
    <param-value>true</param-value> 
    </init-param>
      </filter>
      <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      
         <listener>
            <listener-class>cn.gov.pbc.gwjd.common.TimertaskListener</listener-class>
        </listener>
    </web-app>
    结束
      

  3.   

    lazy="false" 是配置二级缓存的,如果当你关闭二级缓存的话!再去取数据就会报no session!有表连接的时候建议打开二级缓存,不然没错去表关联数据的时候还要去查一边,不然就会报错!
      

  4.   

    现在我见得表就是单独一张啊!不加lazy="false" 是的话 get开头的方法都出错
      

  5.   

    问一下事务控制中<tx:method name="*" read-only="true"/>这句话啥意思啊
      

  6.   

    把 <tx:method name="*" read-only="true"/>这句话去掉 就不会出现:2、执行保存操作的时候可以保存数据 但会出现warning:16:35:52,225 WARN JDBCExceptionReporter:48 - SQL Warning: 0, SQLState: null
    16:35:52,225 WARN JDBCExceptionReporter:49 - Connection readOnly mode is not enforcable after the connection has been established. To enforce a read only connection, set the readOnly data source or connection property.
    但是怎么在这句话不去掉的时候 也没有warning呢?
      

  7.   

    如果你几个方法不是在同一个事务里 一个事务执行完后事务提交那么意味着session关闭了 缓存的数据是在session里的 所以加了lazy="false"没事 ="ture"就会有你的那个问题了
    OpenSessionInViewFilter  是可以解决这个问题的 你有配置<filter-mapping>么
      

  8.   

    我要说的是Hibernate版本,不要追求最新版,对你一点好处没有,如果是的话,建议换一些旧一点点的,比如3.3.3.2也OK.为什么 这里不说.牛人可以解释下.
    半天都是一些 表面代码,具体哪句出错了呢?一点没看到.
      

  9.   

      <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>这个配置了怎样才能去掉警告呢?
    16:35:52,225 WARN JDBCExceptionReporter:48 - SQL Warning: 0, SQLState: null
    16:35:52,225 WARN JDBCExceptionReporter:49 - Connection readOnly mode is not enforcable after the connection has been established. To enforce a read only connection, set the readOnly data source or connection property.这些警告在 <tx:method name="*" read-only="true"/>时出现,去掉这句话没就没有警告。
    代码如下:
    Dao层
    public class DataDictionaryDaoImpl extends HibernateDaoSupport implements DataDictionaryDao{
     
    public DataDictionary getDataDictionary(String id){
    DataDictionary result;
    try{
    result= (DataDictionary)this.getHibernateTemplate().load(DataDictionary.class, id);
    }catch(RuntimeException e ){e.printStackTrace();
    throw e;
    }
    return result;
    }services层:
    public DataDictionary getDataDictionary(String id){
    DataDictionary result =null;
    try{
    result= dataDictionaryDao.getDataDictionary(id);
    }catch(RuntimeException e){
    e.printStackTrace();
    throw e;
      
    }
    return result;
    }
    Action中 :
    DataDictionary rusult=null;
    try{
    rusult = dataDictionaryService.getDataDictionary("dddd");
    System.out.println(rusult.getValue() );
    }
    catch(RuntimeException e){
    System.out.println("" );
    }
      if(rusult !=null)
      

  10.   

    <tx:method name="get*" propagation="REQUIRED" readonly="true"/>试一下