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:
every Dao class extends BaseDao, and sessionFactory is injected through applicationContext.xmlCode:public abstract class BaseDao { protected SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public Session getCurrentSession() { return sessionFactory.getCurrentSession(); } }
Transactions are being set up using AOP and <tx:advisor>
hibernate.cfg.xmlCode:<!-- 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>
two things that catches my eye are: :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&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" /> ............
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>


Reply With Quote