下面列出了org.hibernate.classic.Session#lock ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testVersioning() throws Exception {
Session s = openSession();
Transaction txn = s.beginTransaction();
GlarchProxy g = new Glarch();
s.save(g);
GlarchProxy g2 = new Glarch();
s.save(g2);
Serializable gid = s.getIdentifier(g);
Serializable g2id = s.getIdentifier(g2);
g.setName("glarch");
txn.commit();
s.close();
getSessions().evict(Glarch.class);
s = openSession();
txn = s.beginTransaction();
g = (GlarchProxy) s.load( Glarch.class, gid );
s.lock(g, LockMode.UPGRADE);
g2 = (GlarchProxy) s.load( Glarch.class, g2id );
assertTrue( "version", g.getVersion()==1 );
assertTrue( "version", g.getDerivedVersion()==1 );
assertTrue( "version", g2.getVersion()==0 );
g.setName("foo");
assertTrue(
"find by version",
s.find("from Glarch g where g.version=2").size()==1
);
g.setName("bar");
txn.commit();
s.close();
getSessions().evict(Glarch.class);
s = openSession();
txn = s.beginTransaction();
g = (GlarchProxy) s.load( Glarch.class, gid );
g2 = (GlarchProxy) s.load( Glarch.class, g2id );
assertTrue( "version", g.getVersion()==3 );
assertTrue( "version", g.getDerivedVersion()==3 );
assertTrue( "version", g2.getVersion()==0 );
g.setNext(null);
g2.setNext(g);
s.delete(g2);
s.delete(g);
txn.commit();
s.close();
}
public void testFormulaDiscriminator() throws Exception {
if ( ( getDialect() instanceof OracleDialect ) || ( getDialect() instanceof HSQLDialect ) ) return;
Session s = getSessions().openSession();
I i = new I();
i.setName( "i" );
i.setType( 'a' );
J j = new J();
j.setName( "j" );
j.setType( 'x' );
j.setAmount( 1.0f );
Serializable iid = s.save(i);
Serializable jid = s.save(j);
s.flush();
s.connection().commit();
s.close();
getSessions().evict(I.class);
s = getSessions().openSession();
j = (J) s.get(I.class, jid);
i = (I) s.get(I.class, iid);
assertTrue( i.getClass()==I.class );
j.setAmount( 0.5f );
s.lock(i, LockMode.UPGRADE);
s.flush();
s.connection().commit();
s.close();
s = getSessions().openSession();
j = (J) s.get(I.class, jid, LockMode.UPGRADE);
i = (I) s.get(I.class, iid, LockMode.UPGRADE);
s.flush();
s.connection().commit();
s.close();
s = getSessions().openSession();
assertTrue( s.find("from I").size()==2 );
assertTrue( s.find("from J").size()==1 );
assertTrue( s.find("from I i where i.class = 0").size()==1 );
assertTrue( s.find("from I i where i.class = 1").size()==1 );
s.connection().commit();
s.close();
s = getSessions().openSession();
j = (J) s.get(J.class, jid);
i = (I) s.get(I.class, iid);
s.delete(j);
s.delete(i);
s.flush();
s.connection().commit();
s.close();
}
public void testUnionSubclass() throws Exception {
Session s = getSessions().openSession();
I i = new I();
i.setName( "i" );
i.setType( 'a' );
J j = new J();
j.setName( "j" );
j.setType( 'x' );
j.setAmount( 1.0f );
Serializable iid = s.save(i);
Serializable jid = s.save(j);
s.flush();
s.connection().commit();
s.close();
getSessions().evict(I.class);
s = getSessions().openSession();
j = (J) s.get(I.class, jid);
j = (J) s.get(J.class, jid);
i = (I) s.get(I.class, iid);
assertTrue( i.getClass()==I.class );
j.setAmount( 0.5f );
s.lock(i, LockMode.UPGRADE);
s.flush();
s.connection().commit();
s.close();
getSessions().evict(I.class);
s = getSessions().openSession();
j = (J) s.get(J.class, jid);
j = (J) s.get(I.class, jid);
i = (I) s.get(I.class, iid);
assertTrue( i.getClass()==I.class );
j.setAmount( 0.5f );
s.lock(i, LockMode.UPGRADE);
s.flush();
s.connection().commit();
s.close();
getSessions().evict(I.class);
s = getSessions().openSession();
assertTrue( s.find("from I").size()==2 );
assertTrue( s.find("from J").size()==1 );
assertTrue( s.find("from J j where j.amount > 0 and j.name is not null").size()==1 );
assertTrue( s.find("from I i where i.class = org.hibernate.test.legacy.I").size()==1 );
assertTrue( s.find("from I i where i.class = J").size()==1 );
s.connection().commit();
s.close();
getSessions().evict(I.class);
s = getSessions().openSession();
j = (J) s.get(J.class, jid);
i = (I) s.get(I.class, iid);
K k = new K();
Serializable kid = s.save(k);
i.setParent(k);
j.setParent(k);
s.flush();
s.connection().commit();
s.close();
getSessions().evict(I.class);
s = getSessions().openSession();
j = (J) s.get(J.class, jid);
i = (I) s.get(I.class, iid);
k = (K) s.get(K.class, kid);
System.out.println(k + "=" + i.getParent());
assertTrue( i.getParent()==k );
assertTrue( j.getParent()==k );
assertTrue( k.getIs().size()==2 );
s.flush();
s.connection().commit();
s.close();
getSessions().evict(I.class);
s = getSessions().openSession();
assertTrue( s.find("from K k inner join k.is i where i.name = 'j'").size()==1 );
assertTrue( s.find("from K k inner join k.is i where i.name = 'i'").size()==1 );
assertTrue( s.find("from K k left join fetch k.is").size()==2 );
s.connection().commit();
s.close();
s = getSessions().openSession();
j = (J) s.get(J.class, jid);
i = (I) s.get(I.class, iid);
k = (K) s.get(K.class, kid);
s.delete(k);
s.delete(j);
s.delete(i);
s.flush();
s.connection().commit();
s.close();
}
public void testCategories() throws Exception {
Session s = openSession();
Category c = new Category();
c.setName(Category.ROOT_CATEGORY);
Category c1 = new Category();
Category c2 = new Category();
Category c3 = new Category();
c.getSubcategories().add(c1);
c.getSubcategories().add(c2);
c2.getSubcategories().add(null);
c2.getSubcategories().add(c3);
s.save(c);
s.flush();
s.connection().commit();
s.close();
s = openSession();
Transaction tx = s.beginTransaction();
s.lock(c, LockMode.UPGRADE);
Category loaded = (Category) s.load( Category.class, new Long( c3.getId() ) );
assertTrue( s.contains(c3) );
assertTrue(loaded==c3);
assertTrue( s.getCurrentLockMode(c3)==LockMode.NONE );
assertTrue( s.getCurrentLockMode(c)==LockMode.UPGRADE );
s.flush();
tx.commit();
s.close();
s = openSession();
loaded = (Category) s.load( Category.class, new Long( c.getId() ) );
assertFalse( Hibernate.isInitialized( loaded.getSubcategories() ) );
s.connection().commit();
s.close();
s = openSession();
s.lock(loaded, LockMode.NONE);
assertTrue( loaded.getSubcategories().size()==2 );
s.connection().commit();
s.close();
s = openSession();
c = (Category) s.load( Category.class, new Long( c.getId() ) );
System.out.println( c.getSubcategories() );
assertTrue( c.getSubcategories().get(0)!=null && c.getSubcategories().get(1)!=null );
List list = ( (Category) c.getSubcategories().get(1) ).getSubcategories();
assertTrue( list.get(1)!=null && list.get(0)==null );
assertTrue(
s.iterate("from Category c where c.name = org.hibernate.test.legacy.Category.ROOT_CATEGORY").hasNext()
);
s.delete(c);
s.flush();
s.connection().commit();
s.close();
}
public void testDeleteOwner() throws Exception {
Session s = openSession();
Qux q = new Qux();
s.save(q);
Fum f1 = new Fum( fumKey("f1") );
Fum f2 = new Fum( fumKey("f2") );
Set set = new HashSet();
set.add(f1);
set.add(f2);
List list = new LinkedList();
list.add(f1);
list.add(f2);
f1.setFum("f1");
f2.setFum("f2");
q.setFums(set);
q.setMoreFums(list);
s.save(f1);
s.save(f2);
s.flush();
s.connection().commit();
s.close();
s = openSession();
q = (Qux) s.load( Qux.class, q.getKey(), LockMode.UPGRADE );
s.lock( q, LockMode.UPGRADE );
s.delete(q);
s.flush();
s.connection().commit();
s.close();
s = openSession();
list = s.find("from Fum fum where not fum.fum='FRIEND'");
assertTrue( "deleted owner", list.size()==2 );
s.lock( list.get(0), LockMode.UPGRADE );
s.lock( list.get(1), LockMode.UPGRADE );
Iterator iter = list.iterator();
while ( iter.hasNext() ) {
s.delete( iter.next() );
}
s.flush();
s.connection().commit();
s.close();
}
public void testLocking() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Simple s1 = new Simple(); s1.setCount(1);
Simple s2 = new Simple(); s2.setCount(2);
Simple s3 = new Simple(); s3.setCount(3);
Simple s4 = new Simple(); s4.setCount(4);
s.save(s1, new Long(1) );
s.save(s2, new Long(2) );
s.save(s3, new Long(3) );
s.save(s4, new Long(4) );
assertTrue( s.getCurrentLockMode(s1)==LockMode.WRITE );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
s1 = (Simple) s.load(Simple.class, new Long(1), LockMode.NONE);
assertTrue( s.getCurrentLockMode(s1)==LockMode.READ || s.getCurrentLockMode(s1)==LockMode.NONE ); //depends if cache is enabled
s2 = (Simple) s.load(Simple.class, new Long(2), LockMode.READ);
assertTrue( s.getCurrentLockMode(s2)==LockMode.READ );
s3 = (Simple) s.load(Simple.class, new Long(3), LockMode.UPGRADE);
assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE );
s4 = (Simple) s.get(Simple.class, new Long(4), LockMode.UPGRADE_NOWAIT);
assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT );
s1 = (Simple) s.load(Simple.class, new Long(1), LockMode.UPGRADE); //upgrade
assertTrue( s.getCurrentLockMode(s1)==LockMode.UPGRADE );
s2 = (Simple) s.load(Simple.class, new Long(2), LockMode.NONE);
assertTrue( s.getCurrentLockMode(s2)==LockMode.READ );
s3 = (Simple) s.load(Simple.class, new Long(3), LockMode.READ);
assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE );
s4 = (Simple) s.load(Simple.class, new Long(4), LockMode.UPGRADE);
assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT );
s.lock(s2, LockMode.UPGRADE); //upgrade
assertTrue( s.getCurrentLockMode(s2)==LockMode.UPGRADE );
s.lock(s3, LockMode.UPGRADE);
assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE );
s.lock(s1, LockMode.UPGRADE_NOWAIT);
s.lock(s4, LockMode.NONE);
assertTrue( s.getCurrentLockMode(s4)==LockMode.UPGRADE_NOWAIT );
tx.commit();
tx = s.beginTransaction();
assertTrue( s.getCurrentLockMode(s3)==LockMode.NONE );
assertTrue( s.getCurrentLockMode(s1)==LockMode.NONE );
assertTrue( s.getCurrentLockMode(s2)==LockMode.NONE );
assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE );
s.lock(s1, LockMode.READ); //upgrade
assertTrue( s.getCurrentLockMode(s1)==LockMode.READ );
s.lock(s2, LockMode.UPGRADE); //upgrade
assertTrue( s.getCurrentLockMode(s2)==LockMode.UPGRADE );
s.lock(s3, LockMode.UPGRADE_NOWAIT); //upgrade
assertTrue( s.getCurrentLockMode(s3)==LockMode.UPGRADE_NOWAIT );
s.lock(s4, LockMode.NONE);
assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE );
s4.setName("s4");
s.flush();
assertTrue( s.getCurrentLockMode(s4)==LockMode.WRITE );
tx.commit();
tx = s.beginTransaction();
assertTrue( s.getCurrentLockMode(s3)==LockMode.NONE );
assertTrue( s.getCurrentLockMode(s1)==LockMode.NONE );
assertTrue( s.getCurrentLockMode(s2)==LockMode.NONE );
assertTrue( s.getCurrentLockMode(s4)==LockMode.NONE );
s.delete(s1); s.delete(s2); s.delete(s3); s.delete(s4);
tx.commit();
s.close();
}