当前位置: 代码迷 >> Java Web开发 >> struts + hibernate 后台页面修改数据后,前台页面显示的内容新旧数据交替出现。请高手帮忙分析上原因
  详细解决方案

struts + hibernate 后台页面修改数据后,前台页面显示的内容新旧数据交替出现。请高手帮忙分析上原因

热度:3354   发布时间:2013-02-25 21:08:55.0
struts + hibernate 后台页面修改数据后,前台页面显示的内容新旧数据交替出现。请高手帮忙分析下原因
目前问题是能解决的,但是不知道原因。有高手能给分析下吗


就是修改完记录后,进数据库里直接看,记录确实更改了,
但是每次刷新前台页面,有时候是显示最新的数据内容,有时候又是显示修改前的数据内容。
我上网找了下,能做的都做了。
映射配置里 lazy设成false了。
DAO里修改的方法,flush(), commit(), closs(), 都做过了
hibernate配置里 2级缓存也禁用了。
但是问题依然存在。而这样的情况,我在本机调试 始终没有出现数据混乱,部署到服务器上就会出现,两边都用的weblogic8.1。

dao里修改的代码:
Java code
    public void attachDirty(PageList instance) {        Session session = HibernateSessionFactory.getSession();        if(instance!=null){            Transaction ts=null;            try{                ts=session.beginTransaction();                session.saveOrUpdate(instance);                session.flush();                ts.commit();            }catch(Exception ex){                if(ts!=null){                    ts.rollback();                }            }                    }        session.close();    }


hibernate.cfg.xml里2级缓存也设置过取消了
XML code
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider </property>     <property name="hibernate.cache.use_second_level_cache">false</property>


前台显示页面中
HTML code
<%//......    PageListDAO pdao=new PageListDAO();// pdao.getSession().close();[color=#FF0000]如果加这句话,问题就解决了,但是不知道原因。[/color]    PageList ptemp=pdao.findById(pageid);//然后out print 该记录的内容...%>


// pdao.getSession().close();如果加这句话,问题就解决了,但是不知道原因。
页面里的这行代码,加的时候本机调试和服务器部署后都正常;不加的话,本机正常,部署到服务器会出现新旧数据混乱问题。
实在想不明白其中的道理,hibernate session我dao里 明明做过close了。而且这行代码不加时候,本机调试就是没问题,一部署就出问题。郁闷死

------解决方案--------------------------------------------------------
检查下HibernateSessionFactory 这个工具类是不是有问题?好像DAO层得到的session都是从HibernateUtil 中的ThreadLocal<Session>取得的 不需要session.close()了 用完放回 ThreadLocal(session)[code=Java][/code]package com.hangzhoubank.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the current
 * thread of execution. Follows the Thread Local Session pattern, see
 * {@link http://hibernate.org/42 }.
 */
public class HibernateUtil {

/**
* Location of hibernate.cfg.xml file. Location should be on the classpath
* as Hibernate uses #resourceAsStream style lookup for its configuration
* file. The default classpath location of the hibernate config file is in
* the default package. Use #setConfigFile() to update the location of the
* configuration file for the current session.
*/
private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;

static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

private HibernateUtil() {
}

/**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.

* @return Session
  相关解决方案