小弟新手,最近在学习Hibernate,在如何使用事务上碰到一个问题。
想知道大家都是如何取得Session和处理事务的!(不使用Spring).
是把开Session和事务放在DAO中还是放在业务逻辑层。开Session放在业务逻辑层好像挺麻烦,要把Session做为参数传到DAO中去。如果是在DAO中开Session,那业务逻辑层又如何控制事务。实在想不到什么好办法。跪求高手解惑。详细说明请参考: http://www.javapknet.com/htmlhelp/2009090/01674e8295f4db99.html
想知道大家都是如何取得Session和处理事务的!(不使用Spring).
是把开Session和事务放在DAO中还是放在业务逻辑层。开Session放在业务逻辑层好像挺麻烦,要把Session做为参数传到DAO中去。如果是在DAO中开Session,那业务逻辑层又如何控制事务。实在想不到什么好办法。跪求高手解惑。详细说明请参考: http://www.javapknet.com/htmlhelp/2009090/01674e8295f4db99.html
解决方案 »
- 大家帮忙看一下这样的SSH框架结构好不好
- java实现类似百度文库功能 调用flashpaper实现doc等格式文档转换为swf,并存到指定路径
- 这段JSP页面的JS脚本为什么不执行?
- 关于事务,困惑我好久了,求解答!
- 【跪求高手】ssh+dwr出现问题,在线等,在线结
- [很费解] struts2 iterator标签 的奇怪问题
- JSP中是否没有id这个字段
- struts中同一path有不同的参数如何配置不同struts-config.xml中的action
- 100分求高手教我用md5
- linux下已经安装了mysql3.23,现在要安装mysql4.0,怎样卸载旧的Mysql ??? 急
- 请问用tomcat或sturts2拦截器把这些空格和分行弄掉?
- hibnate一对一主键关联
private static String CONFIG_FILE_LOCATION = "/dao/hibernate/Hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
private HibernateSessionFactory() {
}}