Results 1 to 3 of 3

Thread: Spring 3.1.1 and Hibernate 4.0.1 integration problem

  1. #1
    Join Date
    Jan 2012
    Posts
    1

    Default Spring 3.1.1 and Hibernate 4.0.1 integration problem

    I have web application, currently it runs well on spring 3.0.5 and hibernate 3.6.9
    I wanted to switch to hibernate 4.0.1 because of desired special feature in Envers data Auditing

    In documentation and several blogs it's clearly pointed out, that HibernateDaoSupport is obsolete
    and one should use: sessionFactory.getCurrentSession();

    I did rewrite the dao layer, ending up with custom BaseDao class:

    Code:
    public abstract class BaseDao {
        protected SessionFactory sessionFactory;
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
        public Session getCurrentSession() {        
            return sessionFactory.getCurrentSession();
        }
    }
    every Dao class extends BaseDao, and sessionFactory is injected through applicationContext.xml

    Transactions are being set up using AOP and <tx:advisor>

    Code:
        <!-- the transactional advice -->
        <tx:advice id="txAdvice" transaction-manager="txManager">
            <tx:attributes>
                <!-- all methods starting with 'get' are read-only -->
                <tx:method name="get*" read-only="true"  />
                <!-- other methods use the default transaction settings -->
                <tx:method name="*"  />
            </tx:attributes>
        </tx:advice>
    
        <aop:config>
            <aop:pointcut id="minibankServiceOperation" expression="execution(* lt.minibank.service.*.*(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="minibankServiceOperation" order="0"/>
        </aop:config>
    
        <aop:config>
            <aop:pointcut id="externalServiceOperation" expression="execution(* lt.minibank.service.external.*.*.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="externalServiceOperation" order="1"/>
        </aop:config>
    
        <aop:config>
            <aop:pointcut id="configurationServiceOperation" expression="execution(* lt.minibank.service.implementation.Configuration.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="configurationServiceOperation" order="2"/>
        </aop:config>
    
    	<aop:config>
            <aop:pointcut id="statisticsDaoOperation" expression="execution(* lt.minibank.dao.AggregatedLogRecordDao.insert(..))"/>
            <aop:advisor advice-ref="txAdvice" pointcut-ref="statisticsDaoOperation" />
        </aop:config>
    
        <!-- Hibernate's Session Factory -->
        <bean id="sessionFactory"
              class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
                
              <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
        </bean>
        
        
        <!-- Transaction manager for Hibernate -->
        <bean id="txManager"
              class="org.springframework.orm.hibernate4.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
    hibernate.cfg.xml

    Code:
    <hibernate-configuration>
        <session-factory>
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="hibernate.connection.url">jdbc:mysql://${dbHost}/${dbName}?characterEncoding=UTF-8&amp;CharSet=UTF8</property>
            <property name="hibernate.connection.username">${dbUsername}</property>
            <property name="hibernate.connection.password">${dbPassword}</property>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    	  			   
        <!-- Use the C3P0 connection pool provider --> 
            <property name="hibernate.c3p0.min_size">5</property>
            <property name="hibernate.c3p0.max_size">20</property>
            <property name="hibernate.c3p0.timeout">300</property>
            <property name="hibernate.c3p0.max_statements">50</property>
            <property name="hibernate.c3p0.idle_test_period">3000</property>
          
        <!-- List of XML mapping files -->
            <mapping resource="Client.hbm.xml" />
            <mapping resource="ClientLog.hbm.xml" />
       ............
    two things that catches my eye are: :

    Invocation of init method failed; nested exception is org.hibernate.HibernateException: No Session found for current thread

    Bean "Configuration"

    <bean id="configuration" class="lt.minibank.service.implementation.Configur ation" init-method="init">
    <constructor-arg ref="configurationValueDao" />
    </bean>

    On Init fetches some values from DB, using sessionFactory.getCurrentSession();

    I have the feeling that transactions are not initialized, and that's results in "No Session Found" although i did
    specifically write AOP:
    Code:
        <aop:config>
            <aop:pointcut id="configurationServiceOperation" expression="execution(* lt.minibank.service.implementation.Configuration.*(..))" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="configurationServiceOperation" order="2"/>
        </aop:config>

  2. #2
    Join Date
    Jan 2008
    Location
    Mohnton, PA USA (that's near Philadelphia)
    Posts
    2,148

    Default

    Please repost your question in Spring Data forum or Spring Core. This forum is for Spring Integration project

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Apart from the wrong forum this question has also been answered numerous times before. transactions (AOP basically) hasn't been applied when the init method is called, you need to do manual transaction management (using a TransactionTemplate or PlatformTransactionManager) directly.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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