-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Hibernate & Spring
PostPosted: Fri Mar 12, 2004 2:26 am 
Beginner
Beginner

Joined: Tue Feb 17, 2004 7:53 am
Posts: 24
Is there an advantage to using Spring to manage the session factory and transactions?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 3:23 am 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
Have a look at the article at http://www.hibernate.org/110.html. It gives an overview of what values Spring can add to Hibernate. Two important values in the middle tier area are:

Configuration management

Spring can wire your entire middle tier in a loosely coupled fashion (yep, those IoC and AOP buzzwords ;-):

- resources like JDBC data sources and Hibernate session factories
- data access objects that access those resources
- business objects that delegate to your DAOs for persistence

This completely avoids the need for custom singletons or the like that hold configuration information, SessionFactory references, etc. You have one homogeneous style of configuration throughout your middle tier then. Spring cares for the entire lifecycle of your middle tier objects.

Spring provides means to link in JNDI resources into such definitions, wiring them with your application objects, without the latter being aware of JNDI. Alternatively, you can easily use local resource definitions, like a local Commons DBCP BasicDataSource.

Spring's LocalSessionFactoryBean is a convenient way of setting up a Hibernate SessionFactory in a Spring context, as an alternative to fetching the SessionFactory from JNDI. It provides all of Hibernate's configuration power, avoiding the need for a separate hibernate.cfg.xml file.

Transaction management

Transaction management is one of the most important Spring features. You have the choice between programmatic and declarative transaction demarcation, with pluggable transaction strategies: JTA, JDBC, Hibernate, JDO. (You just need JTA for actually distributed transactions.)

A popular usage is to proxy your business objects with Spring's AOP TransactionProxyFactoryBean, with HibernateTransactionManager as strategy. Each business service invocation will then automatically execute within a proper Hibernate transaction; DAOs will simply participate.

So Spring helps to separate transaction and data access concerns, avoiding the need for passing around Hibernate Sessions or using custom ThreadLocals. Multiple DAO invocations within a single transaction are seamless to achieve, reusing the same Hibernate Session.

Essentially, Spring's middle tier support offers similar services like local Session Beans but for POJOs - just like Hibernate offers persistence for POJOs as alternative to the Entity Beans component model. Spring and Hibernate are absolutely complementary here.

Juergen


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 13, 2004 11:05 am 
Newbie

Joined: Mon Sep 08, 2003 12:16 am
Posts: 9
Hi, I have a question about this statement:

Multiple DAO invocations within a single transaction are seamless to achieve, reusing the same Hibernate Session.


I'm would like the same Hibernate Session (and Transaction) to be used accross multiple DAO's but Spring seems to be getting a new Session each time SessionFactoryUtils.getSession(...) is called. Here's the simplest example I can give:

Code:
public class ProductManagerDaoHibernate extends HibernateDaoSupport {
  public List getProductList() {
    logger.debug("GET SESSION 1: " + SessionFactoryUtils.getSession(getSessionFactory(), true));
    logger.debug("GET SESSION 2: " + SessionFactoryUtils.getSession(getSessionFactory(), true));
    ...
  }
}


which gives this output:

Code:
2004-03-12 15:31:08,598 DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Opening Hibernate session
2004-03-12 15:31:08,775 DEBUG net.sf.hibernate.impl.SessionImpl - opened session
2004-03-12 15:31:08,776 DEBUG hibernate.ProductManagerDaoHibernate - GET SESSION 1: net.sf.hibernate.impl.SessionImpl@84416d
2004-03-12 15:31:08,776 DEBUG org.springframework.orm.hibernate.SessionFactoryUtils - Opening Hibernate session
2004-03-12 15:31:08,777 DEBUG net.sf.hibernate.impl.SessionImpl - opened session
2004-03-12 15:31:08,777 DEBUG hibernate.ProductManagerDaoHibernate - GET SESSION 2: net.sf.hibernate.impl.SessionImpl@b526df


Am I correct that this is creating 2 Sessions?

Is this the expected behavior or am I doing something wrong? Do I need to use OpenSessionInViewInterceptor or OpenSessionInViewFilter to do what I want?

Here is part of my applicationContext.xml

Code:
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
   <property name="driverClassName"><value>${jdbc.driver}</value></property>
   <property name="url"><value>${jdbc.url}</value></property>
   <property name="username"><value>${jdbc.username}</value></property>
   <property name="password"><value>${jdbc.password}</value></property>
</bean>   

<bean id="mySessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
   <property name="mappingResources">
     <list>
      <value>hibernate/product.hbm.xml</value>
     </list>
   </property>
   <property name="hibernateProperties">
     <props>
      <prop key="hibernate.dialect">${hibernate.dialect}</prop>
      <prop key="hibernate.query.substitutions">true=1 false=0</prop>
      <prop key="hibernate.show_sql">true</prop>
     </props>
   </property>
   <property name="dataSource"><ref local="myDataSource"/></property>
</bean>

<bean id="productManagerDao" class="hibernate.ProductManagerDaoHibernate">
   <property name="sessionFactory"><ref local="mySessionFactory"/></property>
</bean>



I am running Spring rc2, Hibernate 2.1 with Tomcat and MySQL

Thanks for any help!

- Ryan


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 14, 2004 7:32 am 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
Ryan,

You don't seem to demarcate a transaction anywhere: In this case, each of your data access invocations will execute non-transactionally, i.e. within its own Hibernate Session.

As a side note, you need to close the Sessions you open with SessionFactoryUtils.getSession, via SessionFactoryUtils.closeSessionIfNecessary. Consider HibernateTemplate for a convenient one-line approach, handling Sessions under the hood.

You can demarcate transactions programmatically or declaratively: In the latter case, a simple way is to proxy your DAO (or a higher-level business object that delegates to your DAOs) with a TransactionProxyFactoryBean. Your data access logic will seamlessly participate in any such transaction, allowing for flexible reuse behind a variety of business facades, with a variety of transaction strategies.

Have a look at sections 5 to 7 of the article at http://www.hibernate.org/110.html for a discussion, and at the Petclinic sample app that comes with the Spring distribution for a working example of transactions with a Hibernate DAO.

Juergen


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 12:35 am 
Newbie

Joined: Mon Sep 08, 2003 12:16 am
Posts: 9
Ok, I guess somehow I missed that setting up transactions is required. I think I assumed Spring would use the session-per-request pattern rather than session-per-operation by default (http://hibernate.bluemars.net/168.html)

It works great now, thanks.

- Ryan


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.