开发者

Loaded records is not up to date. Using Hibernate and OpenSessionInViewFilter

开发者 https://www.devze.com 2023-01-27 04:48 出处:网络
I am using OpenSessionInViewFilter, Spring, and Hibernate for data access. When I commit a data record, my view results do not show the commited record using lazy loading.My session factory configurat

I am using OpenSessionInViewFilter, Spring, and Hibernate for data access. When I commit a data record, my view results do not show the commited record using lazy loading. My session factory configuration looks like this:

<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 configuration in the web.xml looks exactly like this:

    <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>

To obtain a session for my DAO I have below SessionLocator class which opens session for me;

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 curr开发者_如何学PythonentSession() 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;

}

And then in My DAO service I do Save and Update like this:

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

When I call upsert(), I can see the record on the table, but the updated value does not show up in my view during lazy loading. Do any one knows why the hibernate cache is not refreshed with the current record?


This issue was resolved by removing below line from openSessionInViewFilter config:

<dispatcher>FORWARD</dispatcher>

And annotate the DAO Service with @Transactional annotation

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

    } catch (Exception e) {

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

精彩评论

暂无评论...
验证码 换一张
取 消