posts - 165, comments - 198, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

hibernate 问题_3(锁)

Posted on 2007-11-26 18:57 G_G 阅读(1091) 评论(0)  编辑  收藏 所属分类: hibernate
环境:
     private  SessionFactory sessionFactory;

    
protected   void  setUp()  throws  Exception {
        
super .setUp();
        
// 利用java反射得到 HibernateSessionFactory ->
        
// private  static org.hibernate.SessionFactory sessionFactory;
        
// 要模拟 并发 要 HibernateSessionFactory 得出的 有 threadLocal 不行 
        
// 要 
        HibernateSessionFactory.currentSession();
        HibernateSessionFactory.closeSession();
        Field field 
=  HibernateSessionFactory. class .getDeclaredField( " sessionFactory " );
        field.setAccessible(
true );
        sessionFactory 
=  (SessionFactory) field.get(HibernateSessionFactory. class );
    }

    
protected   void  tearDown()  throws  Exception {
        
super .tearDown();
    }

悲观锁:
     /**  悲观锁问题 线程模拟 并发 
     * Table -> T1oo(id,name)
     *     +----+------+
     *    | id | name |
     *    +----+------+
     *    |  4 | xx1  |
     *    +----+------+
     
*/
    
public   void  ctestLock()  throws  Exception {
        
//  insert test Data 测试数据
        Session seInsert  =  sessionFactory.openSession();
        Transaction tr 
=  seInsert.beginTransaction();
        T1oo testUse 
=  testUse  =   new  T1oo();
        testUse.setName(
" liukaiyi_test " );
        seInsert.save(testUse);
        tr.commit();
        System.out.println( 
" **************Test Use Bean : insert******************* "  );
        seInsert.close();
        Integer testId 
=  testUse.getId();
        
        //并发模拟
        Session session1 
=  sessionFactory.openSession();
        Session session2 
=  sessionFactory.openSession();
        
        
final  Transaction tr1  =  session1.beginTransaction();
        Transaction tr2 
=  session2.beginTransaction();
        
        Query qu1 
=  session1.createQuery( "  from T1oo t1oo where t1oo.name='liukaiyi_test' " );
        Query qu2 
=  session2.createQuery( "  from T1oo t1oo where t1oo.name='liukaiyi_test' " );
        
        //加悲观锁
        qu1.setLockMode(
" t1oo " ,LockMode.UPGRADE);
        
final  Object bean1  =  qu1.uniqueResult();
        Object bean2 
=  qu2.uniqueResult();

        T1oo t1oo2 
=  (T1oo)bean2;
        
        t1oo2.setName(
" run1 " );
        
        
new  Thread(
                
new  Runnable(){
                    
public   void  run()  {
                        
try  {
                            System.out.println( 
" ********解锁准备********* "  );
                            Thread.sleep(
10 * 1000 );
                            T1oo t1oo1 
=  (T1oo)bean1;
                            t1oo1.setName(
" run2 " );
                            tr1.commit();
                            System.out.println( 
" ********解锁成功t********* "  );
                        } 
catch  (InterruptedException e) {e.printStackTrace();}
                    }
                }
        ).start();

       
/ /这里会 等待十秒后 tr1解锁 /
        tr2.commit();
        
        session1.close();
        session2.close();
        
        
        
//  delete Test Date 删除测试数据
        Session seDelete  =  sessionFactory.openSession();
        Transaction trD 
=  seDelete.beginTransaction();
        Object obj 
=  seDelete.createQuery( "  from T1oo t where t.id=:id  " ).
                        setInteger(
" id " ,testId.intValue()).
                        uniqueResult();
        seDelete.delete(obj);
        trD.commit();
        System.out.println( 
" **************Test Use Bean : delete************** "  );
        seDelete.close();
    }

乐观锁:
     /**  乐观锁问题
     * Table -> T1oo(id,name)
     * T2oo.hbn.xml <class> +> optimistic-lock="version"
     * id下面 +> <version name="version" type="integer" column="version" />
     *    +---------+---------+------+
     *    | Field   | Type    | Null |
     *    +---------+---------+------+
     *    | id      | int(11) |      |
     *    | avg     | int(11) |      |
     *    | aid     | int(11) | YES  |
     *    | version | int(11) | YES  |
     *    +---------+---------+------+
     *  注意:要在数据库中多加一列 
     *  mysql> alter table T2OO add version int;
     
*/
    
public   void  testOpLock() throws  Exception{
        //添加测试数据
        Session session 
=  sessionFactory.openSession();
        Transaction trI 
=  session.beginTransaction();
        Connection conn 
=  session.connection();
        T2oo t2oo 
=   new  T2oo();
        T1oo t1oo 
= new  T1oo( " t1ooOpLock " );
        t1oo.setT2ooSet(
new  HashSet());
        t2oo.setAvg(
new  Integer( 23 ));    
        t2oo.setT1oo(t1oo);
        t1oo.getT2ooSet().add(t2oo);
        session.save(t2oo);
        trI.commit();
        session.clear();
        session.close();

        //并发模拟
        Session se1 
=  sessionFactory.openSession();
        Session se2 
=  sessionFactory.openSession();
        
        Transaction tr1 
=  se1.beginTransaction();
        Transaction tr2 
=  se2.beginTransaction();
       
        T1oo obj1 
=  (T1oo)se1.load(T1oo. class , new  Integer( 1 ));
        T1oo obj2 
=  (T1oo)se2.load(T1oo. class , new  Integer( 1 ));
        
        ((T2oo)obj1.getT2ooSet().iterator().next()).setAvg(
new  Integer( 9 ));
        ((T2oo)obj2.getT2ooSet().iterator().next()).setAvg(
new  Integer( 10 ));
                
        tr1.commit();
        
        
try  {
            tr2.commit();
        } 
catch  (Exception e) {
            se2.clear();
            tr2.commit();
            
// tr2报错
        } finally {
            se1.close();
            se2.close();
        }
        
        
        //测试数据删除
        Session dele 
=  sessionFactory.openSession();
        Transaction tr 
=  dele.beginTransaction();
        dele.delete(obj1);
        tr.commit();
        dele.close();
        
        HibernateSessionFactory.closeSession();
    }


只有注册用户登录后才能发表评论。


网站导航: