使用Hibernate回调的优点? [英] Advantages of using Hibernate Callback?

查看:126
本文介绍了使用Hibernate回调的优点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解使用Hibernate回调方法的优点,有什么优势或特定的用例,我们应该去。

  public List findRecentRequests(final int offset,final int length)
{
List list = getHibernateTemplate ).executeFind(new HibernateCallback()
{
public Object doInHibernate(Session session)throws HibernateException
{
Query q = session.createQuery(FIND_RECENT_REQUESTS);
q .setFirstResult(offset);
q.setMaxResults(length);
return q.list();
}
});
return list;
}

另外一个更重要的问题是HibernateCallback方法在查询返回后每次都关闭会话值?我有使用情况下,每次刷新状态页多次调用此功能,因此将每次打开会话和查询数据库或将它存储查询结果在内存中,然后每次我调用此功能,结果将弹出从记忆里。



我已阅读(参考):



Spring HibernateTemplate.execute()在完成后会默认关闭任何打开的会话。当使用延迟初始化时,您可能会得到像下面的LazyInitializationException


org.hibernate.LazyInitializationException:无法初始化代理 - p>

对相关文档部分的任何引用将非常感谢。



更新:



在我的案例中,我使用ejb交易, 支持,我相信在这种情况下,事务设置为支持,它是可选的,所以每次新的会话将被创建,并且hibernate将查询数据库以获取结果,这是有瓶颈,将有一个正确的假设吗?

解决方案

关于为什么使用 HibernateCallback 。简短的答案 - 它允许您访问当前事务绑定会话,以便执行更复杂的休眠功能。大多数时候,在 HibernateTemplate 上的简单方法就足够了,但有时你需要去到 Session 。 / p>

这个谜题有两个部分。



第一个是事务范围, c $ c> PlatformTransactionManager / TransactionTemplate @Transactional 注释。有关更多信息,请参阅spring docs / google。



第二个是当你在一个事务 HibernateTemplate



所以像 hibernateTemplate.save()这样一个简单的操作将会参与交易。像你的例子一样更复杂也将参与事务。事实上,hTemplate上几乎任何方法都会参与。



所以知道你的问题关于会话何时关闭




  • 如果您明确使用交易,请参阅上述第一点,那么当交易范围关闭时,交易将被提交,会话将被关闭。

  • 没有事务spring每次创建一个会话你每次调用 HibernateTemplate 方法并立即关闭它。这不是首选的方法,除非你做的事情很简单,结果将从会话中分离出来,你会得到LazyInit异常。



在第二种情况下要注意的一个重要的点是没有显式事务。你处于连接的自动提交模式的怜悯,所以你可能在回调,保存,保存,抛出异常。



我的建议,在做任何更新时,使用一个事务。



如果所有的交易东西是新的,请检查spring文档的交易章节。


I am not able to understand advantages of using Hibernate Callback method, are there any advantages or specific use case where we should go for it.

public List findRecentRequests(final int offset, final int length)
{
    List list = getHibernateTemplate().executeFind(new HibernateCallback()
    {
        public Object doInHibernate(Session session) throws HibernateException
        {
            Query q = session.createQuery(FIND_RECENT_REQUESTS);
            q.setFirstResult(offset);
            q.setMaxResults(length);
            return q.list();
        }
    });
    return list;
}

Also one more important question is that does HibernateCallback method close session everytime after query returns values? I have use case where am calling this function multiple times on every refresh of status page and so will it everytime open session and query database or will it store query results in memory and then everytime I make call to this function, results would be popped out from memory.

I have read(Reference):

The spring HibernateTemplate.execute() by default closes any open sessions upon completion. When used with lazy initialization you may get a LazyInitializationException like the following

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Any reference to relevant documentation part would be highly appreciated.

Update:

In my case am using ejb transactions and setting it to "support" and i believe in that case as transaction is set to support, it's optional and so everytime new session will be created and hibernate will query database to get results and so that's were am having bottleneck, will that an right assumptions to make?

解决方案

To your point about why use HibernateCallback. Short answer - it allows you to access the current transactionally bound session in order to do perform more complex hibernate functions. Most of the time the simple methods on HibernateTemplate are sufficient, but sometimes you need to go down to the Session.

There's two parts to the puzzle.

The first is the transaction scope which is defined either by using PlatformTransactionManager / TransactionTemplate OR @Transactionalannotations. See the spring docs/google for more info.

The second is that when you are within a transaction HibernateTemplate will interact with the current transaction using a bit of magic.

So a simple operation like hibernateTemplate.save() will participate in the transaction. A more complex like your example will also participate in the transaction. In fact pretty much any method on hTemplate will participate.

So know to your question about when does the session get closed

  • If you are using transactions explicitly, see first point above, then when the transaction scope closes the transaction will be committed and the session will be closed.
  • Without transactions spring creates a session for you each time you call a HibernateTemplate method and closes it immediately afterwards. This is not the preferred approach as unless you are doing something very simple the results will be detached from the session and you will get LazyInit exceptions.

An important point to note in the second case above there is NO explicit transaction. You are at the mercy of the auto-commit mode of the connection so you might do, in a callback, save, save, throw exception. The first save MAY have been committed, without a transaction there's no guarantee.

My advice, when doing any updates, use a transaction.

If all the transaction stuff is new to you check the spring docs for the transaction chapter.

这篇关于使用Hibernate回调的优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆