评论

# 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包换的写法。  回复  更多评论

java jdbc事务管理_hibernate事务管理 (jdbc jta)相关推荐

  1. JDBC的入门案列以及JDBC的对事务的管理

    JDBC的概念 Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中 ...

  2. Spring jdbc Template和Spring 事务管理

    使用jdbcTemplate完成增删改查操作(重点) package com.it.jdbctemplate;import java.util.List;import org.junit.Test; ...

  3. java元婴期(21)----java进阶(spring(5)---事务管理AOP事务管理(全自动)spring整合Junit)

    事务管理 事务:一组业务操作ABCD,要么全部成功,要么全部不成功. 特性:ACID 原子性:整体 一致性:完成 隔离性:并发 持久性:结果 隔离问题: 脏读:一个事务读到另一个事务没有提交的数据 不 ...

  4. JDBC事务和JTA事务的区别 --包含spring事务 声明式事务

    2019独角兽企业重金招聘Python工程师标准>>> 一.事务概述 事务表示一个由一系列的数据库操作组成的不可分割的逻辑单位,其中的操作要么全做要么全都不做. 与事务相关的操作主要 ...

  5. jdbctemplate 开启事务_Spring(四):事务管理

    事务 首先,我们要知道事务是什么 构成单一逻辑工作单元的操作集合称为事务 事务的ACID特性 原子性:不可分割的最小操作单位,要么同时成功,要么同时失败 一致性:事务操作前后,数据总量不变 隔离性:多 ...

  6. mybatis和spring jdbc持久层框架事务支持分析

    mybatis和spring jdbc持久层框架事务支持分析 ​ 持久层框架中的事务支持指的是持久层框架如何支持数据库事务,我们先梳理出原生数据库事务操作的主线脉络,它是通过java.sql 包下的C ...

  7. 基于JDBC的数据库连接池高效管理策略

    2019独角兽企业重金招聘Python工程师标准>>> 介绍 在使用Java语言进行和数据库有关的的应用开发中,一般都使用JDBC来进行和数据库的交互,其中有一个关键的概念就是Con ...

  8. JDBC 到 ORM 的事务实现

    一.JDBC 早期SUN公司想编写一套可以连接天下所有数据库的API,但是当他们刚刚开始时就发现这是不可完成的任务,因为各个厂商的数据库服务器差异太大了.后来SUN开始与数据库厂商们讨论,最终得出的结 ...

  9. Spring事务和Aspects框架管理事务,看这篇就够了!(简单易懂!)

    Spring事务和Aspects框架管理事务的用法 一.事务的介绍 1.1什么是事务 1.2事务的四大特性 二.事务的隔离级别以及导致的问题介绍 2.1隔离级别介绍 2.2导致的问题介绍 2.2.1脏 ...

最新文章

  1. 【数据库】mysql报错 编码码1130 和错误码1146
  2. uva 315 (poj 1144 求割点)
  3. 懂你,从选择内裤开始
  4. Android 关于Edittext输入框光标焦点无法及时定位解决办法.
  5. axios如何在nodejs项目里封装_【面经】jq 中 ajax 和 axios 区别,瀑布流布局,添加删除事件...
  6. [原创]flex 3 + .net开发flash Remoting一 --- 开发环境
  7. MVC去掉传参时的验证:从客户端中检测到有潜在危险的Request.QueryString值
  8. Windows 10原创知识题(第三版)
  9. div+js写弹出框
  10. 第二届广东省大学生网络攻防大赛 pyre
  11. 0门槛项目,闲鱼卖特价电影票,免费低价票源
  12. win10怎么添加计算机共享的打印机,win10如何添加打印机共享?
  13. 一只青蛙跳向三个台阶_青蛙跳台阶-递归思想解算
  14. ICON图标设计零基础到精通(UI进阶)
  15. 2015 年最热门的国人开发开源软件 TOP 50
  16. firefox插件推荐
  17. 靶机测试-bulldog
  18. 传奇服务器如何修改装备,架设传奇私服时怎样修改装备图标
  19. 最新的百度网盘不限速下载工具 - 100兆速度理论10m/s
  20. 972信息检索 | 第一章 信息检索概述

热门文章

  1. CSS浏览器兼容性问题
  2. 从零学PyTorch:DataLoader构建高效的自定义数据集
  3. 作为一个算法攻城狮,你训练的算法翻车了该怎么办?
  4. vs2015 html页面没有试图,vs2015 web设计视图假死,求帮助
  5. 易语言微凉模块oracle,跟着微凉学易语言 【简单子类化】
  6. CSDN主页下如何添加访客地图?
  7. mac solr mysql 配置文件_Solr配置文件浅析
  8. npp夜光数据介绍 viirs_科研成果快报第177期:中国地区长时序AVHRR气溶胶数据的主要问题: 气溶胶反演频次与重污染天气...
  9. JQ css3 导航栏到底部上移
  10. Arctext.js - 基于 CSS3 jQuery 的文本弯曲效果