用struts +Hibernate 有时可以保存数据有时候不行,请教高手

[复制链接]
查看11 | 回复2 | 2009-2-22 09:45:40 | 显示全部楼层 |阅读模式
我是在filter统一关闭session不知道是不是session的。当添加5到六条数据时,就添加不了了。删除方法也用不了。但没提示异常。重启TOMCAT后。又可以添加几条了。更新也可以。 但用了几下后又不行。
封装hibernateUtil 工具类代码:
public class HibernateUtil {
private static SessionFactory sessionfactory;
static {
try {
Configuration cfg = new Configuration().configure();
sessionfactory = cfg.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
private static final ThreadLocal threadlocal = new ThreadLocal();
private static final ThreadLocal threadtx = new ThreadLocal();
public static Session getSession() {
Session session = (Session) threadlocal.get();
if (session == null) {
session = sessionfactory.openSession();
threadlocal.set(session);
}
return session;
}
public static void sessionClose() {
Session session = (Session) threadlocal.get();
threadlocal.set(null);
try {
if (session != null && session.isOpen()) {

session.close();
}
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static void beginTransaction() {
Transaction tx = (Transaction) threadtx.get();
try {
if (tx == null) {

tx = getSession().beginTransaction();

threadtx.set(tx);
}
} catch (HibernateException e) {
e.printStackTrace();
}
}
public static void commitTransaction() {
Transaction tx = (Transaction) threadtx.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {

tx.commit();

threadtx.set(null);
}
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void rollbackTransaction() {
Transaction tx = (Transaction) threadtx.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {

tx.rollback();

threadtx.set(null);
}
} catch (HibernateException e) {
}
}}
关闭session.提交事务代码。
public class CloseSessionFilter implements Filter {
Log logger = LogFactory.getLog(this.getClass());
protected FilterConfig config;
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.config = arg0;
}
public void doFilter(
ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
try{
chain.doFilter((HttpServletRequest)request, (HttpServletResponse)response);
}
finally{
try{

HibernateUtil.commitTransaction();

//System.out.println("commit ok");
}catch (Exception e){

HibernateUtil.rollbackTransaction();
}finally{

HibernateUtil.sessionClose();
}
}
}
public void destroy() {
// TODO Auto-generated method stub
this.config = null;
}
}
我查看了CPU占用率。并不高。session应该关闭了吧?

回复

使用道具 举报

千问 | 2009-2-22 09:45:40 | 显示全部楼层
你可以在try { if (session != null && session.isOpen()) { session.close(); } } catch (HibernateException e) { e.printStackTrace(); } 加个finally里面关闭session试试如果 加了finally后程序没问题了就说明你的判断条件有点问题 session就没有成功关闭...
回复

使用道具 举报

千问 | 2009-2-22 09:45:40 | 显示全部楼层
数据库是Access吗?如果是的,就是数据库驱动问题,要购买驱动的,我以前碰到过雷同的问题...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行