求教,关于hibernate中Transaction的commit问题

vagrant_zy 2007-10-22 06:08:01
public static final ThreadLocal tLocalsess = new ThreadLocal();
public static final ThreadLocal tLocaltx = new ThreadLocal();


public static void commitTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
System.out.println(tx.toString());
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
tLocaltx.set(null);
System.out.println("commit tx");
} catch (HibernateException e) {
e.printStackTrace();
//这里报错 org.hibernate.SessionException: Session is closed
}
}


执行到tx.commit();的时候会报错org.hibernate.SessionException: Session is closed
这个问题大概会是出在什么地方?
...全文
376 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lisliefor 2007-10-22
  • 打赏
  • 举报
回复
用一个对象试试,后面的beginTransaction方法也使用tLocalsess对象
vagrant_zy 2007-10-22
  • 打赏
  • 举报
回复
这个我也不太清楚,这是某书上的例子,我仿照着做的,登录的地方用这个没问题的

数据录入用这个,写入后,无法commit,关闭后数据没有保存。但是id却增加了。

比如说现在有id=1,2。增加2个,id就有3,4。关闭后看数据库还是1,2,再运行程序,添加,id是从5开始
Lisliefor 2007-10-22
  • 打赏
  • 举报
回复
我不知道你为什么定义两个这样的对象:
public static final ThreadLocal tLocalsess = new ThreadLocal();

public static final ThreadLocal tLocaltx = new ThreadLocal();

你注意看你的currentSession方法,调用openSession方法的是tLocalsess对象,而beginTransaction方法使用Transaction的是tLocaltx对象。
vagrant_zy 2007-10-22
  • 打赏
  • 举报
回复
感谢楼上几位的回复

这个方法所在的类名字叫HibernateUtil,内容是:
package com.audit.dao.hibernate;

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

public class HibernateUtil {
private static final SessionFactory sessionFactory;

static {
try {
// Create the SessionFactory
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();
System.out.println("Initial SessionFactory creation failed.");
throw new ExceptionInInitializerError(ex);
}
}

public static final ThreadLocal tLocalsess = new ThreadLocal();

public static final ThreadLocal tLocaltx = new ThreadLocal();

/*
* getting the thread-safe session for using
*/
public static Session currentSession() {
Session session = (Session) tLocalsess.get();

// open a new one, if none can be found.
try {
if (session == null || !session.isOpen()) {
session = openSession();
tLocalsess.set(session);
}
} catch (HibernateException e) {
// throw new HibernateException(e);
e.printStackTrace();
}
return session;
}

/*
* closing the thread-safe session
*/
public static void closeSession() {

Session session = (Session) tLocalsess.get();
tLocalsess.set(null);
try {
if (session != null && session.isOpen()) {
session.close();
}

} catch (HibernateException e) {
e.printStackTrace();
}
}

/*
* begin the transaction
*/
public static void beginTransaction() {
System.out.println("begin tx");
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx == null) {
tx = currentSession().beginTransaction();
tLocaltx.set(tx);
}
} catch (HibernateException e) {
e.printStackTrace();
}
}

/*
* close the transaction
*/
public static void commitTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
System.out.println(tx.toString());
try {
//if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
//closeSession();
tLocaltx.set(null);
System.out.println("commit tx");
} catch (HibernateException e) {
e.printStackTrace();
//这里报错 org.hibernate.SessionException: Session is closed
}
}

/*
* for rollbacking
*/
public static void rollbackTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
} catch (HibernateException e) {
//e.printStackTrace();
}
}

private static Session openSession() throws HibernateException {
return getSessionFactory().openSession();
}

private static SessionFactory getSessionFactory() throws HibernateException {
return sessionFactory;// SingletonSessionFactory.getInstance();
}
}



下面是调用:


public List getProvince() {
// TODO Auto-generated method stub
try {
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
List results = s.createQuery("from Province province").list();
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
if (results != null && results.size() > 0) {
return results;
}
} catch (HibernateException e) {
log.fatal(e);
}
return null;
}
z_lping 2007-10-22
  • 打赏
  • 举报
回复
Hibernate Session is closed.
idilent 2007-10-22
  • 打赏
  • 举报
回复
还有,配置文件是不是autocommit?
idilent 2007-10-22
  • 打赏
  • 举报
回复
调用的这个地方怎么写的?

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧