hibernate3 更新数据问题,大家有碰到过的没? session.clear();是清空一级缓存的方法,楼主使用了二级缓存,二级缓存是SessionFactory级的所以要调用SessionFactory的evice()方法去清空 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 上边发错了点.session.getSessionFactory().evict(DocBean.class);我也用了的session.refresh(DocBean);session.clear();session.getSessionFactory().evict(DocBean.class);这几个方法我都用了的 DocBean的同步缓存策略是什么? <property name="hibernate.jdbc.use_streams_for_binary">true</property> <property name="hibernate.order_updates">true</property> <property name="hibernate.connection.autocommit">false</property> <property name="hibernate.transaction.auto_close_session">false</property><property name="hibernate.cache.use_query_cache">true</property><property name="hibernate.cache.use_second_level_cache">true</property><property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property其他都是默认的 另外我还加了个 default-lazy="false" hibernate 步缓存策略 这个我不清楚 那就不太清楚了,不好意思,不过hibernate不建议使用HashTable作为二级缓存,试试第三方的cache 我换过其他的ECache 来用,效果一样,感觉很奇怪,第一次更没问题..... 请问:这些更新已经真正被持久化到表里了吗?你可以试验一下来缩小问题的范围:1,关闭Query_Chache, 因为修改数据时Query_Chache绝对没有意义。2,关闭二级缓存。 批量更新: for(...){ pojo = session.load(...) session.update(pojo); if( i%10 ==0 ) session.flush(); } session.flush(); //这时再看看更新了没有? 数据已经更新了的,但从cache 中读如老的数据后,又吧数据库的内容更新成老的了.有时候更新SQL都没有.我在家里电脑上测试了一下,写了个简单的更新,又没什么问题.就是公司的电脑上这个程序有这个问题.奇怪啊 在加没有打开二级缓存吧,不涉及到缓存同步。或者是DocBean这个hbm的同步策略的问题,这个不太确定。把DocBean.hbm贴出来吧。 这个问题我也碰到过。我的解决方案是:在查询时晴空session.而不是在进行修改时。实例代码:public List getCgList(Object searchObj, int currentPageNumb) { List rtnList = new ArrayList(); try { String sql = "from Phinfo as phinfo "; sql = sql+getWhereSql(searchObj)+getOrderbySql(); Query query = this.getSession().createQuery(sql); query.setFirstResult(DestributePage.perPageNumb * (currentPageNumb - 1)); query.setMaxResults(DestributePage.perPageNumb); this.getSession().clear(); //晴空缓存 rtnList = query.list(); } catch (Exception ex) { this.getLogger().error("____hibernate操作错误____", ex); } return rtnList; } <hibernate-mapping default-lazy="false"> <class name="jspx.jdoc.table.DocPart"> <id name="id" type="java.lang.String" length="50"> <generator class="uuid.hex" /> </id> <property name="partId" type="java.lang.String" column="partId" length="100" not-null="true"/> <property name="manId" type="java.lang.String" column="manId" length="50" not-null="true"/> <property name="title" type="java.lang.String" column="title" length="50" not-null="true"/> <property name="logo" type="java.lang.String" column="logo" length="200" not-null="false"/> <property name="logoLink" type="java.lang.String" column="logoLink" length="200" not-null="false"/> <property name="sortDate" type="java.util.Date" column="sortDate" not-null="true" /> </class></hibernate-mapping> 终于知道问题了,是使用是 ThreadLocal 方式.的问题. 目前的网上通用的 ThreadLocal 方式实现, 在多线程交叉查询的时候并没有很处理.所以解决方法有三个1.不使用ThreadLocal 方式,用最基本的方式就可以了保证稳定了.2.使用的时候不要出现多线程交叉查询,一般很难保证.3.修改你的ThreadLocal 类. java菜鸟对于JFrame小疑问 java WebGIS sitemesh的路径问题 springframework Order 该怎么理解? hibernate 问题很急!!!!!!!!!!! 求助:怎么向Jtable里输入数据阿~!谢谢给位大哥 学习Java的困惑(痛苦!) hashmap j2ee下的EJB调用出现的问题??急救! web 套打 用什么控件 ,新手不知道如何选择下手, jatoolsPrinter、lodop? 请教Hibernate中的Session应该是单例还多例的? 初学:请问struts下一个form里怎么写多个action
session.getSessionFactory().evict(DocBean.class);
我也用了的
session.refresh(DocBean);
session.clear();
session.getSessionFactory().evict(DocBean.class);这几个方法我都用了的
<property name="hibernate.order_updates">true</property>
<property name="hibernate.connection.autocommit">false</property>
<property name="hibernate.transaction.auto_close_session">false</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property其他都是默认的
感觉很奇怪,第一次更没问题.....
你可以试验一下来缩小问题的范围:1,关闭Query_Chache, 因为修改数据时Query_Chache绝对没有意义。
2,关闭二级缓存。
批量更新:
for(...){
pojo = session.load(...)
session.update(pojo);
if( i%10 ==0 )
session.flush();
}
session.flush();
//这时再看看更新了没有?
有时候更新SQL都没有.
我在家里电脑上测试了一下,写了个简单的更新,又没什么问题.就是公司的电脑上这个程序有这个问题.
奇怪啊
或者是DocBean这个hbm的同步策略的问题,这个不太确定。
把DocBean.hbm贴出来吧。
实例代码:
public List getCgList(Object searchObj, int currentPageNumb) {
List rtnList = new ArrayList();
try {
String sql = "from Phinfo as phinfo ";
sql = sql+getWhereSql(searchObj)+getOrderbySql();
Query query = this.getSession().createQuery(sql);
query.setFirstResult(DestributePage.perPageNumb * (currentPageNumb - 1));
query.setMaxResults(DestributePage.perPageNumb);
this.getSession().clear(); //晴空缓存
rtnList = query.list();
} catch (Exception ex) {
this.getLogger().error("____hibernate操作错误____", ex);
}
return rtnList;
}
<class name="jspx.jdoc.table.DocPart">
<id name="id" type="java.lang.String" length="50">
<generator class="uuid.hex" />
</id>
<property name="partId" type="java.lang.String" column="partId" length="100" not-null="true"/>
<property name="manId" type="java.lang.String" column="manId" length="50" not-null="true"/>
<property name="title" type="java.lang.String" column="title" length="50" not-null="true"/>
<property name="logo" type="java.lang.String" column="logo" length="200" not-null="false"/>
<property name="logoLink" type="java.lang.String" column="logoLink" length="200" not-null="false"/>
<property name="sortDate" type="java.util.Date" column="sortDate" not-null="true" />
</class>
</hibernate-mapping>
三个
1.不使用ThreadLocal 方式,用最基本的方式就可以了保证稳定了.
2.使用的时候不要出现多线程交叉查询,一般很难保证.
3.修改你的ThreadLocal 类.