KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > HibernateTest


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool;
7
8 import net.sf.hibernate.HibernateException;
9 import net.sf.hibernate.Session;
10 import net.sf.hibernate.SessionFactory;
11 import net.sf.hibernate.cfg.Configuration;
12 import net.sf.hibernate.cfg.Environment;
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import java.sql.Connection JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 /**
20  * Tests that we are able to get a connection from
21  * <a HREF="http://www.hibernate.org">Hibernate 2.x</a>.
22  * (Code contributed by Mark Eagle)
23  * @version $Revision: 1.3 $, $Date: 2006/03/24 00:14:59 $
24  * @author Bill Horsman {bill@logicalcobwebs.co.uk)
25  * @author $Author: billhorsman $ (current maintainer)
26  */

27 public class HibernateTest extends AbstractProxoolTest {
28
29     private static final Log LOG = LogFactory.getLog(HibernateTest.class);
30
31     public HibernateTest(String JavaDoc alias) {
32         super(alias);
33     }
34
35     /**
36      * Can we get a connection straight from Hibernate? We register the pool first
37      * and theb ask for Hibernate for it.
38      * @throws ProxoolException if there was a Proxool problem
39      * @throws HibernateException if there was a Hibernate problem
40      */

41     public void testSimpleHibernateConnection() throws HibernateException, ProxoolException {
42
43         String JavaDoc testName = "simpleHibernateConnection";
44         String JavaDoc alias = testName;
45
46         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
47                 TestConstants.HYPERSONIC_DRIVER,
48                 TestConstants.HYPERSONIC_TEST_URL);
49         Properties JavaDoc info = new Properties JavaDoc();
50         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
51         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
52         info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
53         ProxoolFacade.registerConnectionPool(url, info);
54
55         Configuration configuration = null;
56         SessionFactory sessionFactory = null;
57         Session session = null;
58         Properties JavaDoc hibernateProperties = new Properties JavaDoc();
59         Connection JavaDoc connection = null;
60
61         try {
62             hibernateProperties.setProperty(Environment.DRIVER, ProxoolDriver.class.getName());
63             hibernateProperties.setProperty(Environment.URL, url);
64
65             configuration = new Configuration().addProperties(hibernateProperties);
66
67             // create a session object to the database
68
sessionFactory = configuration.buildSessionFactory();
69             session = sessionFactory.openSession();
70             assertNotNull("Expected a session", session);
71
72             // Inspect the assigned connection to the session from
73
// the pool.
74
connection = session.connection();
75
76             // assert that the connection is not null
77
assertNotNull("Expected a connection", connection);
78
79         } finally {
80             try {
81                 connection.close();
82             } catch (Exception JavaDoc e) {
83                 LOG.error("Problem closing Hibernate session", e);
84             }
85             // close the session which will also close it's assigned connection
86
try {
87                 session.close();
88             } catch (Exception JavaDoc e) {
89                 LOG.error("Problem closing Hibernate session", e);
90             }
91         }
92
93         try {
94             Thread.sleep(2000);
95         } catch (InterruptedException JavaDoc e) {
96             LOG.debug("Woken up", e);
97         }
98
99         // We just need to test that we served at least one connection. I suspect that
100
// Hibernate is doing its own house keeping and getting at least an additional
101
// one.
102
assertTrue("servedCount", ProxoolFacade.getSnapshot(alias).getServedCount() > 0);
103         // They should definitely all be returned to the pool once we're finished though
104
assertEquals("activeCount", 0, ProxoolFacade.getSnapshot(alias).getActiveConnectionCount());
105
106     }
107
108     /**
109      * Can we get a connection from a Proxool pool that we have already registered? We
110      * ask Hibernate to lookup the pool by its alias.
111      * @throws ProxoolException if there was a Proxool problem
112      * @throws HibernateException if there was a Hibernate problem
113      */

114     public void testDirectHibernateConnection() throws HibernateException, ProxoolException {
115
116         String JavaDoc testName = "directHibernateConnection";
117         String JavaDoc alias = testName;
118
119         String JavaDoc url = TestHelper.buildProxoolUrl(alias,
120                 TestConstants.HYPERSONIC_DRIVER,
121                 TestConstants.HYPERSONIC_TEST_URL);
122         Properties JavaDoc info = new Properties JavaDoc();
123         info.setProperty(ProxoolConstants.USER_PROPERTY, TestConstants.HYPERSONIC_USER);
124         info.setProperty(ProxoolConstants.PASSWORD_PROPERTY, TestConstants.HYPERSONIC_PASSWORD);
125         ProxoolFacade.registerConnectionPool(url, info);
126
127         Configuration configuration = null;
128         SessionFactory sessionFactory = null;
129         Session session = null;
130         Properties JavaDoc hibernateProperties = new Properties JavaDoc();
131         Connection JavaDoc connection = null;
132
133         try {
134             hibernateProperties.setProperty(Environment.PROXOOL_EXISTING_POOL, "true");
135             hibernateProperties.setProperty(Environment.PROXOOL_POOL_ALIAS, alias);
136
137             configuration = new Configuration().addProperties(hibernateProperties);
138
139             // create a session object to the database
140
sessionFactory = configuration.buildSessionFactory();
141             session = sessionFactory.openSession();
142             assertNotNull("Expected a session", session);
143
144             // Inspect the assigned connection to the session from
145
// the pool.
146
connection = session.connection();
147
148             // assert that the connection is not null
149
assertNotNull("Expected a connection", connection);
150
151         } finally {
152             try {
153                 connection.close();
154             } catch (Exception JavaDoc e) {
155                 LOG.error("Problem closing Hibernate session", e);
156             }
157             // close the session which will also close it's assigned connection
158
try {
159                 session.close();
160             } catch (Exception JavaDoc e) {
161                 LOG.error("Problem closing Hibernate session", e);
162             }
163         }
164
165         // We just need to test that we served at least one connection. I suspect that
166
// Hibernate is doing its own house keeping and getting at least an additional
167
// one.
168
assertTrue("servedCount", ProxoolFacade.getSnapshot(alias).getServedCount() > 0);
169         // They should definitely all be returned to the pool once we're finished though
170
assertEquals("activeCount", 0, ProxoolFacade.getSnapshot(alias).getActiveConnectionCount());
171
172     }
173
174     /**
175      * Can we get a connection from a pool configured by Hibernate
176      * @throws ProxoolException if there was a Proxool problem
177      * @throws HibernateException if there was a Hibernate problem
178      */

179     public void testHibernateConfiguredConnection() throws HibernateException, ProxoolException {
180
181         String JavaDoc testName = "hibernateConfiguredConnection";
182         String JavaDoc alias = testName;
183
184         Configuration configuration = null;
185         SessionFactory sessionFactory = null;
186         Session session = null;
187         Properties JavaDoc hibernateProperties = new Properties JavaDoc();
188         Connection JavaDoc connection = null;
189
190         try {
191             hibernateProperties.setProperty(Environment.PROXOOL_XML, "src/java-test/org/logicalcobwebs/proxool/hibernate.xml");
192             hibernateProperties.setProperty(Environment.PROXOOL_POOL_ALIAS, alias);
193
194             configuration = new Configuration().addProperties(hibernateProperties);
195
196             // create a session object to the database
197
sessionFactory = configuration.buildSessionFactory();
198             session = sessionFactory.openSession();
199             assertNotNull("Expected a session", session);
200
201             // Inspect the assigned connection to the session from
202
// the pool.
203
connection = session.connection();
204             assertNotNull("Expected a connection", connection);
205
206         } finally {
207             try {
208                 connection.close();
209             } catch (Exception JavaDoc e) {
210                 LOG.error("Problem closing Hibernate session", e);
211             }
212             // close the session which will also close it's assigned connection
213
try {
214                 session.close();
215             } catch (Exception JavaDoc e) {
216                 LOG.error("Problem closing Hibernate session", e);
217             }
218         }
219
220         // We just need to test that we served at least one connection. I suspect that
221
// Hibernate is doing its own house keeping and getting at least an additional
222
// one.
223
assertTrue("servedCount", ProxoolFacade.getSnapshot(alias).getServedCount() > 0);
224         // They should definitely all be returned to the pool once we're finished though
225
assertEquals("activeCount", 0, ProxoolFacade.getSnapshot(alias).getActiveConnectionCount());
226
227     }
228
229 }
230
231 /*
232 Revision history:
233 $Log: HibernateTest.java,v $
234 Revision 1.3 2006/03/24 00:14:59 billhorsman
235 Changes for Hibernate 3
236
237 Revision 1.2 2006/01/18 14:40:06 billhorsman
238 Unbundled Jakarta's Commons Logging.
239
240 Revision 1.1 2003/09/28 09:38:30 billhorsman
241 New unit test for Hibernate.
242
243 */

244
245
Popular Tags