Hibernate4中使用getCurrentSession報Could not obtain transaction-synchronized Session for current thread

來源:互聯網
上載者:User

標籤:

架個spring4+hibernate4的demo,dao層直接注入的sessionFactory,然後用getCurrentSession方法擷取session,然後問題來了,直接報錯:

Could not obtain transaction-synchronized Session for current thread

提示無法擷取當前線程的事務同步session,略微奇怪,這和事務有什麼關係..然後百度一下有人說改成用openSession方法就好了,那我又百度了一下這2個方法的區別:

(1)openSession每次開啟都是新的Session,所以多次擷取的Session執行個體是不同的,並且需要人為的調用close方法進行Session關閉。(2)getCurrentSession是從當前上下文中擷取Session並且會綁定到當前線程,第一次調用時會建立一個Session執行個體,如果該Session未關閉,後續多次擷取的是同一個Session執行個體;事務提交或者復原時會自動關閉Sesison,無需人工關閉。

看起來這個getCurrentSession方法的確和事務有點關係.然後我加上事務:

    <bean id="transactionManager"        class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory">            <ref bean="sessionFactory" />        </property>    </bean>    <tx:annotation-driven transaction-manager="transactionManager"/>

然後在dao層加上@Transaction註解,再運行ok了..不過好奇驅使吧,看了一下getCurrentSession的源碼(我的demo中用的spring的實作類別),關鍵點:

if (TransactionSynchronizationManager.isSynchronizationActive()) {            Session session = this.sessionFactory.openSession();            if (TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {                session.setFlushMode(FlushMode.MANUAL);            }            SessionHolder sessionHolder = new SessionHolder(session);            TransactionSynchronizationManager.registerSynchronization(                    new SpringSessionSynchronization(sessionHolder, this.sessionFactory, true));            TransactionSynchronizationManager.bindResource(this.sessionFactory, sessionHolder);            sessionHolder.setSynchronizedWithTransaction(true);            return session; } else {            throw new HibernateException("Could not obtain transaction-synchronized Session for current thread"); }

然後點進去看了一下isSynchronizationActive()方法:

public static boolean isSynchronizationActive() {        return (synchronizations.get() != null);    }

get方法說明:

Returns the value in the current thread‘s copy of this thread-local variable. If the variable has no value for the current thread, it is first initialized to the value returned by an invocation of the initialValue method.

然後再看initialValue的說明:

This implementation simply returns null; if the programmer desires thread-local variables to have an initial value other than null, ThreadLocal must be subclassed, and this method overridden. Typically, an anonymous inner class will be used.

到此問題明了了,補充一點之前設定檔中配了事務,不過是原來那種在配置中根據方法名字來定義事務傳播的方式,但是在dao中並沒有繼承它,故實際上是沒有事務的,只有實現.而在spring的事務實現中需要判斷當前線程中的事務是否同步,而沒有事務的時候,那個判斷是否同步的方法會因為get返回初始的null值而返回false,最終導致throw一個Could not obtain transaction-synchronized Session for current thread的異常.

綜上:spring4+hibernate4,使用hibernate的api的時候需要配置事務的,如果不配置事務會導致擷取當前session拋出異常.

Hibernate4中使用getCurrentSession報Could not obtain transaction-synchronized Session for current thread

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.