001: /*
002: * This software is released under a licence similar to the Apache Software Licence.
003: * See org.logicalcobwebs.proxool.package.html for details.
004: * The latest version is available at http://proxool.sourceforge.net
005: */
006: package org.logicalcobwebs.proxool;
007:
008: import net.sf.hibernate.HibernateException;
009: import net.sf.hibernate.Session;
010: import net.sf.hibernate.SessionFactory;
011: import net.sf.hibernate.cfg.Configuration;
012: import net.sf.hibernate.cfg.Environment;
013: import org.apache.commons.logging.Log;
014: import org.apache.commons.logging.LogFactory;
015:
016: import java.sql.Connection;
017: import java.util.Properties;
018:
019: /**
020: * Tests that we are able to get a connection from
021: * <a href="http://www.hibernate.org">Hibernate 2.x</a>.
022: * (Code contributed by Mark Eagle)
023: * @version $Revision: 1.3 $, $Date: 2006/03/24 00:14:59 $
024: * @author Bill Horsman {bill@logicalcobwebs.co.uk)
025: * @author $Author: billhorsman $ (current maintainer)
026: */
027: public class HibernateTest extends AbstractProxoolTest {
028:
029: private static final Log LOG = LogFactory
030: .getLog(HibernateTest.class);
031:
032: public HibernateTest(String alias) {
033: super (alias);
034: }
035:
036: /**
037: * Can we get a connection straight from Hibernate? We register the pool first
038: * and theb ask for Hibernate for it.
039: * @throws ProxoolException if there was a Proxool problem
040: * @throws HibernateException if there was a Hibernate problem
041: */
042: public void testSimpleHibernateConnection()
043: throws HibernateException, ProxoolException {
044:
045: String testName = "simpleHibernateConnection";
046: String alias = testName;
047:
048: String url = TestHelper.buildProxoolUrl(alias,
049: TestConstants.HYPERSONIC_DRIVER,
050: TestConstants.HYPERSONIC_TEST_URL);
051: Properties info = new Properties();
052: info.setProperty(ProxoolConstants.USER_PROPERTY,
053: TestConstants.HYPERSONIC_USER);
054: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
055: TestConstants.HYPERSONIC_PASSWORD);
056: info.setProperty(ProxoolConstants.VERBOSE_PROPERTY, "true");
057: ProxoolFacade.registerConnectionPool(url, info);
058:
059: Configuration configuration = null;
060: SessionFactory sessionFactory = null;
061: Session session = null;
062: Properties hibernateProperties = new Properties();
063: Connection connection = null;
064:
065: try {
066: hibernateProperties.setProperty(Environment.DRIVER,
067: ProxoolDriver.class.getName());
068: hibernateProperties.setProperty(Environment.URL, url);
069:
070: configuration = new Configuration()
071: .addProperties(hibernateProperties);
072:
073: // create a session object to the database
074: sessionFactory = configuration.buildSessionFactory();
075: session = sessionFactory.openSession();
076: assertNotNull("Expected a session", session);
077:
078: // Inspect the assigned connection to the session from
079: // the pool.
080: connection = session.connection();
081:
082: // assert that the connection is not null
083: assertNotNull("Expected a connection", connection);
084:
085: } finally {
086: try {
087: connection.close();
088: } catch (Exception e) {
089: LOG.error("Problem closing Hibernate session", e);
090: }
091: // close the session which will also close it's assigned connection
092: try {
093: session.close();
094: } catch (Exception e) {
095: LOG.error("Problem closing Hibernate session", e);
096: }
097: }
098:
099: try {
100: Thread.sleep(2000);
101: } catch (InterruptedException e) {
102: LOG.debug("Woken up", e);
103: }
104:
105: // We just need to test that we served at least one connection. I suspect that
106: // Hibernate is doing its own house keeping and getting at least an additional
107: // one.
108: assertTrue("servedCount", ProxoolFacade.getSnapshot(alias)
109: .getServedCount() > 0);
110: // They should definitely all be returned to the pool once we're finished though
111: assertEquals("activeCount", 0, ProxoolFacade.getSnapshot(alias)
112: .getActiveConnectionCount());
113:
114: }
115:
116: /**
117: * Can we get a connection from a Proxool pool that we have already registered? We
118: * ask Hibernate to lookup the pool by its alias.
119: * @throws ProxoolException if there was a Proxool problem
120: * @throws HibernateException if there was a Hibernate problem
121: */
122: public void testDirectHibernateConnection()
123: throws HibernateException, ProxoolException {
124:
125: String testName = "directHibernateConnection";
126: String alias = testName;
127:
128: String url = TestHelper.buildProxoolUrl(alias,
129: TestConstants.HYPERSONIC_DRIVER,
130: TestConstants.HYPERSONIC_TEST_URL);
131: Properties info = new Properties();
132: info.setProperty(ProxoolConstants.USER_PROPERTY,
133: TestConstants.HYPERSONIC_USER);
134: info.setProperty(ProxoolConstants.PASSWORD_PROPERTY,
135: TestConstants.HYPERSONIC_PASSWORD);
136: ProxoolFacade.registerConnectionPool(url, info);
137:
138: Configuration configuration = null;
139: SessionFactory sessionFactory = null;
140: Session session = null;
141: Properties hibernateProperties = new Properties();
142: Connection connection = null;
143:
144: try {
145: hibernateProperties.setProperty(
146: Environment.PROXOOL_EXISTING_POOL, "true");
147: hibernateProperties.setProperty(
148: Environment.PROXOOL_POOL_ALIAS, alias);
149:
150: configuration = new Configuration()
151: .addProperties(hibernateProperties);
152:
153: // create a session object to the database
154: sessionFactory = configuration.buildSessionFactory();
155: session = sessionFactory.openSession();
156: assertNotNull("Expected a session", session);
157:
158: // Inspect the assigned connection to the session from
159: // the pool.
160: connection = session.connection();
161:
162: // assert that the connection is not null
163: assertNotNull("Expected a connection", connection);
164:
165: } finally {
166: try {
167: connection.close();
168: } catch (Exception e) {
169: LOG.error("Problem closing Hibernate session", e);
170: }
171: // close the session which will also close it's assigned connection
172: try {
173: session.close();
174: } catch (Exception e) {
175: LOG.error("Problem closing Hibernate session", e);
176: }
177: }
178:
179: // We just need to test that we served at least one connection. I suspect that
180: // Hibernate is doing its own house keeping and getting at least an additional
181: // one.
182: assertTrue("servedCount", ProxoolFacade.getSnapshot(alias)
183: .getServedCount() > 0);
184: // They should definitely all be returned to the pool once we're finished though
185: assertEquals("activeCount", 0, ProxoolFacade.getSnapshot(alias)
186: .getActiveConnectionCount());
187:
188: }
189:
190: /**
191: * Can we get a connection from a pool configured by Hibernate
192: * @throws ProxoolException if there was a Proxool problem
193: * @throws HibernateException if there was a Hibernate problem
194: */
195: public void testHibernateConfiguredConnection()
196: throws HibernateException, ProxoolException {
197:
198: String testName = "hibernateConfiguredConnection";
199: String alias = testName;
200:
201: Configuration configuration = null;
202: SessionFactory sessionFactory = null;
203: Session session = null;
204: Properties hibernateProperties = new Properties();
205: Connection connection = null;
206:
207: try {
208: hibernateProperties
209: .setProperty(Environment.PROXOOL_XML,
210: "src/java-test/org/logicalcobwebs/proxool/hibernate.xml");
211: hibernateProperties.setProperty(
212: Environment.PROXOOL_POOL_ALIAS, alias);
213:
214: configuration = new Configuration()
215: .addProperties(hibernateProperties);
216:
217: // create a session object to the database
218: sessionFactory = configuration.buildSessionFactory();
219: session = sessionFactory.openSession();
220: assertNotNull("Expected a session", session);
221:
222: // Inspect the assigned connection to the session from
223: // the pool.
224: connection = session.connection();
225: assertNotNull("Expected a connection", connection);
226:
227: } finally {
228: try {
229: connection.close();
230: } catch (Exception e) {
231: LOG.error("Problem closing Hibernate session", e);
232: }
233: // close the session which will also close it's assigned connection
234: try {
235: session.close();
236: } catch (Exception e) {
237: LOG.error("Problem closing Hibernate session", e);
238: }
239: }
240:
241: // We just need to test that we served at least one connection. I suspect that
242: // Hibernate is doing its own house keeping and getting at least an additional
243: // one.
244: assertTrue("servedCount", ProxoolFacade.getSnapshot(alias)
245: .getServedCount() > 0);
246: // They should definitely all be returned to the pool once we're finished though
247: assertEquals("activeCount", 0, ProxoolFacade.getSnapshot(alias)
248: .getActiveConnectionCount());
249:
250: }
251:
252: }
253:
254: /*
255: Revision history:
256: $Log: HibernateTest.java,v $
257: Revision 1.3 2006/03/24 00:14:59 billhorsman
258: Changes for Hibernate 3
259:
260: Revision 1.2 2006/01/18 14:40:06 billhorsman
261: Unbundled Jakarta's Commons Logging.
262:
263: Revision 1.1 2003/09/28 09:38:30 billhorsman
264: New unit test for Hibernate.
265:
266: */
|