//注意:这里的session用完之后并不close,而只flush一下
public void delCat(String strCatId) {
try {
Session s = HibernateSessionFactory.currentSession(); Object cat = s.load(Cat.class, strCatId);
s.delete(cat);
s.flush();
} catch (HibernateException e) {
e.printStackTrace();
}
}----------
http://www.hibernate.org.cn:8000/92.html
public void delCat(String strCatId) {
try {
Session s = HibernateSessionFactory.currentSession(); Object cat = s.load(Cat.class, strCatId);
s.delete(cat);
s.flush();
} catch (HibernateException e) {
e.printStackTrace();
}
}----------
http://www.hibernate.org.cn:8000/92.html
解决方案 »
- 如何分析代码?
- 寻找JDK 1.4下JAVA+ORACLE的开源论坛原码
- ClassNotFoundException该怎么解决?
- tapestry 中 如何实现 JSP中 <@jsp:include/> 的功能?
- 特别策划之Java十年再回首 经典再现 Java十大事件
- tomcat启动时的异常,是什么原因
- 急救,有没有在JBoss3.2.x上开发遇到这样的古怪问题,请看内容.
- dom4j向Xml指定位置添加数据
- JS怎么实现页面按空格键的时候自动替换成‘’
- 回调函数中数据插入不进网页
- tomcat5.0.29連接池出錯?急急急,請幫忙看一看,謝謝
- Apache与Tomcat整合问题,大家大家帮帮忙!
{
private static final ThreadLocal sessionContext = new ThreadLocal(); private Session session;
private int level; public static Session currentSession()
throws HibernateException
{
SessionFactory factory = ...;
ThreadLocalSession tlSession = (ThreadLocalSession)sessionContext.get();
if (tlSession == null)
{
tlSession = new ThreadLocalSession();
tlSession.session = factory.openSession();
tlSession.level = 0;
sessionContext.set( tlSession );
}
tlSession.level++;
return tlSession.session;
} public static void closeSession()
throws HibernateException
{
ThreadLocalSession tlSession = (ThreadLocalSession)sessionContext.get();
if (tlSession == null)
{
return;
} tlSession.level--;
if (tlSession.level <= 0)
{
if (tlSession.session != null && tlSession.session.isOpen())
{
tlSession.session.close();
}
sessionContext.set( null );
}
}}