首页 新闻 会员 周边

查询数据库时只有第一次是数据库,知不道是hibernate缓存的问题还是程序问题。

0
悬赏园豆:50 [已解决问题] 解决于 2013-05-08 09:43

查询数据库时只有第一次是数据库完整数据,之后增删改成都可以完成,但是查询还是第一次的结果。只有把程序重启后,查询才是更新后的数据库内容。知不道是hibernate缓存的问题还是程序问题。hibernate的二级缓存我没有设置,直接关了。

——————————————————————————————————————————

使用hibernate查询代码如下:

package com.ihome.product.service;

import java.util.ArrayList;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import com.ihome.back.util.HibernateUtil;
import com.ihome.product.pojo.ProductMsg;

public class ProductSelectImp {
public static boolean isSelect = false;
public static ArrayList<ProductMsg> ilist = null;

@SuppressWarnings({ "unchecked" })
public static String selectSQL() {
Session session = null;
try {
session = HibernateUtil.getSession();
Query query = session.createQuery("from ProductMsg");
ArrayList<ProductMsg> list = (ArrayList<ProductMsg>) query.list();

session.flush();
for (ProductMsg productMsg : list) {
ilist = list;
return "success";
}
session.clear();
}
catch (HibernateException e) {
throw e;
} finally {
if (session != null) {
session.close();
}
}
isSelect = true;
return "error";
}
}

——————————————————————————————————————————

package com.ihome.back.util;

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

@SuppressWarnings("deprecation")
public final class HibernateUtil {
private static SessionFactory sessionFactory=null;
private HibernateUtil(){

}
static{
Configuration cfg=new Configuration();
cfg.configure();
sessionFactory=cfg.buildSessionFactory();
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Session getSession(){
return sessionFactory.openSession();
}
}

——————————————————————————————————

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/Ihomebackground</property>
<property name="connection.username">root</property>
<property name="connection.password">0639</property>

<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- 最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>

<!-- 最小连接数 -->
<property name="hibernate.c3p0.min_size">5</property>

<!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
<property name="hibernate.c3p0.timeout">120</property>

<!-- 最大的PreparedStatement的数量 -->
<property name="hibernate.c3p0.max_statements">100</property>

<!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒 -->
<property name="hibernate.c3p0.idle_test_period">120</property>

<!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>

<!-- 每次都验证连接是否可用 -->
<property name="hibernate.c3p0.validate">true</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->

<!-- 取消hibernate二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">false</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->

<mapping class="com.ihome.back.pojo.NewsUser" />
<mapping class="com.ihome.back.pojo.UserMsg" />
<mapping class="com.ihome.product.pojo.ProductMsg" />
<mapping class="com.ihome.product.pojo.SrcImg" />

</session-factory>

</hibernate-configuration>

孙土土的主页 孙土土 | 初学一级 | 园豆:157
提问于:2013-05-07 17:08
< >
分享
最佳答案
1

具体的原因可能是: 删除、添加和查询用的不是同一个Session,这样会话的信息缓存就不对了

解决方法:

使用同一个Session操作,如把session关联到ThreadLocal上

ThreadLocal<Session> s;获取和设置的方法

sess = (Session)s.get();

s.set(sess);

 

以前我们flex的访问时遇到这个问题,不知和你这个是否一样

收获园豆:50
2012 | 高人七级 |园豆:21230 | 2013-05-07 18:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册