Results 1 to 3 of 3

Thread: Hibernate session shared across transaction boundary.

  1. #1

    Default Hibernate session shared across transaction boundary.

    I have got a a logic class which uses a number of dao's and is configured to use declarative transactions. All my dao's use the spring hibernate dao template. It seems the same hibernate session object is being shared across multiple method calls to the UserLogic class. With the configuration I have I was expecting spring to start a new transaction (and get a new session from the session factory) and then commit transaction if no unchecked exceptions were thrown (and remove the session so it would not be used in future calls). I know I am getting the same session because if I call an update method after a get method I get an exception thrown (below). Has anyone had this problem or am I doing something stupid.


    Exception:
    -----------------------------------------------------------------------------------------

    org.springframework.orm.hibernate.HibernateSystemE xception: a different object with the same identifier value was already associated with the session: 4028a0fe02a16cd00102a16cdd810001, of class: com.shr2shr.world.domain.entity.User; nested exception is net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 4028a0fe02a16cd00102a16cdd810001, of class: com.shr2shr.world.domain.entity.User
    net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 4028a0fe02a16cd00102a16cdd810001, of class: com.shr2shr.world.domain.entity.User

    Spring config
    --------------------------------------------------------------------------------------------


    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.Pr opertyPlaceholderConfigurer">
    <property name="locations">
    <list>
    <value>hibernate.properties</value>
    </list>
    </property>
    </bean>

    <!-- Connection pool and hibernate session factory set up -->
    <bean id="WorldDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName">
    <value>${hibernate.connection.driver_class}</value>
    </property>
    <property name="url">
    <value>${hibernate.connection.url}</value>
    </property>
    <property name="username">
    <value>${hibernate.connection.username}</value>
    </property>
    <property name="password">
    <value>${hibernate.connection.password}</value>
    </property>
    </bean>

    <bean id="SessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="dataSource"><ref local="WorldDataSource"/></property>
    <property name="mappingResources">
    <list>
    <value>com/shr2shr/world/domain/entity/User.hbm.xml</value>
    <value>com/shr2shr/world/domain/entity/UserGroup.hbm.xml</value>
    <value>com/shr2shr/world/domain/entity/Location.hbm.xml</value>
    <value>com/shr2shr/world/domain/entity/MenuItem.hbm.xml</value>
    </list>
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.P ostgreSQLDialect</prop>
    <prop key="hibernate.query.substitutions">true=1 false=0</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    </bean>

    <!-- Transaction manager and interecptor set up -->
    <bean id="TransactionManager"
    class="org.springframework.orm.hibernate.Hibernate TransactionManager">
    <property name="sessionFactory"><ref local="SessionFactory"/></property>
    </bean>

    <bean id="BaseTransactionProxy" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean"
    abstract="true">
    <property name="transactionManager"><ref bean="TransactionManager"/></property>
    <property name="transactionAttributes">
    <props>
    <prop key="create*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="remove*">PROPAGATION_REQUIRED</prop>
    <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
    </property>
    </bean>

    <!-- Dao config -->
    <bean id="UserDao" class="com.shr2shr.world.domain.dao.hibernate.User DaoHibernate">
    <property name="sessionFactory"><ref local="SessionFactory"/></property>
    </bean>

    <bean id="UserGroupDao" class="com.shr2shr.world.domain.dao.hibernate.User GroupDaoHibernate">
    <property name="sessionFactory"><ref local="SessionFactory"/></property>
    </bean>

    <bean id="MenuItemDao" class="com.shr2shr.world.domain.dao.hibernate.Menu ItemDaoHibernate">
    <property name="sessionFactory"><ref local="SessionFactory"/></property>
    </bean>

    <!-- Logic config -->
    <bean id="UserLogic" parent="BaseTransactionProxy">
    <property name="target">
    <bean class="com.shr2shr.world.domain.logic.UserLogicImp l">
    <constructor-arg index="0"><ref local="UserDao"/></constructor-arg>
    <constructor-arg index="1"><ref local="UserGroupDao"/></constructor-arg>
    <constructor-arg index="2"><ref local="MenuItemDao"/></constructor-arg>
    </bean>
    </property>
    </bean>
    </beans>

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Code:
    <!-- Logic config --> 
    <bean id="UserLogic" parent="BaseTransactionProxy"> 
    <property name="target"> 
    <bean class="com.shr2shr.world.domain.logic.UserLogicImpl"> 
    <constructor-arg index="0"><ref local="UserDao"/></constructor-arg> 
    <constructor-arg index="1"><ref local="UserGroupDao"/></constructor-arg> 
    <constructor-arg index="2"><ref local="MenuItemDao"/></constructor-arg> 
    </bean> 
    </property> 
    </bean> 
    </beans>
    This config means Spring HibernateTransactionManager will start (if necessary) a new transaction whenever a method is called on UserLogicImpl and commit/rollback after the method returns. So if you use two DAOs inside the same UserLogicImpl method, they will share the same Hibernate Session.
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    b/w
    Code:
    org.springframework.orm.hibernate.HibernateSystemException&#58; a different object with the same identifier value was already associated with the session&#58; 4028a0fe02a16cd00102a16cdd810001, of class&#58; com.shr2shr.world.domain.entity.User; nested exception is net.sf.hibernate.NonUniqueObjectException&#58; a different object with the same identifier value was already associated with the session&#58; 4028a0fe02a16cd00102a16cdd810001, of class&#58; com.shr2shr.world.domain.entity.User 
    net.sf.hibernate.NonUniqueObjectException&#58; a different object with the same identifier value was already associated with the session&#58; 4028a0fe02a16cd00102a16cdd810001, of class&#58; com.shr2shr.world.domain.entity.User
    this exception occurs whenever you try to associate an object to the session while an other object, with the same key, is already associated to the session. You can reproduce this exception inside your DAO!!!
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  2. Replies: 3
    Last Post: May 16th, 2005, 07:04 AM
  3. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  4. Replies: 3
    Last Post: Oct 1st, 2004, 03:56 AM
  5. Transaction pb Hibernate/MySQL
    By syluser in forum Data
    Replies: 2
    Last Post: Aug 28th, 2004, 02:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •