在类声明一个session private Session session=HibernateSessionFactory.getSession();在保存方法里面保存,更新订单
session.saveOrUpdate(p);//减去数量
session.saveOrUpdate(or);//保存订单
然后读取出来。
public Order get(String uuid){
return (Order)session.get(Order.class,uuid);
}结果数据一下读出来是null,一会又有。有时读不到,有时出现。
session.saveOrUpdate(p);//减去数量
session.saveOrUpdate(or);//保存订单
然后读取出来。
public Order get(String uuid){
return (Order)session.get(Order.class,uuid);
}结果数据一下读出来是null,一会又有。有时读不到,有时出现。
解决方案 »
- spring定义一个bean,让别的调用
- 自学时,发现运行异常!请各位指点指点……
- 如何跟踪每个会话过程的运行路径??
- 求助hibernate调用本地sql的问题
- 请教appfuse登录后为什么到mainMenu.jsp画面?
- 求助,hibernate如何更新主键的数据?
- 初学者疑问,jdk和tomcat有什么关系,是不是运行jsp,只要有tocmat就可以了,而jdk只是用来编译jsp程序的,如果是这样,为什么很多地方都
- 请问什么是cvs,vss??有什么区别
- 菜鸟求助:一个BMP的问题
- DataSetView 问题请教
- ajax与struts做了个登录测试,出了问题请各位大虾帮帮忙
- Struts2+Spring+JPA
transaction.commit();在最后我加了,我现在还发现,过一段时间就会一直出现数据如果是刚刚保存,就会出现一时有数据,一时没有。
我现在希望保存了,马上就出现。而且刷新的时候不会一时有,一时无的情况
transaction.commit();
完成以后,最后关闭sessionHibernateSessionFactory.close。(具体代码记不清了)
每次你都直接从工厂拿Sessions,也没看见关闭session的代码。这比较危险。也就是问题的根源。你还是老老实实的写一个HibernUtil的类吧,Session与具体线程捆绑就没有问题了。
HibernateUtil对外提供打开关闭session 的接口。
Configuration configuration = new Configuration();
configuration.configure();
SessionFactory sessionFac = configuration.buildSessionFactory();
Session session = sessionFac.openSession();
Transaction t = session.beginTransaction();
……
t.commit();
session.close();
sessionFac.close();
public class HibernateSessionFactory { /**
* 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 HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}}
想来想去还是你事务提交的问题,是不是事务提交的位置有问题。建议你把“private Session session=HibernateSessionFactory.getSession();”去掉在每个方法里面用session之前写上”private Session session=HibernateSessionFactory.getSession();“,用完之后再关闭session。另外建议不要使用saveOrUpdate()方法,直接用save()或者update()方法。
我现在更新,保存,删除都是用完session就关闭。
public static void update(Object obj) throws Exception{
Session session=null;
try {
session=HibernateSessionFactory.getSession();
session.beginTransaction().begin();
session.update(obj);
session.beginTransaction().commit();
session.flush();
session.clear();
} catch (Exception e) {
session.beginTransaction().rollback();//事务回滚
throw e;
}finally{
if(session!=null)
session.close();
}
}可是他有时候还是不去访问数据库。这个太麻烦了。我都不知道怎么弄的
public static void update(Object obj) throws Exception{
Session session = null;
Transaction transaction = null;
try {
session = HibernateSessionFactory.getSession(); //我通常都是一个DAO一个SESSION
// session.beginTransaction().begin();
transaction = session.beginTransaction();//我通常是这样写 虽然有人说一个
//Session只有一个事务 但是我也没看过里面的实现 不知道
//是否创建的时候也是如此 另外 该事务已经是开始了
session.update(obj); //更新之前我会先获得这个实体 然后更改实体 然后调用方法
transaction.commit();
//session.beginTransaction().commit();
session.flush();
//session.clear();需要么?
} catch (Exception e) {
transaction.rollback();//事务回滚
throw e;
}finally{
//if(session!=null)
//session.close(); 一个DAO里应该给一个关闭连接的方法
}
}我通常都是一个DAO在创建的时候分配给他一个SESSION 然后使用结束后关闭SESSION 手写的 可能看起来会不舒服
这个问题怎么解决呢?
缓存问题,你设置所有数据都从缓存读了。好像配置文件有个lazy 之类的配置在xml可以指定。