Topic: session is closed

  Print this page

1.session is closed Copy to clipboard
Posted by: wz_gu
Posted on: 2004-11-29 17:15

在前台页面上调用后台方法,获取数据。第一次运行没有错误,刷新页面后,数据消失,看控制台出现提示:session is closed。这是为什么?为什么第一次能出来,刷新后,却出现连接中断?

2.Re:session is closed [Re: wz_gu] Copy to clipboard
Posted by: WeiterWay
Posted on: 2004-11-29 18:10

刷新可能是重新连接一次,这时状态已经改变,你需要查一查session是否在第一调用结束已经close了,如果是的话,需要在每次调用之前重新建立一个session。。。
一般做法是,显示数据没有必要每次调用后台逻辑,前台cache数据,这要效率要高一些。当然,如果数据动态性较强情况就不一样了。

3.Re:session is closed [Re: wz_gu] Copy to clipboard
Posted by: wz_gu
Posted on: 2004-11-30 08:57

你好,我的程序是这样的:先建一个HiberanteUtil,在那里面先检查有没有连接,如果有,就不会建立,如果没有,才建立另一个session,每次操作数据库,都会有这样一个过程。真的不知道哪出了问题。

4.Re:session is closed [Re: wz_gu] Copy to clipboard
Posted by: wz_gu
Posted on: 2004-11-30 14:00

没人问答吗?

5.Re:session is closed [Re: wz_gu] Copy to clipboard
Posted by: WeiterWay
Posted on: 2004-11-30 18:27

先把代码(后台和前台调用)贴上来吧。

6.Re:session is closed [Re: wz_gu] Copy to clipboard
Posted by: wz_gu
Posted on: 2004-12-01 08:58

util:负责产生session
/*
* Created on 2004-4-25
*/
package util;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

import org.apache.log4j.Logger;

/**
* @author ibmt41
*
* This is for Hibernate version 2.x
*
* Hibernate is designed to be useable in any kind of Java application,
* including applications that make extensive use of multithreading. So, unlike
* the ODMG API, Hibernate's native API does not use the current thread to
* maintain associations between Session and Transaction or between Session and
* application thread. This can be inconvenient for J2EE applications where
* access to a Session instance is always from a particular thread. It is
* particularly inconvenient when using a DAO pattern, when the different DAO
* classes need to obtain the (same) current session.
*
*
* A thread-local variable effectively provides a separate copy of its value for
* each thread that uses it. Each thread can see only the value associated with
* that thread, and is unaware that other threads may be using or modifying
* their own copies.
*/
public class Hibernate2Session {
private final static Logger log = Logger.getLogger(Hibernate2Session.class
    .getName());

public static final ThreadLocal sessionThread = new ThreadLocal();
public static final ThreadLocal transactionThread = new ThreadLocal();
public static final Configuration _cf = new Configuration();
static SessionFactory sf = null;

  /**
   * Make this class a singleton
   */
private Hibernate2Session() {
  super();
}

static {
try {
init();
}
catch (HibernateException e) {
log.error("could not init " + e.getMessage());
}
}

  /**
   * 获取当前线程使用的Session
   *
   * @return Session
   * @throws HibernateException
   */
  public static Session currentSession() throws HibernateException {
    Session s = (Session) sessionThread.get();

    if (s == null) {
      if (sf == null) {
        init();
      }
      try {
        s = sf.openSession();
      }
      catch (HibernateException e) {
        log.error("Could not get Hibernate session: " + e.getMessage());
        throw new HibernateException(
            "Could not get Hibernate session: " + e.getMessage());
      }
      sessionThread.setMoon;
    }

    return s;
  }

  /**
   * 启动或者加入当前Session的Transaction
   *
   * @return Transaction
   * @throws HibernateException
   */
  public static Transaction currentTransaction() throws HibernateException {
    Transaction tx = (Transaction) transactionThread.get();
    if (tx == null) {
      try {
        tx = currentSession().beginTransaction();
      }
      catch (HibernateException e) {
        log.error("Could not get Hibernate Transaction: "
            + e.getMessage());
        throw new HibernateException(
            "Could not get Hibernate Transaction: "
                + e.getMessage());
      }
      transactionThread.set(tx);
    }
    return tx;
  }

  /**
   * 提交当前Session的Transaction
   *
   * @throws HibernateException
   */
  public static void commitTransaction() throws HibernateException {
    Transaction tx = (Transaction) transactionThread.get();
    transactionThread.set(null);
    if (tx != null) {
      try {
        tx.commit();
      }
      catch (HibernateException e) {
        log.error("Could not commit Hibernate Transaction: "
            + e.getMessage());
        throw new HibernateException(
            "Could not commit Hibernate Transaction: "
                + e.getMessage());
      }
    }
  }

  public static Configuration getConfiguration() {
    return _cf;
  }

  /**
   * 关闭当前线程使用的Session
   *
   * @throws HibernateException
   */
  public static void closeSession() throws HibernateException {
    Session s = (Session) sessionThread.get();
    sessionThread.set(null);
    if (s != null) {
      try {
        s.close();
      }
      catch (HibernateException e) {
        log.error("Could not close Hibernate Session: "
            + e.getMessage());
        throw new HibernateException(
            "Could not close Hibernate Session: " + e.getMessage());
      }
    }
  }

  private static synchronized void init() throws HibernateException {
    if (sf != null) {
      // check again because more than 1 user might be
      // trying to do this at the same time. just return.
      return;
    }

    try {
      sf = _cf.configure().buildSessionFactory();
      log.info("Hibernate2Session Initialized SessionFactory=" + sf);
    }
    catch (HibernateException e) {
      log.error("Could not intialize Hibernate session factory: "
          + e.getMessage());
      throw new HibernateException(
          "Could not intialize Hibernate session factory: "
              + e.getMessage());
    }
  }
}

然后就是对数据库操作:(其中的一个方法)

public static long speCond(String table, String column) {

    List speList = new ArrayList();
    String s = null;
    try {
      Session session = Hibernate2Session.currentSession();
      speList = session.find("select max(tables." + column + ") from "
          + table + " as tables");
      if ( speList == null ||speList.get(0)==null || speList.get(0).equals("")) {
        s="0";
      }
      else
        s = speList.get(0).toString();
      session.flush();
      Hibernate2Session.closeSession();
      return new LongMoon.longValue() + 1;
    }
    catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }

在第一次调用时可以出来结果,但是刷新后数据就没有了,请问为什么?

7.Re:session is closed [Re: wz_gu] Copy to clipboard
Posted by: WeiterWay
Posted on: 2004-12-01 09:53

看不出代码有什么问题,给 currentSession()方法加synchronized试试

8.Re:session is closed [Re: wz_gu] Copy to clipboard
Posted by: austinjust
Posted on: 2004-12-04 23:50

把currentSession() 中的 if (s == null) { 改为 if (s == null || !s.isOpen()) {


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923