hibernate 中的schemaexport,schemaupdate工具应用

package cn.com.unutrip.hibernate.common;

import java.io.File;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.hbm2ddl.SchemaUpdate;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html }.
 */
public class HibernateUtil {

 
 /**
  * Location of hibernate.cfg.xml file.
  * Location should be on the classpath as Hibernate uses 
  * #resourceAsStream style lookup for its configuration file.
  * The default classpath location of the hibernate config file is
  * in the default package. Use #setConfigFile() to update
  * the location of the configuration file for the current session.  
  */
 private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
 private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
 private static final ThreadLocal<Transaction> transLocal = new ThreadLocal<Transaction>();
 private static Configuration configuration = new Configuration();
 private static org.hibernate.SessionFactory sessionFactory;
 private static String configFile = CONFIG_FILE_LOCATION;
 /**
  * 加载Hibernate的初始化的配置文件的信息
  */
 static {
  try {
   // 读取文件
       configuration.configure(configFile);
       // 创建SchemaExport对象              
      SchemaExport  schema=new SchemaExport(configuration);
      schema.setOutputFile("C:\\a.sql");
      // 创建数据库
      schema.create(false, true);
      //是否格式化
      schema.setFormat(true);

 
             //更给数据库的对象的应用     
      SchemaUpdate  update=new SchemaUpdate(configuration);
      update.execute(true, true);
     
     
     
     
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err.println("%%%% Error Creating SessionFactory %%%%");
   e.printStackTrace();
  }
 }

 private HibernateUtil() {
 }

 /**
  * 获取Session对象的应用
  * @return
  * @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 void startTransaction() {
  Transaction trans = transLocal.get();
  if (trans != null) {
   trans.begin();
  } else {

   trans = getSession().getTransaction();
   transLocal.set(trans);
   trans.begin();
  }
 }
 /**
  * 执行提交事务的方法的应用
  */
 public void commitTransaction() {
  Transaction trans = transLocal.get();
  if (trans != null&&!trans.wasCommitted()) {
    trans.commit();
    transLocal.set(null);
  }
 }
 /**
  * 执行回滚事务的方法的应用
  */
 public void rollbackTransaction() {
  Transaction trans = transLocal.get();
  if (trans != null&&!trans.wasRolledBack()) {
    trans.rollback();
    transLocal.set(null);
  }
 }
 /**
  * 用于创建SessionFactory的对象的
  */
 public static void rebuildSessionFactory() {
  try {
   configuration.configure(configFile);
   sessionFactory = configuration.buildSessionFactory();
  } catch (Exception e) {
   System.err.println("%%%% Error Creating SessionFactory %%%%");
   e.printStackTrace();
  }
 }

 /**
  * 用于关闭Session对象
  * @throws HibernateException
  */
 public static void closeSession() throws HibernateException {
  Session session = (Session) threadLocal.get();
  threadLocal.set(null);

  if (session != null) {
   session.close();
  }
 }

 /**
  * 获取SessionFactory的对象的
  * @return
  */
 public static org.hibernate.SessionFactory getSessionFactory() {
  return sessionFactory;
 }

 /**
  * 设置配置文件的信息
  * @param configFile
  */
 public static void setConfigFile(String configFile) {
  HibernateUtil.configFile = configFile;
  sessionFactory = null;
 }

 /**
  * 用于获取Configuration的配置对象的应用
  * @return
  */
 public static Configuration getConfiguration() {
  return configuration;
 }

}

 

Hibernate中使用Threadlocal创建线程安全的Session

 

一、问题的提出
  
 我们知道Session是由SessionFactory负责创建的,而SessionFactory的实现是线程安全的,多个并发的线程可以同时访问一 个SessionFactory并从中获取Session实例,而Session不是线程安全的。Session中包含了数 据库操作相关的状态信息,那么说如果多个线程同时使用一个Session实例进行CRUD,就很有可能导致数据存
取的混乱,你能够想像那些你根本不能预测 执行顺序的线程对你的一条记录进行操作的情形吗?
二、 解决方案思路使用Threadlocal类集合

    早在Java1.2推出之时,Java平台中就引入了一个新的支持:java.lang.ThreadLocal,给我们在编写多线程程序时提供了一种新 的选择。ThreadLocal是什么呢?其实ThreadLocal并非是一个线程的本地实现版本,它并不是一个Thread,而是thread local variable(线程局部变量)。也许把它命名为ThreadLocalVar更加合适。线程局部变量(ThreadLocal)其实的功用非常简单, 就是为每一个使用该变量的线程都提供一个变量值的副本,是每一个线程都可以独立地改变自己的副本,而不会和其它线程的副本冲突。从线程的角度看,就好像每 一个线程都完全拥有一个该变量。
    ThreadLocal这个类本身不是代表线程要访问的变量,这个类的成员变量才是。JDK1.5给ThreadLocal加了泛型功能,即是 ThreadLocal,这个泛型T即是要线程的本地变量。线程通过ThreadLocal的get和set方法去访问这个变量T。
    ThreadLocal是如何做到为每一个线程维护变量的副本的呢?其实实现的思路很简单,在ThreadLocal类中有一个Map,用于存储每一个线程的变量的副本。比如下面的示例实现(为了简单,没有考虑集合的泛型):

   public class ThreadLocal { 
         private Map values = Collections.synchronizedMap(new HashMap()); 
         public Object get() { 
           Thread currentThread = Thread.currentThread();  
            Object result = values.get(currentThread);  
            if(result == null&&!values.containsKey(currentThread)) { 
               result = initialValue(); 
               values.put(currentThread, result);  
            
           return result;  
        
        public void set(Object newValue) { 
           values.put(Thread.currentThread(), newValue); 
        
        public Object initialValue() { 
           return null;  
        
   }

三、解决方案步骤

   1、在HibernateUtil类中我们需要定义一个静态的成员变量用于保存当前线程共用的Session

    public class HibernateUtil {
    private static SessionFactory factory;
    // 使用ThreadLocal集合保存当前业务线程中的SESSION
    private static ThreadLocal session = new ThreadLocal();

    static {
        // 第一步:读取HIBERNATE的配置文件,读取hibernate.cfg.xml文件
        Configuration con = new Configuration().configure();
        // 第二步:创建服务注册构建器对象,通过配置对象中加载所有的配置信息,存放到注册服务中
        ServiceRegistryBuilder regBuilder = new ServiceRegistryBuilder()
                .applySettings(con.getProperties());
        // 创建注册服务
        ServiceRegistry reg = regBuilder.buildServiceRegistry();
        // 第三步:创建会话工厂
        factory = con.buildSessionFactory(reg);
    }

   
    public static Session getLocalThreadSession() {
        Session s = session.get();// 获取当前线程下的SESSION
        if (s == null) {
            s = getFactory().getCurrentSession();// 获取当前线程中的SESSION,
需在在Hibernate.cfg.xml文件,具体请看面的说明
            session.set(s);// 将当前SESSION放入到当前线程的容器中保存
        }
        return s;
    }


    
    public static void closeSession() {
        Session s = session.get();// 获取当前线程下的SESSION
        if (s != null) {
            // s.close();//这里无需将Session关闭,因为该Session是保存在当前线程//中的,线程执行完毕Session自然会销毁
            session.set(null);// 将当前线程中的会话清除
        }
    }

}

2、添加OpenSessionInViewFilter过滤器(不要忘了在Web.xml配置该过滤器

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
Session s = HibernateUtil.getThreadLocalSession();
Transaction t = null;
try {
// 开始事务
t = s.beginTransaction();
// 进入一系列的过滤链,处理相应的ACTION、业务逻辑及数据层
filterChain.doFilter(request, response);
// 提交事务
t.commit();

} catch (Exception e) {
if (t != null)
    t.rollback();//出现异常回滚事务
throw new RuntimeException(e.getMessage(), e);

} finally {
    HibernateUtil.closeSession();
}
}

##############################################################################################

说明:关于getCurrentSession()方法:
    sessionFactory.getCurrentSession()获取当前线程中的Session, 当调用时,hibernate将session绑定到当前线程,事务结束后,hibernate将session从当前线程中释放,并且关闭 session。当再次调用getCurrentSession()时,将得到一个新的session,并重新开始这一系列工作。这样调用方法如下: Session session = HibernateUtil.getSessionFactory().getCurrentSession();

getCurrentSession和openSession的区别:
1、getCurrentSession创建的session会和绑定到当前线程,而openSession不会。
2、getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭
3、
getCurrentSession需在在Hibernate.cfg.xml文件中添加配置:
 
 <property name="current_session_context_class">thread</property>
四、总结

   这种解决方案的优缺点:

1、优点:使用ThreadLocal除了有避免频繁创建和销毁session的好处外, 还有一个特别大的好处,
就是可以做到多线程的数据隔离, 可以避免多个线程同时操作同一个session

2.缺点:  如下图


使用拦截器在响应返回时,又重复过滤了一次,延长了响应的时间(改进:我们可以把写在过滤器中的方法写在一个具体的类,用到的时候再调用)

Hibernate悲观锁、乐观锁解决事务并发

一、使用乐观锁解决事务并发问题
 
 
相 对悲观锁而言,乐观锁机制采取了更加宽松的加锁机制。悲观锁大多数情况下依靠数据库的锁机制实现,以保证操作最大程度的独占性。但随之而来的就是数据库性 能的大量开销,特别是对长事务而言,这样的开销往往无法承受。乐观锁机制在一定程度上解决了这个问题。乐观锁,大多是基于数据版本(Version)记录 机制实现。何谓数据版本?即为数据增加一个版本标识,在基于数据库表的版本解决方案中,一般是通过为数据库表增加一个"version"字段来实现。
  乐观锁的工作原理(这里和SVN版本管理有点相似):读取出数据时,将此版本号一同读出,之后更新时,对此版本号加一。此时,将提交数据的版本数据与数据库表对应记录的当前版本信息进行比对,如果提交的数据版本号大于数据库表当前版本号,则予以更新,否则认为是过期数据。

Hibernate为乐观锁提供了3中实现:

1. 基于version

2. 基于timestamp

3. 为遗留项目添加添加乐观锁 

A、配置基于version的乐观锁:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.suxiaolei.hibernate.pojos.People" table="people">
        <id name="id" type="string">
            <column name="id"></column>
            <generator class="uuid"></generator>
        </id>
       
        <!-- version标签用于指定表示版本号的字段信息 -->
        <version name="version"  type="integer"></version>


        <property name="name" column="name" type="string"></property>
       
    </class>
</hibernate-mapping>

#####:注意<version>元素一定要紧挨在id元素之后,并且在实体类中也添加属性version

B、配置基于timestamp的乐观锁:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.suxiaolei.hibernate.pojos.People" table="people">
        <id name="id" type="string">
            <column name="id"></column>
            <generator class="uuid"></generator>
        </id>
       
        <!-- timestamp标签用于指定表示版本号的字段信息 -->
        <timestamp name="updateDate" ></timestamp>


        <property name="name" column="name" type="string"></property>
       
    </class>
</hibernate-mapping>

#####:注意<timestamp>元素一定要紧挨在id元素之后,并且在实体类中也添加属性updateDate


C、遗留项目,由于各种原因无法为原有的数据库添加"version"或"timestamp"字段,这时不可以使用上面两种方式配置乐观锁,Hibernate为这种情况提供了一个"optimisitic-lock"属性,它位于<class>标签上:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.suxiaolei.hibernate.pojos.People" table="people" optimistic-lock="all">
        <id name="id" type="string">
            <column name="id"></column>
            <generator class="uuid"></generator>
        </id>

        <property name="name" column="name" type="string"></property>
    </class>
</hibernate-mapping>

将该属性的值设置为all,让该记录所有的字段都为版本控制信息。

二、使用悲观锁解决事务并发问题

   悲观锁,悲观锁由数据库来实现,正如其名,它指的是对数据被外界(包括本系统当前的其他事务,以及来自外部系统的事务处理)修改持保守态度,因此,在整个数据处理过程中,将数 据处于锁定状态。悲观锁的实现,往往依靠数据库提供的锁机制(也只有数据库层提供的锁机制才能真正保证数据访问的排他性,否则,即使在本系统中实现了加锁 机制,也无法保证外部系统不会修改数据)。

   一个典型的依赖数据库的悲观锁调用:select * from account where name=”Erica” for update这条 sql 语句锁定了 account 表中所有符合检索条件( name=”Erica” )的记录。本次事务提交之前(事务提交时会释放事务过程中的锁),外界无法修改这些记录。悲观锁,也是基于数据库的锁机制实现。

在Hibernate使用悲观锁十分容易,但实际应用中悲观锁是很少被使用的,因为它大大限制了并发性:



图 为Hibernate3.6的帮助文档Session文档的get方法截图,可以看到get方法第三个参数"lockMode" 或"lockOptions",注意在Hibernate3.6以上的版本中"LockMode"已经不建议使用。方法的第三个参数就是用来设置悲观锁 的,使用第三个参数之后,我们每次发送的SQL语句都会加上"for update"用于告诉数据库锁定相关数据。



LockMode参数选择该选项,就会开启悲观锁。



   T1,T2时刻取款事务和转账事务分别开启,T3事务查询ACCOUNTS表的数据并用悲观锁锁定,T4转账事务也要查询同一条数据,数据库发现该记录 已经被前一个事务使用悲观锁锁定了,然后让转账事务等待直到取款事务提交。T6时刻取款事务提交,T7时刻转账事务获取数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值