Hibernate 事物隔离级别
|
Connection = null;
PreparedStatement pstmt = null;
try{
con = DriverManager.getConnection(dbUrl, username, password);
//设置手工提交事务模式
con.setAutoCommit(false);
pstmt = ……;
pstmt.executeUpdate();
//提交事务
con.commit();
}catch(Exception e){
//事务回滚
con.rollback();
…..
} finally{
…….
}
|
|
<session-factory>
……
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
……
</session-factory>
|
|
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();
}
|
|
<session-factory>
……
<property name="hibernate.transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
……
</session-factory>
|
|
// BMT(bean管理事务) 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
}
|
|
// CMT idiom
Session sess = factory.getCurrentSession();
// do some work
...
|
|
<session-factory>
<!-- 设置JDBC的隔离级别 -->
<property name="hibernate.connection.isolation">2</property>
</session-factory>
|
|
package org.qiujy.domain.versionchecking;
import java.util.Date;
public class Product implements java.io.Serializable{
private Long id ;
/** 版本号 */
private int version;
private String name; //产品名
private String description; //描述--简介
private Double unitCost; //单价
private Date pubTime; //生产日期
public Product(){}
//以下为getter()和setter()方法
}
|
|
package org.qiujy.domain.versionchecking;
import java.util.Date;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.qiujy.common.HibernateSessionFactory;
public class TestVersionChecking {
public static void main(String[] args) {
Product prod = new Product();
prod.setName("IBM thinkPad T60");
prod.setUnitCost(new Double(26000.00));
prod.setDescription("笔记本电脑");
prod.setPubTime(new Date());
//test start.......
Session session = HibernateSessionFactory.getSession();
Transaction tx =null;
try{
tx = session.beginTransaction();
session.save(prod);
tx.commit();
}catch(HibernateException e){
if(tx != null){
tx.rollback();
}
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
//进行更新 测试..
prod.setDescription("新款的");
Session session2 = HibernateSessionFactory.getSession();
Transaction tx2 =null;
try{
tx2 = session2.beginTransaction();
session2.update(prod);
tx2.commit();
}catch(HibernateException e){
if(tx2 != null){
tx2.rollback();
}
e.printStackTrace();
}finally{
HibernateSessionFactory.closeSession();
}
}
}
|
|
insert into products (version, name, description, unitCost, pubTime)
values(?, ?, ?, ?, ?)
|
|
update
products
set
version=?,
name=?,
description=?,
unitCost=?,
pubTime=?
where
id=?
and version=?
|
Hibernate 事物隔离级别的更多相关文章
- Hibernate 事物隔离级别 深入探究
目录 一.数据库事务的定义 二.数据库事务并发可能带来的问题 三.数据库事务隔离级别 四.使用Hibernate设置数据库隔离级别 五.使用悲观锁解决事务并发问题 六.使用乐观锁解决事务并发问题 Hi ...
- SQL事物隔离级别
标准SQL定义了4个隔离级别 Read uncommitted 未提交读 Read committed 已提交读 Repeatable read 可重复读 Serializable 可序列化 基本语法 ...
- spring事物传播机制 事物隔离级别
Spring事务类型详解: PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. PROPAGATION_SUPPORTS--支持当前事务,如 ...
- Spring事物隔离级别及事物传播行为@Transactional实现
阅读本篇文章前,请先阅读如下文章: 四种事物隔离级别详解 先看下@Transactional可以配制那些参数及以其所代表的意义. isolation 枚举org.springframework.tra ...
- MySQL数据库的事物隔离级别
一. 查看数据库的事物隔离级别 mysql> show variables like '%isolation'; +-----------------------+--------------- ...
- MVCC原理 4步 什么是MVCC、事务ACID、事物隔离级别、Innodb存储引擎是如何实现MVCC的
MVCC是来处理并发的问题,提高并发的访问效率,读不阻塞写.事物A 原子性C 一致性I 隔离性D 持久性高并发的场景下的问题脏读不可重复读幻读事物隔离级别RU读未提交 脏读/不可重复读/幻读 .不适用 ...
- Oracle中事物处理--事物隔离级别
n 事物隔离级别 概念:隔离级别定义了事物与事物之间的隔离程度. ANSI/ISO SQL92标准定义了一些数据库操作的隔离级别(这是国际标准化组织定义的一个标准而已,不同的数据库在实现时有所不同) ...
- 数据库事物 jdbc事物 spring事物 隔离级别:脏幻不可重复读
1.数据库事物: 事物的概念 a给b打100块钱的例子 2.jdbc事物: 通过下面代码实现 private Connection conn = null; private PreparedState ...
- Hibernate - 设置隔离级别
JDBC 数据库连接使用数据库系统默认的隔离级别. 在 Hibernate 的配置文件中可以显式的设置隔离级别. 每一个隔离级别都对应一个整数: 1. READ UNCOMMITED2. READ C ...
随机推荐
- 变废为宝,用旧电脑自己DIY组建 NAS 服务器
i17986 出品,必属佳作! 前言: 老外不喜欢升级硬件和软件,大家应该都知道.我昨天无意看到 FreeNAS 自述文件,这个系统可以让你使用旧的计算机硬件,于是我决定这么做.垃圾电脑你怎么能没有, ...
- Python3解leetcode Linked List Cycle
问题描述: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given link ...
- android和iOS平台的崩溃捕获和收集
转自:http://www.cnblogs.com/lancidie/archive/2013/04/13/3019349.html 通过崩溃捕获和收集,可以收集到已发布应用(游戏)的异常,以便开发人 ...
- 第二课2、ROS
1.ROS框架 分为以下三个级别: 1)文件系统级 2)计算图级 计算图级是ROS处理数据的一种点对点的网络形式,描述程序是如何运行的. 基本的计算图级概念包括:节点,参数服务器,消息,服务,主题和包 ...
- 数据库中rs("ABC")与rs.Fields("ABC").value的差别(Rs是RecordSet对象)
透过RecordSet取得数据的时候我们要将数据显示出来时,假设字段名称是ABCABCX = rs("ABC")对于RecordSet来说....是把ABC这个[Fileds对象] ...
- [Oracle]oracle查询表列名、及列数
--查询表列数 select count( column_name ) from user_tab_columns where table_name = 'CJ_HOME_MEDICAL_RECORD ...
- LeetCode: 669 Trim a Binary Search Tree(easy)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
- MVC用户验证
MVC提供了四种Filter(钩子),用于在Action执行之前或者之后,我们能够做一些事情,比如说判断有没有登录,比如说判断有没有权限. IAuthorizationFilter:在所有Filter ...
- Python实现栈、队列
目录 1. 栈的Python实现 1.1 以列表的形式简单实现栈 1.2 以单链表形式实现栈 2. 队列的Python实现 2.1 以列表实现简单队列 2.2 以单链表形式实现队列 本文将使用py ...
- jzoj6005. 【PKUWC2019模拟2019.1.17】数学 (生成函数+FFT+抽代+高精)
题面 题解 幸好咱不是在晚上做的否则咱就不用睡觉了--都什么年代了居然还会出高精的题-- 先考虑如果暴力怎么做,令\(G(x)\)为\(F(n,k)\)的生成函数,那么不难发现\[G^R(x)=\pr ...