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 ...
随机推荐
- sublime插件insertDate显示ISO时间
1 下载insertDate插件以及安装完毕 2 把光标放在想插入ISO时间的地方 3 按住:alt+f5,之后,在sublime下面的Date format string输入:iso.之后按ente ...
- [转]script之defer&async
html5中script的async属性 我兴奋于html5的原因之一是一些久久未能实现的特性现在可以真正运用于实际项目中了. 如我们使用placeholder效果蛮久了但是那需要javascript ...
- Python 静态方法和类方法的区别
python staticmethod and classmethod Though classmethod and staticmethod are quite similar, there’s a ...
- 一 Kubernetes介绍
Kubenetes是一款由Google开发的开源的容器编排工具,它可以解决以下分布式环境下的问题: 调度 你已经得到了这个很棒的基于容器的应用程序? 太棒了!现在你需要确保它能够运行在它应该运行的地方 ...
- python2和python3中的range区别
python2中的range返回的是一个列表 python3中的range返回的是一个迭代值 for i in range(1,10)在python2和python3中都可以使用,但是要生成1-10的 ...
- 【opencv学习笔记七】访问图像中的像素与图像亮度对比度调整
今天我们来看一下如何访问图像的像素,以及如何改变图像的亮度与对比度. 在之前我们先来看一下图像矩阵数据的排列方式.我们以一个简单的矩阵来说明: 对单通道图像排列如下: 对于双通道图像排列如下: 那么对 ...
- linux工具————fish shell
1.说明 fish is a fully-equipped command line shell (like bash or zsh) that is smart and user-friendly. ...
- Sharding & IDs at Instagram, Flickr ID generation
Instagram: http://instagram-engineering.tumblr.com/post/10853187575/sharding-ids-at-instagram Flickr ...
- 算法java实现--回溯法--图的m着色问题
(转自:http://blog.csdn.net/lican19911221/article/details/26264471) 图的m着色问题的Java实现(回溯法) 具体问题描述以及C/C++实现 ...
- SetROP2
一个Windows API SetROP2(int nDrawMode)的使用 该函数的主要的作用是根据nDrawMode设置的方式重新设定绘图的方式,下面就不同的nDrawMode值具体解释绘图模式 ...