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.mzph.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…