wz_gu
发贴: 27
积分: 0
|
于 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.set; }
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 Long.longValue() + 1; } catch (Exception e) { e.printStackTrace(); return 0; } }
在第一次调用时可以出来结果,但是刷新后数据就没有了,请问为什么?
●█〓██▄▄▄▄▄▄ ●●msn:wz_gu@hotmail.com ▄▅██████▅▄▃▂ ██████████████ ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲◤
|