Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

java - Best way to handle Hibernate Sessions in a layered Spring MVC Web application

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...