java jdbc事務管理_hibernate事務管理 (jdbc jta)

評論

#?re: hibernate事務管理 (jdbc jta)

2007-07-29 10:18

pig

JTA事務的開始

Transaction tx = session.beginTransaction();

應該不是這樣吧,應該是從容器中獲得。??回復??更多評論

#?re: hibernate事務管理 (jdbc jta)

2007-07-29 12:35

slx

@pig

建議看看hibernate reference 事務處理 jta部分。

11.2.2. Using JTA

If your persistence layer runs in an application server (e.g. behind EJB session beans), every datasource connection obtained by Hibernate will automatically be part of the global JTA transaction. You can also install a standalone JTA implementation and use it without EJB. Hibernate offers two strategies for JTA integration.

If you use bean-managed transactions (BMT) Hibernate will tell the application server to start and end a BMT transaction if you use the Transaction API. So, the transaction management code is identical to the non-managed environment.

// BMT idiom

Session sess = factory.openSession();

Transaction tx = null;

try {

tx = sess.beginTransaction();

// do some work

...

tx.commit();

}

catch (RuntimeException e) {

if (tx != null) tx.rollback();

throw e; // or display error message

}

finally {

sess.close();

}

If you want to use a transaction-bound Session, that is, the getCurrentSession() functionality for easy context propagation, you will have to use the JTA UserTransaction API directly:

// BMT idiom with getCurrentSession()

try {

UserTransaction tx = (UserTransaction)new InitialContext()

.lookup("java:comp/UserTransaction");

tx.begin();

// Do some work on Session bound to transaction

factory.getCurrentSession().load(...);

factory.getCurrentSession().persist(...);

tx.commit();

}

catch (RuntimeException e) {

tx.rollback();

throw e; // or display error message

}

With CMT, transaction demarcation is done in session bean deployment descriptors, not programatically, hence, the code is reduced to:

// CMT idiom

Session sess = factory.getCurrentSession();

// do some work

...

In a CMT/EJB even rollback happens automatically, since an unhandled RuntimeException thrown by a session bean method tells the container to set the global transaction to rollback. This means you do not need to use the Hibernate Transaction API at all with BMT or CMT, and you get automatic propagation of the "current" Session bound to the transaction.

Note that you should choose org.hibernate.transaction.JTATransactionFactory if you use JTA directly (BMT), and org.hibernate.transaction.CMTTransactionFactory in a CMT session bean, when you configure Hibernate's transaction factory. Remember to also set hibernate.transaction.manager_lookup_class. Furthermore, make sure that your hibernate.current_session_context_class is either unset (backwards compatiblity), or set to "jta".

The getCurrentSession() operation has one downside in a JTA environment. There is one caveat to the use of after_statement connection release mode, which is then used by default. Due to a silly limitation of the JTA spec, it is not possible for Hibernate to automatically clean up any unclosed ScrollableResults or Iterator instances returned by scroll() or iterate(). You must release the underlying database cursor by calling ScrollableResults.close() or Hibernate.close(Iterator) explicity from a finally block. (Of course, most applications can easily avoid using scroll() or iterate() at all from the JTA or CMT code.)

回復??更多評論

#?re: hibernate事務管理 (jdbc jta)

2007-11-03 06:17

jeadu

pig 所說的是JTA規范中定義的寫法,而你所說的是經過hibernate包換的寫法。??回復??更多評論

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/372760.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/372760.shtml
英文地址,請注明出處:http://en.pswp.cn/news/372760.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

@Resource VS @Autowired

Resource 和 Autowired 均是用于bean注入的注解,都可以寫在字段和setter方法上,如果都寫在字段上,就無需寫setter方法。 Autowired 由Spring的org.springframework.beans.factory.annotation.Autowired提供 默認byType方式注入,并且對象不能為…

用于Spring應用程序的Gradle原型

我發布了Gradle原型,可用于基于Springframework創建Java / Groovy應用程序。 當然,它不是一個真正的原型,因為這樣的創作是不可能的 。不過,你可以創建,編輯和部署應用服務器很少的步驟。 對于可部署的軟件項目而言&am…

java tm無響應_Java(TM) Platform SE binary 未響應 是怎么個情況?

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓988098 [Thread-10] INFO sound.oo0O - Creating streaming player for music with id [faction_pirate_encounter_02_hostile.ogg]988099 [Thread-10] INFO sound.OooO - Playing music with id [faction_pirate_encounter_02_hos…

ROS and PCL install

ROS hydro安裝指南: http://wiki.ros.org/cn/hydro/Installation/Ubuntu (加ppa源后直接安裝) Linux OpenCV安裝指南:http://blog.sciencenet.cn/blog-571755-694742.html (從源代碼編譯) PCL:…

揭開Python科學計算的面紗

春牛春杖。無限春風來海上。便與春工。染得桃紅似肉紅。 春幡春勝。一陣春風吹酒醒。不似天涯。卷起楊花似雪花。 標準的Python中用列表保存一組值,可以當做數組使用,但是由于其值類型任意,所以列表中保存的是指針,這樣的話保存一…

FXML:使用BuilderFactory的自定義組件

當您想使用FXML時,您將需要能夠添加自己的組件。 這很容易,您只需要添加一個import語句。 FXML文件中以大寫字母開頭的元素將被解釋為實例,如果它們是Java Bean,則最重要:如果它們具有無參數的標準構造函數&#xff0c…

Excel 一鍵上傳到數據庫

<a class"edit" id"batchImport"> 批量導入 </a> js代碼彈窗&#xff1a; $("#batchImport").click(function(){ //彈窗彈窗下列內容 var html<form id"execlForm" method"post" enctype&quo…

SQL——實例記錄(對查詢結果排行號)

select 訂單編號, DENSE_RANK() over(order by 訂單編號) from test 排序結果&#xff1a; 55678-0-1 1 55678-0-1 1 33454-0-1 2 33454-0-1 2 33454-0-1 2 這種是按照訂單不同的順序依次往后排 當然也可以在 over后面加上你想要的起始號 例&#xff1a;DE…

TeamCity構建依賴項

介紹 構建依存關系的主題既不重要也不是次要的。 各種構建工具從不同的角度處理此主題&#xff0c;從而提供各種解決方案&#xff0c;每種解決方案都有其優點和缺點。 熟悉發行版和快照依賴項的Maven和Gradle用戶可能不了解TeamCity快照依賴項&#xff0c;或者認為他們與Maven…

復選框操作checked選中為true,反之為False,也可以賦值為true,false

轉載于:https://www.cnblogs.com/shiluoliming/p/6518236.html

java 個稅計算_【JAVA300例】10、計算個人所得稅

邏輯是這樣的。每個等級計算的系數都不一樣。分別有多個檔位。要判斷處于什么檔位然后用特殊的公式去計算。原版是從小到大判斷&#xff0c;每次寫條件很煩。這里換成從大到小。節省敲代碼時間。import java.util.Scanner;public class Test010{public static void main(String…

Java是否越來越接受靜態導入?

曾經有一段時間&#xff0c;至少在禮貌的社會中&#xff0c;人們普遍認為使用“ 不是 ”一詞是不可接受的。 確實&#xff0c;那時&#xff08;也許直到今天&#xff09;&#xff0c;許多人確實&#xff08;也確實&#xff09;認為不是一個真實的詞。 盡管這個詞并沒有 引起爭議…

Stream 工具方法

inputstream 轉 string 1、使用字符流 InputStream is TestZhimaCustomerCertificationInitialize.class.getClassLoader().getResourceAsStream("config/rsa_private_key_pkcs8.pem"); InputStreamReader isr new InputStreamReader(is); BufferedReader br new…

從0開始學習 GitHub 系列匯總筆記

本文學習自Stromzhang, 原文地址請移步&#xff1a;從0開始學習 GitHub 系列匯總 我的筆記&#xff1a; 0x00 從0開始學習GitHub 系列之[初識GitHub] GitHub 影響力 a.全球頂級科技公司紛紛加入 GitHub &#xff0c;并貢獻他們自己的項目代碼 Google: https://github.com/goog…

Drools Guvnor –管理訪問

外部化業務或技術規則對于可伸縮應用程序非常重要&#xff0c;但是應該管理BRMS服務訪問。 guvnor使用基于角色的授權提供控件UI訪問和操作。 在drools-guvnor參考手冊中列出了幾種權限類型。 具有所有權限的管理員。 分析師或只讀分析師&#xff1a;特定類別的分析師權限。 軟…

java文件操作和_JAVA文件操作類和文件夾的操作

JAVA文件操作類和文件夾的操作package com.gamvan.tools;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import jav…

CCNA基礎知識摘錄

cisco設備的啟動要點&#xff1a;1、檢測硬件(保存在rom)2、載入軟件&#xff08;IOS&#xff09;&#xff08;保存在Flash&#xff09;3、調入配置文件&#xff08;密碼&#xff0c;IP地址&#xff0c;路由協議都保存在此&#xff09;&#xff08;此文件保存在NVRAM&#xff0…

【VS開發】IP地址格式轉換(htonl、ntohl;inet_addr、inet_ntoa)

1、htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u_long netlong); u_short PASCAL FAR ntohs (u_short netshort); ntohl( )-----網絡順序轉換成主機順序 u_long PASCAL FAR htonl (u_long hostlong); u_short PASCAL FAR htons (u_short hostshort); htonl ()-----主機順序轉…

SOA示例應用程序

SOA描述了一組用于創建松散耦合的&#xff0c;基于標準的&#xff0c;與業務相關的服務的模式&#xff0c;由于描述&#xff0c;實現和綁定之間的關注點分離&#xff0c;因此提供了新的靈活性。 近年來&#xff0c;至少在參與大多數信息技術活動的人們中&#xff0c;面向服務的…

java 分貝_java11教程--jhsdb命令

您可以使用該jhsdb工具將Java進程或崩潰的Java虛擬機(JVM)的核心轉儲連接。概要jhsdb clhsdb [--pid pid | --exe executable --core coredump]jhsdb debugd [options] (pid | executable coredump) [server-id]jhsdb hsdb [--pid pid | --exe executable --core coredump]jhsd…