Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java EE 综合讨论区 » Hibernate  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 乐观锁的问题
Henry2006





发贴: 9
积分: 0
于 2009-04-29 14:59 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
近些天关注了一下乐观锁,做了一些测试,但是好像有点问题,我从SessionFactory得到两个Session,分别是session1和session2然后让session1和session2分别load同一条数据,得到实体entity1和entity2,然后分别修改entity1和entity2中同一个字段的数据,先让session2提交,然后是session1提交,按照书上写的这时应该是抛出异常,但是什么也没发生,居然正常通过。。。。

hbm文件如下:
<?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="entity.Person" table="t_person" optimistic-lock="version">
    <id name="id" column="id">
      <generator class="native"/>
    </id>

    <version name="version" column="version"/>
    <property name="name" column="name" type="string" />
    <property name="age" column="age" type="int" />
    <property name="gender" column="gender" type="string" />
    <property name="length" column="length" type="int" />
    <property name="strong" column="strong" type="int" />
  </class>
</hibernate-mapping>

测试代码如下:
@Test public void testOptimisticLock(){
    Configuration config = new Configuration();
    config.configure(TestInitData.cfgFile);
    SessionFactory sessionFactory = config.buildSessionFactory();
    Session session1 = sessionFactory.openSession();
    Session session2 = sessionFactory.openSession();
    
    Person p1 = (Person) session1.load(Person.class, 1l);
    Person p2 = (Person) session2.load(Person.class, 1l);
    
    Transaction tx1 = session1.beginTransaction();
    p1.setLength(200);

    Transaction tx2 = session2.beginTransaction();
    p2.setLength(100);
    session2.update(p2);
    tx2.commit();
    session2.close();
    System.out.println("session1 close!");
    session1.update(p1);
    tx1.commit();
    session1.close();
  }

可否帮我看看,哪里出了问题!!




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923