Java开发网 |
注册 |
登录 |
帮助 |
搜索 |
排行榜 |
发帖统计
|
您没有登录 |
» Java开发网 » Java EE 综合讨论区
» Hibernate
打印话题 寄给朋友 订阅主题 |
作者 | Re:session is closed [Re:wz_gu] |
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 ▄▅██████▅▄▃▂ ██████████████ ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲◤ 有了Swing为什么还要SWT? |
话题树型展开 |
人气 | 标题 | 作者 | 字数 | 发贴时间 |
9087 | session is closed | wz_gu | 94 | 2004-11-29 17:15 |
7668 | Re:session is closed | WeiterWay | 147 | 2004-11-29 18:10 |
7591 | Re:session is closed | wz_gu | 101 | 2004-11-30 08:57 |
7518 | Re:session is closed | wz_gu | 6 | 2004-11-30 14:00 |
7578 | Re:session is closed | WeiterWay | 18 | 2004-11-30 18:27 |
7611 | Re:session is closed | wz_gu | 5415 | 2004-12-01 08:58 |
7627 | Re:session is closed | WeiterWay | 46 | 2004-12-01 09:53 |
7609 | Re:session is closed | austinjust | 74 | 2004-12-04 23:50 |
已读帖子 新的帖子 被删除的帖子 |
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 |