i am currently writing a spring-webmvc app and some days ago, my first problem was that i didn´t know how to use transactions with Hibernate and Spring. Now everything works, a transactionsmanager is up living and i can successfully work with transactions.
This is the part of my Java based spring config:
@Configuration
@ComponentScan(basePackages = { "de.macomp.maipds" })
@EnableTransactionManagement
public class MainConfigLib {
...
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager manager = new HibernateTransactionManager(sessionFactory());
return manager;
}
}
So, when i want a method in my service- or dao-layer to run in a transaction i simply annotate it with @Transactional and everything´s ok. My problem is, when i want a service method not to run in a transaction, i am getting the following error:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
The code where this happens looks like:
...
@Override
public Publisher findByName(String name) {
Session session = this.sessionFactory.getCurrentSession();
//bam!
...
Can i "force" my environment somehow to give me a "non-transactional" session?! The transactionManager property of the sessionFactory in my DAO is null.
Adding @Transactional to "findByName" solves the problem, but for this method no transaction is needed...
Any ideas?