1 4 package org.ofbiz.minerva.pool.jdbc; 5 6 import java.sql.Connection ; 7 import java.sql.DriverManager ; 8 import java.sql.SQLException ; 9 import java.util.Properties ; 10 11 import org.apache.log4j.Logger; 12 import org.ofbiz.minerva.pool.ObjectPool; 13 import org.ofbiz.minerva.pool.PoolObjectFactory; 14 import org.ofbiz.minerva.pool.cache.ObjectCache; 15 16 25 public class JDBCConnectionFactory extends PoolObjectFactory { 26 27 private String url; 28 private Properties props; 29 private String userName; 30 private String password; 31 private int psCacheSize = 10; 32 private ObjectPool pool; 33 34 private static Logger log = Logger.getLogger(JDBCConnectionFactory.class); 35 36 40 public JDBCConnectionFactory() { 41 } 42 43 46 public void setConnectURL(String url) { 47 this.url = url; 48 } 49 50 53 public String getConnectURL() { 54 return url; 55 } 56 57 61 public void setConnectProperties(Properties props) { 62 this.props = props; 63 } 64 65 68 public Properties getConnectProperties() { 69 return props; 70 } 71 72 76 public void setUser(String userName) { 77 this.userName = userName; 78 } 79 80 83 public String getUser() { 84 return userName; 85 } 86 87 91 public void setPassword(String password) { 92 this.password = password; 93 } 94 95 98 public String getPassword() { 99 return password; 100 } 101 102 107 public void setPSCacheSize(int size) { 108 psCacheSize = size; 109 } 110 111 115 public int getPSCacheSize() { 116 return psCacheSize; 117 } 118 119 122 public void poolStarted(ObjectPool pool) { 123 if (log.isDebugEnabled()) 124 log.debug("Starting"); 125 126 super.poolStarted(pool); 127 if (url == null) { 128 log.error("Must specify JDBC connection URL"); 129 throw new IllegalStateException ("Must specify JDBC connection URL to " + getClass().getName()); 130 } 131 this.pool = pool; 132 } 133 134 137 public void poolClosing(ObjectPool pool) { 138 if (log.isDebugEnabled()) 139 log.debug("Stopping"); 140 141 super.poolClosing(pool); 142 this.pool = null; 143 } 144 145 148 public Object createObject(Object parameters) throws Exception { 149 150 log.debug("Opening new connection"); 151 152 try { 153 if (userName != null && userName.length() > 0) 154 return DriverManager.getConnection(url, userName, password); 155 else if (props != null) 156 return DriverManager.getConnection(url, props); 157 else 158 return DriverManager.getConnection(url); 159 } catch (SQLException e) { 160 log.error("SQL Error", e); 161 throw e; 162 } 163 } 164 165 169 public Object prepareObject(Object pooledObject) { 170 Connection con = (Connection ) pooledObject; 171 ConnectionInPool wrapper = new ConnectionInPool(con); 172 wrapper.setPSCacheSize(psCacheSize); 173 return wrapper; 174 } 175 176 180 public Object translateObject(Object clientObject) { 181 return ((ConnectionInPool) clientObject).getUnderlyingConnection(); 182 } 183 184 188 public Object returnObject(Object clientObject) { 189 ConnectionInPool wrapper = (ConnectionInPool) clientObject; 190 Connection con = wrapper.getUnderlyingConnection(); 191 try { 192 wrapper.reset(); 193 } catch (SQLException e) { 194 pool.markObjectAsInvalid(clientObject); 195 } 196 return con; 197 } 198 199 202 public void deleteObject(Object pooledObject) { 203 Connection con = (Connection ) pooledObject; 204 try { 205 con.rollback(); 206 } catch (SQLException ignored) { 207 } 208 209 ObjectCache cache = (ObjectCache) ConnectionInPool.psCaches.remove(con); 211 if (cache != null) 212 cache.close(); 213 214 try { 215 con.close(); 216 } catch (SQLException ignored) { 217 } 218 } 219 } 220 221 224 | Popular Tags |