下面列出了org.hibernate.classic.Session#disconnect ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testDisconnect() throws Exception {
Session s = openSession();
Foo foo = new Foo();
Foo foo2 = new Foo();
s.save(foo);
s.save(foo2);
foo2.setFoo(foo);
s.flush();
s.connection().commit();
s.disconnect();
s.reconnect();
s.delete(foo);
foo2.setFoo(null);
s.flush();
s.connection().commit();
s.disconnect();
s.reconnect();
s.delete(foo2);
s.flush();
s.connection().commit();
s.close();
}
public void testUserProvidedConnection() throws Exception {
ConnectionProvider dcp = new DriverManagerConnectionProvider();
dcp.configure( Environment.getProperties() );
Session s = getSessions().openSession( dcp.getConnection() );
Transaction tx = s.beginTransaction();
s.find("from Fo");
tx.commit();
Connection c = s.disconnect();
assertTrue( c!=null );
s.reconnect(c);
tx = s.beginTransaction();
s.find("from Fo");
tx.commit();
assertTrue( s.close()==c );
c.close();
}
public void testProxyArray() throws Exception {
Session s = openSession();
GlarchProxy g = new Glarch();
Glarch g1 = new Glarch();
Glarch g2 = new Glarch();
g.setProxyArray( new GlarchProxy[] { g1, g2 } );
Glarch g3 = new Glarch();
s.save(g3);
g2.setProxyArray( new GlarchProxy[] {null, g3, g} );
Set set = new HashSet();
set.add(g1);
set.add(g2);
g.setProxySet(set);
s.save(g);
s.save(g1);
s.save(g2);
Serializable id = s.getIdentifier(g);
s.flush();
s.connection().commit();
s.close();
s = openSession();
g = (GlarchProxy) s.load(Glarch.class, id);
assertTrue( "array of proxies", g.getProxyArray().length==2 );
assertTrue( "array of proxies", g.getProxyArray()[0]!=null );
assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[0]==null );
assertTrue("deferred load test",g.getProxyArray()[1].getProxyArray()[2]==g );
assertTrue( "set of proxies", g.getProxySet().size()==2 );
Iterator iter = s.iterate("from Glarch g");
while ( iter.hasNext() ) {
iter.next();
iter.remove();
}
s.flush();
s.connection().commit();
s.disconnect();
SerializationHelper.deserialize( SerializationHelper.serialize(s) );
s.close();
}
public void testWierdSession() throws Exception {
Session s = openSession();
Transaction t = s.beginTransaction();
Serializable id = s.save( new Foo() );
t.commit();
s.close();
s = openSession();
s.setFlushMode(FlushMode.NEVER);
t = s.beginTransaction();
Foo foo = (Foo) s.get(Foo.class, id);
t.commit();
s.disconnect();
s.reconnect();
t = s.beginTransaction();
s.flush();
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
foo = (Foo) s.get(Foo.class, id);
s.delete(foo);
t.commit();
s.close();
}
public void testQueryLockMode() throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Bar bar = new Bar();
s.save(bar);
s.flush();
bar.setString("changed");
Baz baz = new Baz();
baz.setFoo(bar);
s.save(baz);
Query q = s.createQuery("from Foo foo, Bar bar");
if ( !(getDialect() instanceof DB2Dialect) ) {
q.setLockMode("bar", LockMode.UPGRADE);
}
Object[] result = (Object[]) q.uniqueResult();
Object b = result[0];
assertTrue( s.getCurrentLockMode(b)==LockMode.WRITE && s.getCurrentLockMode( result[1] )==LockMode.WRITE );
tx.commit();
s.disconnect();
s.reconnect();
tx = s.beginTransaction();
assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
s.find("from Foo foo");
assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
q = s.createQuery("from Foo foo");
q.setLockMode("foo", LockMode.READ);
q.list();
assertTrue( s.getCurrentLockMode(b)==LockMode.READ);
s.evict(baz);
tx.commit();
s.disconnect();
s.reconnect();
tx = s.beginTransaction();
assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
s.delete( s.load( Baz.class, baz.getCode() ) );
assertTrue( s.getCurrentLockMode(b)==LockMode.NONE );
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
q = s.createQuery("from Foo foo, Bar bar, Bar bar2");
if ( !(getDialect() instanceof DB2Dialect) ) {
q.setLockMode("bar", LockMode.UPGRADE);
}
q.setLockMode("bar2", LockMode.READ);
result = (Object[]) q.list().get(0);
if ( !(getDialect() instanceof DB2Dialect) ) {
assertTrue( s.getCurrentLockMode( result[0] )==LockMode.UPGRADE && s.getCurrentLockMode( result[1] )==LockMode.UPGRADE );
}
s.delete( result[0] );
tx.commit();
s.close();
}