KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > test > connections > AggressiveReleaseTest


1 // $Id: AggressiveReleaseTest.java,v 1.4 2005/06/03 16:15:48 steveebersole Exp $
2
package org.hibernate.test.connections;
3
4 import org.hibernate.Session;
5 import org.hibernate.ConnectionReleaseMode;
6 import org.hibernate.ScrollableResults;
7 import org.hibernate.util.SerializationHelper;
8 import org.hibernate.test.tm.DummyConnectionProvider;
9 import org.hibernate.test.tm.DummyTransactionManagerLookup;
10 import org.hibernate.test.tm.DummyTransactionManager;
11 import org.hibernate.cfg.Configuration;
12 import org.hibernate.cfg.Environment;
13
14 import junit.framework.Test;
15 import junit.framework.TestSuite;
16
17 import java.sql.Connection JavaDoc;
18
19 /**
20  * Implementation of AggressiveReleaseTest.
21  *
22  * @author Steve Ebersole
23  */

24 public class AggressiveReleaseTest extends ConnectionManagementTestCase {
25
26     public AggressiveReleaseTest(String JavaDoc name) {
27         super( name );
28     }
29
30     public static Test suite() {
31         return new TestSuite( AggressiveReleaseTest.class );
32     }
33
34     protected void configure(Configuration cfg) {
35         cfg.setProperty( Environment.RELEASE_CONNECTIONS, ConnectionReleaseMode.AFTER_STATEMENT.toString() );
36         cfg.setProperty( Environment.CONNECTION_PROVIDER, DummyConnectionProvider.class.getName() );
37         cfg.setProperty( Environment.TRANSACTION_MANAGER_STRATEGY, DummyTransactionManagerLookup.class.getName() );
38     }
39
40     protected Session getSessionUnderTest() throws Throwable JavaDoc {
41         return openSession();
42     }
43
44     protected void reconnect(Session session) {
45         session.reconnect();
46     }
47
48     protected void prepare() throws Throwable JavaDoc {
49         DummyTransactionManager.INSTANCE.begin();
50     }
51
52     protected void done() throws Throwable JavaDoc {
53 // long initialCount = getSessions().getStatistics().getSessionCloseCount();
54
DummyTransactionManager.INSTANCE.commit();
55 // long currentCount = getSessions().getStatistics().get.getSessionCloseCount();
56
// assertEquals( "Transaction commit did not force close of session", initialCount, currentCount - 1 );
57
}
58
59     // Some additional tests specifically for the aggressive-release functionality...
60

61     public void testSerializationOnAfterStatementAggressiveRelease() throws Throwable JavaDoc {
62         prepare();
63         Session s = getSessionUnderTest();
64         Silly silly = new Silly( "silly" );
65         s.save( silly );
66
67         // this should cause the CM to obtain a connection, and then release it
68
s.flush();
69
70         // We should be able to serialize the session at this point...
71
SerializationHelper.serialize( s );
72
73         s.delete( silly );
74         s.flush();
75
76         release( s );
77         done();
78     }
79
80     public void testSerializationFailsOnAfterStatementAggressiveReleaseWithOpenResources() throws Throwable JavaDoc {
81         prepare();
82         Session s = getSessionUnderTest();
83
84         Silly silly = new Silly( "silly" );
85         s.save( silly );
86
87         // this should cause the CM to obtain a connection, and then release it
88
s.flush();
89
90         // both scroll() and iterate() cause the batcher to hold on
91
// to resources, which should make aggresive-release not release
92
// the connection (and thus cause serialization to fail)
93
ScrollableResults sr = s.createQuery( "from Silly" ).scroll();
94
95         try {
96             SerializationHelper.serialize( s );
97             fail( "Serialization allowed on connected session; or aggressive release released connection with open resources" );
98         }
99         catch( IllegalStateException JavaDoc e ) {
100             // expected behavior
101
}
102
103         // Closing the ScrollableResults does currently force the batcher to
104
// aggressively release the connection
105
sr.close();
106         SerializationHelper.serialize( s );
107
108         s.delete( silly );
109         s.flush();
110
111         release( s );
112         done();
113     }
114
115     public void testSuppliedConnection() throws Throwable JavaDoc {
116         prepare();
117
118         Connection JavaDoc originalConnection = DummyTransactionManager.INSTANCE.getCurrent().getConnection();
119         Session session = getSessions().openSession( originalConnection );
120
121         Silly silly = new Silly( "silly" );
122         session.save( silly );
123
124         // this will cause the connection manager to cycle through the aggressive release logic;
125
// it should not release the connection since we explicitly suplied it ourselves.
126
session.flush();
127
128         assertTrue( "Different connections", originalConnection == session.connection() );
129
130         session.delete( silly );
131         session.flush();
132
133         release( session );
134         done();
135     }
136 }
137
Popular Tags