0

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?

jubi
  • 485
  • 7
  • 19

2 Answers2

2

Transaction is needed, you can mark this method as read-only

@Transactional(readOnly = true)
public Publisher findByName(String name) {
   ...
}

From spring documentation:

Read-only status: a read-only transaction does not modify any data. Read-only transactions can be a useful optimization in some cases (such as when using Hibernate).

From JBoss Wiki

Many application developers think they can talk to a database outside of a transaction. This obviously isn’t possible; no SQL statement can be send to a database outside of a database transaction. The term nontransactional data access means there are no explicit transaction boundaries, no system transaction, and that the behavior of data access is that of the autocommit mode. It doesn’t mean no physical database transactions are involved.

More:

Community
  • 1
  • 1
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • ok. but it still opens a transaction... i want to run queries directly without having my orm to open / begin a transaction and finally commit it. but if i correctly understood the first answer from your posted question link, the reason for my problem is hibernates internal behaviour... – jubi Nov 14 '13 at 13:43
  • 1
    I have updated my answer: "no SQL statement can be send to a database outside of a database transaction" – MariuszS Nov 14 '13 at 13:44
0
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory">
       <ref bean="mySessionFactory"/>
   </property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

add above definition, and make your service or dao class marked with @Transactional annotation.

Terry Zhao
  • 115
  • 7
  • 1
    hm. i think this is not necessary because i configuered my app with Java based Spring Configuration – jubi Nov 14 '13 at 13:37
  • You can enable transactions with JavaConfig (@EnableTransactionManagement), this is not needed. – MariuszS Nov 14 '13 at 13:39