1 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 ; 17 import java.util.Properties ; 18 19 27 public class HibernateTest extends AbstractProxoolTest { 28 29 private static final Log LOG = LogFactory.getLog(HibernateTest.class); 30 31 public HibernateTest(String alias) { 32 super(alias); 33 } 34 35 41 public void testSimpleHibernateConnection() throws HibernateException, ProxoolException { 42 43 String testName = "simpleHibernateConnection"; 44 String alias = testName; 45 46 String url = TestHelper.buildProxoolUrl(alias, 47 TestConstants.HYPERSONIC_DRIVER, 48 TestConstants.HYPERSONIC_TEST_URL); 49 Properties info = new Properties (); 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 hibernateProperties = new Properties (); 59 Connection 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 sessionFactory = configuration.buildSessionFactory(); 69 session = sessionFactory.openSession(); 70 assertNotNull("Expected a session", session); 71 72 connection = session.connection(); 75 76 assertNotNull("Expected a connection", connection); 78 79 } finally { 80 try { 81 connection.close(); 82 } catch (Exception e) { 83 LOG.error("Problem closing Hibernate session", e); 84 } 85 try { 87 session.close(); 88 } catch (Exception e) { 89 LOG.error("Problem closing Hibernate session", e); 90 } 91 } 92 93 try { 94 Thread.sleep(2000); 95 } catch (InterruptedException e) { 96 LOG.debug("Woken up", e); 97 } 98 99 assertTrue("servedCount", ProxoolFacade.getSnapshot(alias).getServedCount() > 0); 103 assertEquals("activeCount", 0, ProxoolFacade.getSnapshot(alias).getActiveConnectionCount()); 105 106 } 107 108 114 public void testDirectHibernateConnection() throws HibernateException, ProxoolException { 115 116 String testName = "directHibernateConnection"; 117 String alias = testName; 118 119 String url = TestHelper.buildProxoolUrl(alias, 120 TestConstants.HYPERSONIC_DRIVER, 121 TestConstants.HYPERSONIC_TEST_URL); 122 Properties info = new Properties (); 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 hibernateProperties = new Properties (); 131 Connection 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 sessionFactory = configuration.buildSessionFactory(); 141 session = sessionFactory.openSession(); 142 assertNotNull("Expected a session", session); 143 144 connection = session.connection(); 147 148 assertNotNull("Expected a connection", connection); 150 151 } finally { 152 try { 153 connection.close(); 154 } catch (Exception e) { 155 LOG.error("Problem closing Hibernate session", e); 156 } 157 try { 159 session.close(); 160 } catch (Exception e) { 161 LOG.error("Problem closing Hibernate session", e); 162 } 163 } 164 165 assertTrue("servedCount", ProxoolFacade.getSnapshot(alias).getServedCount() > 0); 169 assertEquals("activeCount", 0, ProxoolFacade.getSnapshot(alias).getActiveConnectionCount()); 171 172 } 173 174 179 public void testHibernateConfiguredConnection() throws HibernateException, ProxoolException { 180 181 String testName = "hibernateConfiguredConnection"; 182 String alias = testName; 183 184 Configuration configuration = null; 185 SessionFactory sessionFactory = null; 186 Session session = null; 187 Properties hibernateProperties = new Properties (); 188 Connection 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 sessionFactory = configuration.buildSessionFactory(); 198 session = sessionFactory.openSession(); 199 assertNotNull("Expected a session", session); 200 201 connection = session.connection(); 204 assertNotNull("Expected a connection", connection); 205 206 } finally { 207 try { 208 connection.close(); 209 } catch (Exception e) { 210 LOG.error("Problem closing Hibernate session", e); 211 } 212 try { 214 session.close(); 215 } catch (Exception e) { 216 LOG.error("Problem closing Hibernate session", e); 217 } 218 } 219 220 assertTrue("servedCount", ProxoolFacade.getSnapshot(alias).getServedCount() > 0); 224 assertEquals("activeCount", 0, ProxoolFacade.getSnapshot(alias).getActiveConnectionCount()); 226 227 } 228 229 } 230 231 244 245 | Popular Tags |