多執行緒出現HibernateException: Could not obtain transaction-synchronized Session for current thread
使用hibernate4的SessionFactory獲取Session時有兩種方式
1、sessionFactory.getCurrentSession()
2、sessionFactory.openSession(),開啟新session,需要關閉
當使用1獲取CurrentSession,不會開啟新的session,速度很快。
但是如果你是使用多執行緒的時候,就不能使用getCurrentSession,這樣會報以下錯誤:HibernateException: Could not obtain transaction-synchronized Session for current thread
這是因為這spring事務中,在一個執行緒中獲取的connection都是同一個,並且用threadlocal儲存這個connnection.如果在多執行緒中,那就不是在同一個執行緒中了,因此就會發生上述的問題。
解決方案一:
將getCurrentSession()需要改成openSession()方式就可以解決了
方案二:
新建一個class檔案,把操作資料庫的程式碼寫在這裡,並且新增@Transactional,就可以了。
相關推薦
多執行緒出現HibernateException: Could not obtain transaction-synchronized Session for current thread
使用hibernate4的SessionFactory獲取Session時有兩種方式 1、sessionFactory.getCurrentSession() 2、sessionFactory.openSession(),開啟新session,需要關閉 當使用1
Could not obtain transaction-synchronized Session for current thread
ram form string spa 實現 sql 新的 line nal 今天運行程序時 報了 如下的錯誤。 Could not obtain transaction-synchronized Session for current thread 在Spring中使
Could not obtain transaction-synchronized Session for current thread原因及解決方案
在開發中,碰到到了Could not obtain transaction-synchronized Session for current thread異常,因此特意記錄下。 一、問題的產生  
Hibernate4中使用getCurrentSession報Could not obtain transaction-synchronized Session for current thread
Dao層直接注入的sessionFactory,然後用getCurrentSession方法獲取session,然後問題來了,直接報錯:
spring hibernate整合: Could not obtain transaction-synchronized Session for current thread
當hibernate 將事務交由 spring管理時, 應將 @Transactional 加入對接的介面層,即其它物件呼叫的方法上,若將@Transactional加在內部方法上,將會出現 Could not obtain transacti
解決Could not obtain transaction-synchronized Session for current thread這個問題
關於出現 這個問題 我花了兩天時間查詢資料 最終是從同事的專案中無意中找到了解決方法 我的專案是spring mvc +hibernate 接下來請注意 你的問題 可能 是出現 父子容器上 spring和springmvc 分別會有一個容器
關於Hibernate Could not obtain transaction-synchronized Session for current thread
最近幾年一直再搞android,最近閒下來了,順便玩一下web。 整了個最新版本的SSH(hibernate5.1.0+spring4.2.6+struts-2.5) 在寫dao實現類的時候碰到一個問題, @Repository public class UserDaoIm
使用ssh出現 Could not obtain transaction-synchronized Session for current thre情況
這個除錯是基於Hibernate4 的getSessionFactory().getCurrentSession()那個Hibernate3的HibernateTemplate().get(entit
gdb除錯多執行緒出現的死鎖
多執行緒的條件下,程式很容易出現死鎖,此時各個執行緒處於等待狀態,可以通過gdb除錯找到死鎖出現的地方。 例子: #include <stdio.h> #include <pthread.h> #include <uni
使用多執行緒出現: java.util.ConcurrentModificationException的問題。
在一個批量Insert業務中,使用多執行緒進行約束insert。其中出現的一個問題。 部分資料是成功insert的。 百度得知此問題發生的原因是:在迴圈迭代中,如果對Vetor、ArrayList進行修改,就可能對丟擲這個異常。仔細錯誤檢查程式碼:
ros訂閱函式的多執行緒使用方法(C++: 外部變數控制跳出for迴圈)
最初是想實現接收到某個some_topic然後解析內容,若是內容為指令action,action函式進入for迴圈,若是再接收到some_topic,裡面指令為stop則stop=true停止正在進行的action,跳出for迴圈(想
Java多執行緒之生產者消費者問題:使用synchronized 關鍵字解決生產者消費者問題
今天看了一片博文,講Java多執行緒之執行緒的協作,其中作者用程式例項說明了生產者和消費者問題,但我及其他讀者發現程式多跑幾次還是會出現死鎖,百度搜了下大都數的例子也都存在bug,經過仔細研究發現其中的問題,並解決了,感覺有意義貼出來分享下。
解決多執行緒安全問題-無非兩個方法synchronized和lock
部落格引用處(以下內容在原有部落格基礎上進行補充或更改,謝謝這些大牛的部落格指導): 解決多執行緒安全問題-無非兩個方法synchronized和lock 具體原理(百度) 還有其他的鎖,如果想要了解,參考:JAVA鎖機制-可重入鎖,可中斷鎖,公平鎖,讀寫鎖,自旋鎖, 解決多執行緒的
多執行緒實現方式1:自定義一個類,繼承Thread類,並重寫run方法。
/** * 多執行緒實現方式1: * 自定義一個類,繼承Thread類,並重寫run方法。 * 執行緒同時執行。 * @author jiaxutianhuo * */ public cl
SimpleDateFormat時間格式化高併發、多執行緒時出現問題
SimpleDateFormat是是 Java 中一個非常常用的類,該類用來對日期字串進行解析和格式化輸出,但是DateFormat 和 SimpleDateFormat 類不都是執行緒安全的,在生產環境的多執行緒或高併發情況使用 format() 和 parse() 方法,會出現很多問題:
python中多執行緒開啟的兩種方式(內含有event的應用,即安全的機制,類似於java的等待喚醒機制,不會出現多個執行緒之間的錯亂問題)
event是類似於java中的等待喚醒機制,具體方法參照上一篇CSDN 下面來介紹開啟執行緒的第一種方式 #Filename:threading1.py #開啟執行緒的第一種方式 import threading import time event=threadin
多執行緒不加鎖會出現錯誤
這是一個會出現錯誤的程式,原本設想的是能夠通過多執行緒操作,實現一個標誌的遞減,在關鍵地方新增Thread.Sleep()函式,就可以模仿不加鎖出現的錯誤 public class RunableDemo { public static void main(String[] args
Java多執行緒程式設計-(11)-面試常客ThreadLocal出現OOM記憶體溢位的場景和原理分析
一、案例程式碼 1、首先看一下程式碼,模擬了一個執行緒數為500的執行緒池,所有執行緒共享一個ThreadLocal變數,每一個執行緒執行的時候插入一個大的List集合: 2、設定JVM引數設定最大記憶體為256M,以便模擬出OOM: 3、執行程式碼,輸出結果: 可以看出,單執行緒池
python多執行緒操作報錯:No handlers could be found for logger "websocket"
用python模擬對講機線上工具,在伺服器上成功連線的連線上限是1020個,需要確定遇到這個瓶頸的原因 1.連線失敗時的報錯資訊為 No handlers could be found for logger "websocket" 解決方法: import logging
openwrt多執行緒程式交叉編譯時候出現錯誤解決方法
[email protected]:~/smb/lc_smarthome$ mipsel-openwrt-linux-gcc lc_tty.c lc_tty.h main.c -lpthread -o main 會出現一下錯誤: /work/openwrt/tru