- 浏览: 536878 次
- 性别:
- 来自: 湖南娄底
文章分类
- 全部博客 (252)
- C#资料 (0)
- java资料 (22)
- 数据库资料 (0)
- Java :开发中遇到的问题 (4)
- 书写属于我的人生 (6)
- spring (6)
- hibernate (16)
- struts (17)
- SSH (5)
- Linux操作系统 (1)
- Ext (5)
- Jquery (9)
- Tomcat部署出错 (2)
- xml (5)
- JSON (5)
- javascript 专题 (5)
- 开发环境配置 (2)
- web 开发 (27)
- Eclipse (7)
- Integration (1)
- 解决方案搜罗 (7)
- 网站收藏 (8)
- 测试框架 (1)
- iframe 专题 (6)
- JSTL (2)
- 各种插件收集 (2)
- 权限设计模型探讨 (1)
- DWR (1)
- log4j (3)
- java (11)
- java 格式化 (1)
- 软件工程 (2)
- 乱码专题 (1)
- UML (4)
- java 设计模式 篇 (1)
- 用法收集 (0)
- sourceforge apache solutions (2)
- Struts2 + JPA + Spring 开发 (1)
- 自定义工具箱 (0)
- IDE (4)
- opencms (1)
- jeecms (1)
- ant (0)
- maven (2)
- 数据库连接池 (0)
- spring MVC (0)
- android (1)
- web-server (1)
- iphone-开发环境 (1)
- iphone-软件开发 (6)
- iPhone-软件设计 (1)
- iphone-应用 (3)
- iPhone-设置 (2)
- iphone-软件开发-设计模式篇 (0)
- iphone-软件开发-OC基础 (9)
- iphone-软件开发- UI篇 (9)
- iphone-软件开发-工具类篇 (1)
- iphone-软件开发-第三方篇 (0)
- iphone-软件开发-特效篇 (2)
- iphone-软件开发-动画篇 (1)
- oracle (1)
- Java-Security (0)
- Three20 UI (1)
- IOS-HOW-TO-专题 (1)
- Mark-Develope Issues (1)
- iphone-软件开发- 资料参考篇 (1)
- C 语言-基本语法 (0)
- Java Servlet (2)
- Eclipse optimization (1)
- IOS Basic (0)
- ecl (0)
- FRONT-HTML (0)
- FRONT-JS (0)
- PHP (0)
最新评论
-
qq_29862421:
[[color=brown]color=yellow][url ...
java 上传图片同时获得图片的宽和高 -
hhb19900618:
NSString <==> NSNumber 之前 ...
OC 常用数据类型之间的转换 -
baohuan_love:
果然能解决问题,感谢分享
Tomcat Error Page配置 与 Error Page在IE下不能转发的问题 -
唐丽梅6313:
很细致。。
键盘样式风格有关设置-iOS开发 -
olived:
哥哥,为什么我的没有破解成功,总是提示许可文件丢失!!QQ:3 ...
[软件测试解决方案] soapUI 4.0.1破解方法
在HibernateSessionFactory类中,我们可以仿照Session的管理方式来实现对Transaction事务的管理!
package com.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static String CONFIG_FILE_LOCATION = "/com/hibernate/hibernate.cfg.xml";
/**
*
* 程序在运行过程中会启动多个线程, threadLocal可以在单个线程内实现线程内部共享数据---使用单例模式
*
*/
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
/**
* 使用ThreadLocal实现事务的管理
*
*/
private static final ThreadLocal<Transaction> txThreadLocal = new ThreadLocal<Transaction>();
//配置文件节点<hibernate-configuration>对应读取的对象
private static Configuration configuration = new Configuration();
//配置文件<session-factory>节点对应读取的对象
private static org.hibernate.SessionFactory sessionFactory;
//配置文件hibernate.cfg.xml配置文件所在的路径
private static String configFile = CONFIG_FILE_LOCATION;
//静态块,会早于构造函数执行
static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
}
return session;
}
public static void beginTransaction() throws HibernateException {
//取得已经绑定的事务
Transaction tx = (Transaction) txThreadLocal.get();
//如果事务为空,或已经提交或已经回滚,则开启一个新的事务
if (tx == null || tx.wasCommitted() || tx.wasRolledBack()) {
tx = getSession().beginTransaction();
//把事务绑定的本地线程中,供提交会回滚使用
txThreadLocal.set(tx);
}
}
public static void commitTransaction() throws HibernateException {
//取得已经绑定的事务
Transaction tx = (Transaction) txThreadLocal.get();
//如果事务不等于空,并没有提交或回滚。则提交事务
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.commit();
}
//清空当前线程事务,确保下个操作取得新的事务
txThreadLocal.set(null);
}
public static void rollbackTransaction() throws HibernateException {
Transaction tx = (Transaction) txThreadLocal.get();
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {
tx.rollback();
}
txThreadLocal.set(null);
}
/**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
/**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
/**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
}
/**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
}
/**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
}
发表评论
-
detached entity passed to persist异常解决
2012-04-21 15:48 0病理特征:Caused by: org.hibernat ... -
hibernate中inverse作用
2012-04-07 20:52 0inverse常用于一对多双向关联关系中。 以Student ... -
hibernate Restrictions用法
2012-04-07 19:31 0方法 说明 Restricti ... -
QBC Hibernate 多表查询
2012-04-07 19:30 01.java 实体对象 Java代码 ... -
Hibernate继承映射-多态关联
2012-04-07 19:24 0把类之间的继承关系反 ... -
hibernate - 10.12. 使用元数据
2012-04-07 11:32 928Hibernate中有一个非常丰富的元级别(meta-leve ... -
Hibernate <property>配置
2012-04-07 10:34 1287<property name="name&qu ... -
Hibernate Tool: hbm2java or hbm2ddl
2012-04-01 18:30 1409Hibernate为了简化开发,给我们提供了四种工 具 ... -
Hibernate锁机制 悲观锁和乐观锁
2012-04-01 18:21 11211、Pessimistic Locking 悲观锁 ... -
hibernate一级缓存和二级缓存的区别
2012-04-01 17:09 740缓存是介于应用程序 ... -
Hibernate的Fetch
2012-03-17 22:55 019.1. 抓取策略(Fetching strategies ... -
hibernate 使用二级缓存
2012-03-15 22:04 0二级缓存: 二级缓存也称进程级的缓存或SessionF ... -
hibernate Isolation Level(事务隔离等级)
2011-12-25 14:33 1542hibernate Isolation Level(事务隔离等 ... -
Hibernate Criteria 多层次查询关联问题(转)
2011-12-22 21:52 1115轉載,收藏一下! http://terrencemai ... -
hibernate hql函数
2011-12-20 20:07 4440√通过 ⊙测试没结果 ... -
openSession() 与 getCurrentSession()
2011-09-17 18:04 9861 getCurrentSession创建的session会和 ... -
criteria进行分页查询
2010-11-10 01:33 1526public List findStockBySt ... -
hibernate HQL 模糊查询 3种方式
2010-11-10 01:01 1144hibernate HQL 模糊查询 3种方式 以下是 ... -
hibernate从数据库中读取出来的数据乱码的问题分析!
2010-10-19 22:26 3172最近在部署一个开源项目时,发现从数据库中读取出来的数据都乱码了 ... -
Hibernate常见问题集锦(2)
2010-10-17 22:17 9721。问题: 异常原因: 2010/10/17-22: ...
相关推荐
使用三层架构改写我之前上传的代码 技术要点:1....使用事务管理器6.使用ThreadLocal管理Connection,实现线程单例 感兴趣的朋友可以下载,想要更具体了解可以参考我的“JDBC访问数据库操作详解系列”博客
脂肪FAT,基于springboot,使用zookeeper,redis,spring异步,spring transactionManager的强一致性分布式事务解决方案框架介绍纯编码方式,强一致性。使用redis / zookeeper作为注册中心,代理事务的执行,使用...
9.3.2 Spring的事务管理器实现类 9.3.3 事务同步管理器 9.3.4 事务传播行为 9.4 编程式的事务管理 9.5 使用XML配置声明式事务 9.5.1 一个将被实施事务增强的服务接口 9.5.2 使用原始的 TransactionProxyFactoryBean ...
9.3.2 Spring的事务管理器实现类 9.3.3 事务同步管理器 9.3.4 事务传播行为 9.4 编程式的事务管理 9.5 使用XML配置声明式事务 9.5.1 一个将被实施事务增强的服务接口 9.5.2 使用原始的 TransactionProxyFactoryBean ...
9.2.5 事务管理和异常处理 359 9.3 使用NetBeans开发EJB 359 9.3.1 使用NetBeans开发Session Bean 359 9.3.2 使用NetBeans开发MDB 362 9.4 本章小结 363 第10章 Java持久化API(JPA) 364 10.1 实体简介 365 10.1.1 ...
9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring ...
9.9.1. 对一个特定的 DataSource 使用了错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. 选择一...
9.9.1. 对一个特定的 DataSource 使用了错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. 选择一种...
9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring ...
9.2.2 线程变量管理器的实现... 159 9.3 事务... 163 9.3.1 为什么需要事务... 163 9.3.2 什么是事务... 164 9.3.3 事务的边界划分... 164 9.3.4 声明型事务的属性... 166 9.3.5 事务的隔离... 168 9.3.6 事务的隔离...
9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring JDBC包结构...
MySQL:基础知识、存储引擎、日志、SQL优化、数据索引、锁、事务、高可用实现等;Spring:IOC、AOP、声明式事务、MVC等;Redis:持久化过程、高可用实现、缓存设计、应用场境等。 所有文档都是md格式,方便阅读,...
学生提问:使用组合关系来实现复用时,需要创建两个Animal对象,是不是意味着使用组合关系时系统开销更大? 159 5.9 初始化块 159 5.9.1 使用初始化块 160 5.9.2 初始化块和构造器 161 5.9.3 静态初始化块 162 ...