2010-11-29 5 views
2

데이터 액세스를 위해 OpenSessionInViewFilter, Spring, Hibernate를 사용하고 있습니다. 데이터 레코드를 커밋 할 때 내보기 결과에 지연된로드를 사용하여 커밋 된 레코드가 표시되지 않습니다. 내 세션 공장 구성은 다음과 같습니다은 web.xml에서로드 된 레코드가 최신이 아닙니다. Hibernate와 OpenSessionInViewFilter 사용하기

<bean id="dbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="${database.driver}" /> 
    <property name="url" value="${database.url}" /> 
    <property name="username" value="${database.user}" /> 
    <property name="password" value="${database.password}" /> 
    <property name="poolPreparedStatements" value="true" /> 
    <property name="defaultAutoCommit" value="true" /> 
</bean> 

<tx:annotation-driven transaction-manager="txManager" /> 
<bean id="txManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dbcpDataSource" /> 
</bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dbcpDataSource" /> 
    <property name="annotatedClasses"> 
     <list> 
      <value>or.pac.Address</value> 
      <value>or.pac.Customer</value> 
      <value>or.pac.Documents</value> 
     </list> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
      <prop key="hibernate.connection.release_mode">auto</prop> 
      <prop key="hibernate.current_session_context_class">jta</prop> 
      <prop key="hibernate.transaction.auto_close_session">true</prop> 
      <prop key="hibernate.show_sql">false</prop> 
      <prop key="hibernate.generate_statistics">false</prop> 
      <prop key="hibernate.cache.use_query_cache">true</prop> 
      <prop key="hibernate.max_fetch_depth">4</prop> 
      <prop key="hibernate.cache.use_second_level_cache">true</prop> 
      <prop key="hibernate.cache.use_query_cache">true</prop> 
      <prop key="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</prop> 
      <prop key="hibernate.hbm2ddl.auto">update</prop> 
     </props> 
    </property> 
</bean> 
    <bean id="locator" class="cc.SessionLocator"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

OpenSessionInViewFilter 구성은 다음과 같이 정확히 같습니다

<filter> 
    <filter-name>openSessionInViewFilter</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> 
    <init-param> 
     <param-name>flushMode</param-name> 
     <param-value>AUTO</param-value> 
    </init-param> 
    <init-param> 
     <param-name>sessionFactoryBeanName</param-name> 
     <param-value>sessionFactory</param-value> 
    </init-param> 
</filter> 

<filter-mapping> 
    <filter-name>openSessionInViewFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
    <dispatcher>FORWARD</dispatcher> 
</filter-mapping> 

내가 SessionLocator 클래스 아래에있는 내 DAO에 대한 세션을 얻으려면 나를 위해 세션을 엽니 다.

public class SessionLocator{ 

public static final ThreadLocal session = new ThreadLocal(); 
private static SessionLocator me; 

static { 
    try { 
     me = new SessionLocator(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

private SessionLocator() throws HibernateException, JDBCException { 
} 

public static Session currentSession() throws Exception { 
    Session s = (Session) session.get(); 
    if (s != null) { 
     s.setCacheMode(CacheMode.REFRESH); 
    } 

    if (s == null) { 
     synchronized SessionLocator{ 
      s =openSession(); 
     } 
     session.set(s); 
    } 

    return s; 
} 



public void setSessionFactory(SessionFactory sessionFactory) { 
    PersistenceManager.sessionFactory = sessionFactory; 
} 

public static Session openSession() { 
    Session session = SessionFactoryUtils.doGetSession(sessionFactory, true); 
    return session; 
} 

private static SessionFactory sessionFactory; 

}

그리고 에 내 DAO 서비스 내가 저장 않으며 다음과 같이 업데이트 : 나는 upsert()를 호출 할 때

public boolean upsert(Object d) { 
    try { 
     SessionLocator.currentSession().saveOrUpdate(d); 
       } catch (Exception e) { 
     e.printStackTrace(); 
     return false; 
    } 
    return true; 
} 

, 나는 테이블에 레코드를 볼 수 있지만, 업데이트 된 값이 게으른로드 중에 내보기에 표시되지 않습니다. 최대 절전 모드 캐시가 현재 레코드로 새로 고쳐지지 않는 이유를 아는 사람이 있습니까?

답변

1

이 문제는 openSessionInViewFilter의 설정에서 선 아래 제거하여 해결되었습니다 : @Transactional 주석으로 DAO 서비스

<dispatcher>FORWARD</dispatcher> 

그리고 주석

@Transactional 
public boolean upsert(Object d) { 
try { 
    SessionLocator.currentSession().saveOrUpdate(d); 

    } catch (Exception e) { 

    e.printStackTrace(); 
    return false; 
    } 
    return true; 
    }