在分层的Spring MVC Web应用程序中处理Hibernate会话的最佳方法 [英] Best way to handle Hibernate Sessions in a layered Spring MVC Web application

查看:89
本文介绍了在分层的Spring MVC Web应用程序中处理Hibernate会话的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有一个Web应用程序,它具有


  • 沉重的UI(Spring MVC +带JSON的JQuery)
  • 使用JPA批注作为域模型的Hibernate
  • 将Spring提供的DAO扩展到DAO层代码
  • JBOSS是带有Oracle的应用程序服务器作为后端

  • 基于数据源(JNDI)的连接池(不是XA本地数据源)
  • 也可以访问多个数据源(处理多个数据库)


    行为上,很多数据检索(70%)和更新数据为30%

    有效消费数据库连接的最佳做法是什么?并且还要看到连接使用时没有太多泄漏?


    1. 选择基于Hibernate模板的DAO会更好吗?

    2. 什么样的事务管理器会被建议 - 我们是否应该在基于AOP的事务管理中

    3. 在哪里实例化会话以及在哪里关闭会话以有效地使用来自连接池的连接。

    4. 确实,我们需要处理来自服务层的事务,但会话会发生什么情况,他们会等待更长时间(我们没有使用任何opensessioninviewFilter)
    5. 哪个层更好地处理检查的异常(业务异常)和运行时异常。

    对不起,这是一个比较冗长的问题,但是我发现这是一个常见的查询,我尝试整合它。感谢您的耐心和指导。谢谢你的帮助。

    解决方案

    这听起来像是一个非常典型的Spring / Hibernate应用程序,所以我会推荐以下当前最佳实践, 另一个答案。具体来说:


    1. 不要扩展Spring DAO支持类或使用HibernateTemplate。使用 @Repository 注释与组件扫描相结合,以及直接将SessionFactory注入到您的DAO中。

    2. 使用Spring的 HibernateTransactionManager的和肯定使用声明式事务管理通过 @Transactional 作为您的默认方法。 Spring管理这个。它会在默认情况下及时为交易打开会话,但首选以查看模式打开会话通过Spring的的 OpenSessionInViewFilter
    3. 请参阅#3。

    4. 处理应该处理的异常 - 换句话说,这是一个设计决定。但是,请注意,默认情况下Spring事务框架回滚未检查的异常,但未检查,以匹配EJB规范的行为。请确保在使用检查的异常的任何地方设置适当的回滚规则(请参阅上一个链接)。

    另外,使用连接池。 Apache Commons DBCP 是一个不错的选择。 连接使用量没有太多泄漏是不够的。你必须有零连接泄漏。依靠Spring来管理你的资源将有助于确保这一点。至于其他任何性能问题,请勿尝试过早优化。等到你看到你的问题所在的地方,然后找出解决每个问题的最佳方法。由于您的瓶颈很可能与数据库相关,请查看 Hibernate参考的性能章节来了解你所面临的问题。它涵盖了缓存和获取策略的重要概念。


    If we have a web application which has

    • heavy UI (Spring MVC + JQuery with JSON)
    • Hibernate with JPA annotations being the domain model
    • extend Spring-provided DAO to code DAO layer
    • JBOSS being the app server with Oracle as backend
    • Datasource (JNDI) based connection pooling (Not an XA rather Local data source)
    • also has access to multiple data sources (dealing with multiple DB)

    Behaviorally, lot of Data retrieval (70%) and update of data being 30%
    What would be the best practices for the following to effectively consume DB connections and also see to that there is no much leakage at connection usage?

    1. would it be better to opt for Hibernate template based DAOs?
    2. What kind of transaction manager would be suggest-able and should we go for AOP-based transaction managementWhere
    3. where to instantiate session and and where to close the sessions to effectively consume connections from connection pooling.
    4. It is true that we need to handle transactions from Service layer but what happens to sessions would they be waiting for longer time (we are not using any opensessioninviewFilter)
    5. which layer is better to handle the checked exceptions (business exceptions) and runtime exceptions.

    Sorry for this being bit lengthier question, however I see that this is being a common query and I tried consolidating it. Appreciate your patience and guidance. Thanks for your help.

    解决方案

    This sounds like a pretty typical Spring/Hibernate application, so I would recommend following current best practices, which I recently outlined in another answer. Specifically:

    1. Do not extend Spring DAO support classes or use HibernateTemplate. Use the @Repository annotation combined with component scanning, and directly inject the SessionFactory into your DAO.
    2. Use Spring's HibernateTransactionManager, and definitely use declarative transaction management via @Transactional as your default approach.
    3. Let Spring manage that. It will open sessions just in time for transactions by default, but prefer the open session in view pattern enabled by Spring's OpenSessionInViewFilter.
    4. See #3.
    5. Always handle exceptions where they should be handled--in other words, this is a design decision. Note, however, that the Spring transaction framework by default rolls back on unchecked exceptions, but not checked, to match the behavior of the EJB spec. Make sure to set the proper rollback rules (see previous link) anywhere you use checked exceptions.

    Additionally, use a connection pool, obviously. Apache Commons DBCP is a great choice. "Not much leakage in connection usage" isn't enough. You have to have zero connection leakage. Depending on Spring to manage your resources will help ensure this. As for any other performance issues, don't try to optimize prematurely. Wait until you see where your problem areas are, and then figure out the best way to solve each one individually. Since your bottlenecks will most likely be database-related, check out the performance chapter of the Hibernate reference to get an idea what you're up against. It covers the important concepts of caching and fetching strategies.

    这篇关于在分层的Spring MVC Web应用程序中处理Hibernate会话的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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