1 package org.hibernate.test.connections; 3 4 import org.hibernate.test.TestCase; 5 import org.hibernate.Session; 6 import org.hibernate.util.SerializationHelper; 7 8 13 public abstract class ConnectionManagementTestCase extends TestCase { 14 15 public ConnectionManagementTestCase(String name) { 16 super( name ); 17 } 18 19 protected final String [] getMappings() { 20 return new String [] { "connections/Silly.hbm.xml" }; 21 } 22 23 protected void release(Session session) { 24 if ( session != null ) { 25 try { 26 session.close(); 27 } 28 catch( Throwable ignore ) { 29 } 30 } 31 } 32 33 protected void prepare() throws Throwable { 34 } 35 protected abstract Session getSessionUnderTest() throws Throwable ; 36 protected abstract void reconnect(Session session) throws Throwable ; 37 protected void done() throws Throwable { 38 } 39 40 public final void testConnectedSerialization() throws Throwable { 41 prepare(); 42 Session sessionUnderTest = getSessionUnderTest(); 43 44 sessionUnderTest.connection(); 45 46 try { 47 SerializationHelper.serialize( sessionUnderTest ); 48 49 fail( "Serialization of connected session allowed!" ); 50 } 51 catch( IllegalStateException e ) { 52 } 54 finally { 55 release( sessionUnderTest ); 56 done(); 57 } 58 } 59 60 public final void testManualDisconnectedSerialization() throws Throwable { 61 prepare(); 62 Session sessionUnderTest = getSessionUnderTest(); 63 64 sessionUnderTest.disconnect(); 65 66 SerializationHelper.serialize( sessionUnderTest ); 67 68 release( sessionUnderTest ); 69 done(); 70 } 71 72 public final void testManualDisconnectChain() throws Throwable { 73 prepare(); 74 Session sessionUnderTest = getSessionUnderTest(); 75 76 sessionUnderTest.disconnect(); 77 78 byte[] bytes = SerializationHelper.serialize( sessionUnderTest ); 79 Session s2 = ( Session ) SerializationHelper.deserialize( bytes ); 80 81 reconnect( s2 ); 82 83 s2.disconnect(); 84 reconnect( s2 ); 85 86 release( sessionUnderTest ); 87 release( s2 ); 88 done(); 89 } 90 91 public final void testManualDisconnectWithOpenResources() throws Throwable { 92 prepare(); 93 Session sessionUnderTest = getSessionUnderTest(); 94 95 Silly silly = new Silly( "tester" ); 96 sessionUnderTest.save( silly ); 97 sessionUnderTest.flush(); 98 99 sessionUnderTest.createQuery( "from Silly" ).iterate(); 100 101 sessionUnderTest.disconnect(); 102 SerializationHelper.serialize( sessionUnderTest ); 103 104 reconnect( sessionUnderTest ); 105 sessionUnderTest.createQuery( "from Silly" ).scroll(); 106 107 sessionUnderTest.disconnect(); 108 SerializationHelper.serialize( sessionUnderTest ); 109 110 reconnect( sessionUnderTest ); 111 sessionUnderTest.delete( silly ); 112 sessionUnderTest.flush(); 113 114 release( sessionUnderTest ); 115 done(); 116 } 117 } 118 | Popular Tags |