1. 程式人生 > >@Transactional未新增就報錯:No Hibernate Session bound to thread, and configuration does not allow creation

@Transactional未新增就報錯:No Hibernate Session bound to thread, and configuration does not allow creation

我的事務是通過<tx:annotation-driven的註解方式去標註的。@Transactional未新增就預設為readonly狀態。

之前一直執行的好好的,但是,最近在新的工程裡面加這個功能,servcie裡面的方法只要沒加上@Transactional,就會報錯:

No Hibernate Session bound to thread, and configuration does not allow creation。

這是告訴我沒有設定事務。只有加上@Transactional才會正常。

查了半天才發現,之前@Transactional未新增就預設為readonly狀態,是在web.xml裡面設定了:
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

我給加上就行了,不過這種情況對於單元測試可能就有問題了