1 package org.hibernate.connection; 3 4 import java.sql.Connection ; 5 import java.sql.DriverManager ; 6 import java.sql.SQLException ; 7 import java.util.Properties ; 8 9 import org.hibernate.HibernateException; 10 import org.hibernate.cfg.Environment; 11 import org.hibernate.util.PropertiesHelper; 12 import org.hibernate.util.StringHelper; 13 import org.hibernate.util.ConfigHelper; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 18 import org.logicalcobwebs.proxool.ProxoolException; 19 import org.logicalcobwebs.proxool.ProxoolFacade; 20 import org.logicalcobwebs.proxool.configuration.JAXPConfigurator; 21 import org.logicalcobwebs.proxool.configuration.PropertyConfigurator; 22 23 28 public class ProxoolConnectionProvider implements ConnectionProvider { 29 30 31 private static final String PROXOOL_JDBC_STEM = "proxool."; 32 33 private static final Log log = LogFactory.getLog(ProxoolConnectionProvider.class); 34 35 private String proxoolAlias; 36 37 private boolean existingPool; 39 40 private Integer isolation; 43 44 private boolean autocommit; 45 46 51 public Connection getConnection() throws SQLException { 52 Connection c = DriverManager.getConnection(proxoolAlias); 54 55 if (isolation!=null) c.setTransactionIsolation( isolation.intValue() ); 57 58 if ( c.getAutoCommit()!=autocommit ) c.setAutoCommit(autocommit); 60 61 return c; 63 } 64 65 70 public void closeConnection(Connection conn) throws SQLException { 71 conn.close(); 72 } 73 74 78 public void configure(Properties props) throws HibernateException { 79 80 String jaxpFile = props.getProperty(Environment.PROXOOL_XML); 82 String propFile = props.getProperty(Environment.PROXOOL_PROPERTIES); 83 String externalConfig = props.getProperty(Environment.PROXOOL_EXISTING_POOL); 84 85 proxoolAlias = props.getProperty(Environment.PROXOOL_POOL_ALIAS); 87 88 if ( "true".equals(externalConfig) ) { 91 92 if ( !StringHelper.isNotEmpty(proxoolAlias) ) { 94 String msg = "Cannot configure Proxool Provider to use an existing in memory pool without the " + Environment.PROXOOL_POOL_ALIAS + " property set."; 95 log.fatal(msg); 96 throw new HibernateException(msg); 97 } 98 proxoolAlias = PROXOOL_JDBC_STEM + proxoolAlias; 100 101 existingPool = true; 103 104 log.info("Configuring Proxool Provider using existing pool in memory: " + proxoolAlias); 105 106 } 108 else if ( StringHelper.isNotEmpty(jaxpFile) ) { 109 110 log.info("Configuring Proxool Provider using JAXPConfigurator: " + jaxpFile); 111 112 if ( !StringHelper.isNotEmpty(proxoolAlias) ) { 114 String msg = "Cannot configure Proxool Provider to use JAXP without the " + Environment.PROXOOL_POOL_ALIAS + " property set."; 115 log.fatal(msg); 116 throw new HibernateException(msg); 117 } 118 119 try { 120 JAXPConfigurator.configure( ConfigHelper.getConfigStreamReader(jaxpFile), false ); 121 } 122 catch (ProxoolException e) { 123 String msg = "Proxool Provider unable to load JAXP configurator file: " + jaxpFile; 124 log.fatal(msg, e); 125 throw new HibernateException(msg, e); 126 } 127 128 proxoolAlias = PROXOOL_JDBC_STEM + proxoolAlias; 130 log.info("Configuring Proxool Provider to use pool alias: " + proxoolAlias); 131 132 } 134 else if ( StringHelper.isNotEmpty(propFile) ) { 135 136 log.info("Configuring Proxool Provider using Properties File: " + propFile); 137 138 if ( !StringHelper.isNotEmpty(proxoolAlias) ) { 140 String msg = "Cannot configure Proxool Provider to use Properties File without the " + Environment.PROXOOL_POOL_ALIAS + " property set."; 141 log.fatal(msg); 142 throw new HibernateException(msg); 143 } 144 145 try { 146 PropertyConfigurator.configure( ConfigHelper.getConfigProperties(propFile) ); 147 } 148 catch (ProxoolException e) { 149 String msg = "Proxool Provider unable to load load Property configurator file: " + propFile; 150 log.fatal(msg, e); 151 throw new HibernateException(msg, e); 152 } 153 154 proxoolAlias = PROXOOL_JDBC_STEM + proxoolAlias; 156 log.info("Configuring Proxool Provider to use pool alias: " + proxoolAlias); 157 } 158 159 isolation = PropertiesHelper.getInteger(Environment.ISOLATION, props); 161 if (isolation!=null) { 162 log.info("JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) ); 163 } 164 165 autocommit = PropertiesHelper.getBoolean(Environment.AUTOCOMMIT, props); 166 log.info("autocommit mode: " + autocommit); 167 } 168 169 173 public void close() throws HibernateException { 174 175 if (existingPool) { 177 return; 178 } 179 180 try { 182 ProxoolFacade.shutdown(0); 183 } 184 catch (Exception e) { 185 log.warn("Exception occured when closing the Proxool pool", e); 188 throw new HibernateException("Exception occured when closing the Proxool pool", e); 189 } 190 } 191 192 195 public boolean supportsAggressiveRelease() { 196 return false; 197 } 198 199 } 200 | Popular Tags |