设为首页 加入收藏

TOP

通向架构师的道路(第七天)之漫谈使用 ThreadLocal 改进你的层次的划分(七)
2018-02-06 13:13:05 】 浏览:980
Tags:通向 架构 师的 道路 漫谈 使用 ThreadLocal 改进 层次 划分
ernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.url"> jdbc:oracle:thin:@localhost:1521:myorcl </property> <property name="dialect"> org.hibernate.dialect.Oracle9Dialect </property> <property name="connection.username">abc</property> <property name="connection.password">abc</property> <property name="connection.driver_class"> oracle.jdbc.OracleDriver </property> <property name="show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.current_session_context_class">thread</property> <mapping resource="com/cts/testhb/model/TUser.hbm.xml" /> </session-factory> </hibernate-configuration>

然后上述代码将变成如下的样子:

public void testUser() throws Exception {

                   Transaction tran = null;

                   SessionFactory factory = null;

                   UserDAO userDAO = new UserDAOImpl();

                   try {

                            factory = HibernateUtil.getInstance().getSessionFactory();

                            Session session = factory.getCurrentSession();

                            tran = session.beginTransaction();

                            for (int i = 0; i < 100; i++) {

                                     TUser testUser = new TUser();

                                     testUser.setId(new Integer(i));

                                     testUser.setName("abc");

                                     userDAO.addUser(testUser);

                            }

                            tran.commit();

                   } catch (Exception e) {

                            tran.rollback();

                            throw new Exception(e);

                   } finally {

                            ThreadLocalSessionContext.unbind(factory);

                   }

         }

而你的每个DAO方法中的代码是这样实现的:

public void addUser(TUser user) throws Exception {

         SessionFactory factory = HibernateUtil.getInstance()

                            .getSessionFactory();

         Session session = factory.getCurrentSession();

         session.save(user);

}

是不是很方便的哈。

3.1.3 openSession与getCurrentSession的区别

严重注意下面3点:

  • openSession一旦被调用,必须且一定要在finally块中close,要不然你就等着out of memory吧;
  • 如果你使用的是getCurrentSession,那么你不能在finally块中调用”session.close()”,不行你可以在finally块中用try-catch把session.close();包起来,然后在catch{}块中抛出这个exception,这个exception将会是:sessionhas been already closed。

因为:

l   如果你用的是getCurrentSession,那么它在session.commit()或者是session.rollback()时就已经调用了一次session.close()了,因此你只要正确放置session.commit()与rollback()即可。

l   你必须在finally块中调用”ThreadLocalSessionContext.unbind(factory);”,以使得当前的事务结束时把session(即dbconnection)还回db connection pool中

²  如果你使用的是getCurrentSession,那么就算你是一个简单的select语句,也必须包含在:

tran = session.beginTransaction();

//your select hibernate query

tran.commit();

这样的事务块中,要不然它将会抛出这样的一个错误:

NoHibernate Session bound to thread, and configuration does not allow creation ofnon-transactional

看下面的例子:

try {

                    factory = HibernateUtil.getInstance().getSessionFactory();

                    Session session = factory.getCurrentSession();

                    tran = session.beginTransaction();

                    TUser testUser = userDAO.getUserByID("1");

                    log.info("user id===="+testUser.getId()+"  user name===="+testUser.getName());

                    tran.commit();

           } catch (Excep
首页 上一页 4 5 6 7 8 下一页 尾页 7/8/8
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇大数据搜索选开源还是商业软件?E.. 下一篇记一次 MySQL 删库的数据恢复

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目