I was using programmatic transaction management in spring, now I have switched to declarative transaction management.
SessionFactory
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="packagesToScan" value="com.hcentive.cig.domain" />
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
TransactionManager
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<beans:property name="sessionFactory">
<beans:ref bean="sessionFactory" />
</beans:property>
</beans:bean>
Now If run my code
@Override
@Transactional
public Request saveRequest(Request request) {
sessionFactory.getCurrentSession().save(request);
return request;
}
I get exception save is not valid without an active transaction
if I remove below line
<beans:prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</beans:prop>
I get
No CurrentSessionContext configured!
You definitely don't need this setting:
<beans:prop key="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</beans:prop>
Spring Transaction Management layer should bind the Hibernate Session to the current running Thread.
The settings are fine, the only thing that might cause it comes from this statement of yours:
no it is getting called from service layer , and also I have tried moving @ transactional to service layer
You need to expose this method:
Request saveRequest(Request request);
through a Service interface, that you inject in any other component (web or other service layer beans).
To validate this, you can place a debug break-point in the saveRequest
method implementation, and look for the TransactionInterceptor up the call-stack. If it's not there, then Spring couldn't wrap your method call into a Transaction Aspect processing logic.
Having your function annotated as Transactional is not necessarily enough. You also need to ensure that:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With