zoukankan      html  css  js  c++  java
  • hibernate学习(8)——事务.连接池.锁 相关设置

    1、整合c3p0(连接池)

    步骤一:导入c3p0 jar包

    步骤二:hibernate.cfg.xml 配置

    hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider

    步骤三:c3p0具体配置参数

    #hibernate.c3p0.max_size 2

    #hibernate.c3p0.min_size 2

    #hibernate.c3p0.timeout 5000

    #hibernate.c3p0.max_statements 100

    #hibernate.c3p0.idle_test_period 3000

    #hibernate.c3p0.acquire_increment 2

    #hibernate.c3p0.validate false

    	<!-- 使用C3p0连接池
     hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider  -->
    	<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
    	<!-- 
    	      #hibernate.c3p0.max_size 2
              #hibernate.c3p0.min_size 2
              #hibernate.c3p0.timeout 5000
              #hibernate.c3p0.max_statements 100
              #hibernate.c3p0.idle_test_period 3000
              #hibernate.c3p0.acquire_increment 2
              #hibernate.c3p0.validate false
        -->
        <property name="hibernate.c3p0.max_size">5</property>
        <property name="hibernate.c3p0.timeout">50000</property>
    

      2、事务

      特性:ACID

    1        原子性:整体, 事务:被事务包裹的sql语句是一组业务操作,要么全部成功,要么全部不成功。

    2       一致性:数据, 在事务操作中,数据要统一,增加1000,必须减少1000,类似于物理中能量守恒。

     3      隔离性:并发,并发环境下,事务与事务之间不干扰

       4    持久性:结果,事务一旦提交,事务中的操作要会永远保存

     隔离问题:

           脏读:一个事务读到另一个事务未提交的内容

           不可重复读:一个事务读到另一个事务已提交的内容(insert)

           虚读(幻读):一个事务读到另一个事务已提交的内容(update)

     隔离级别--解决问题

           read uncommittd,读未提交。存在3个问题。

           read committed,读已提交。解决:脏读。存在2个问题。

           repeatable read ,可重复读。解决:脏读、不可重复读。存在1个问题。

           serializable,串行化。单事务。没有问题。

    2.1  hibernate设置隔离级别

    2.2 数据库中的锁

    数据库的锁(了解的了解)
               悲观锁(数据库提供实现) :默认认为一定会发送别人要修改我使用的数据. 那我就可以为我读取的数据加锁.
                      读锁/共享锁 => 读锁可被其他线程所共享,如果是读取的话大家都可以用这把锁读到数据.
                        select * from table lock in share mode(读锁、共享锁)

                        写锁/排他锁 => 写锁不能共享,只要有人为数据加入了写锁,其他人就不能为数据加任何锁.
                       select * from table for update

    @Test
        //悲观锁
            //写锁
        public void test01(){
            Session session = HibernateUtil.openSession();
            Transaction tran = session.beginTransaction();
            
            Customer cust  = (Customer) session.get(Customer.class, 6, LockOptions.UPGRADE);
            System.out.println(cust);
            tran.commit();
            session.close();
        }

    (写锁、排它锁)

    乐观锁(需要自己实现):丢失更新肯定不会发生

    在表中提供一个字段(版本字段),用于标识记录。如果版本不一致,不允许操作。

    在PO对象(javabean)提供字段,表示版本字段。一般Integer

           在*.hbm.xml 文件配置 <version name="...">

    @Test
        //乐观锁
        public void test02(){
            Session session = HibernateUtil.openSession();
            Transaction tran = session.beginTransaction();
            
            Customer cust  = (Customer) session.get(Customer.class, 4, LockOptions.READ);
            cust.setName("aloce");
            tran.commit();
            session.close();
        }
  • 相关阅读:
    网络结构解读之inception系列五:Inception V4
    网络结构解读之inception系列四:Inception V3
    网络结构解读之inception系列三:BN-Inception(Inception V2)
    网络结构解读之inception系列二:GoogLeNet(Inception V1)
    网络结构解读之inception系列一:Network in Network
    深度学习相关转载收集
    Person Re-identification 系列论文笔记(八):SPReID
    Person Re-identification 系列论文笔记(七):PCB+RPP
    Person Re-identification 系列论文笔记(六):AlignedReID
    Spring@Autowired注解与自动装配
  • 原文地址:https://www.cnblogs.com/snowwang/p/6132000.html
Copyright © 2011-2022 走看看