1 11 12 package org.jivesoftware.database; 13 14 import org.jivesoftware.util.JiveGlobals; 15 import org.jivesoftware.util.Log; 16 17 import java.io.IOException ; 18 import java.sql.Connection ; 19 import java.sql.SQLException ; 20 21 26 public class DefaultConnectionProvider implements ConnectionProvider { 27 28 private ConnectionPool connectionPool = null; 29 30 private String driver; 31 private String serverURL; 32 private String username; 33 private String password; 34 private int minConnections = 3; 35 private int maxConnections = 10; 36 37 40 private double connectionTimeout = 0.5; 41 42 47 private boolean mysqlUseUnicode; 48 49 private Object initLock = new Object (); 50 51 54 public DefaultConnectionProvider() { 55 loadProperties(); 56 } 57 58 public boolean isPooled() { 59 return true; 60 } 61 62 public Connection getConnection() throws SQLException { 63 if (connectionPool == null) { 64 synchronized (initLock) { 66 if (connectionPool == null) { 68 Log.error("Warning: DbConnectionDefaultPool.getConnection() was " + 69 "called before the internal pool has been initialized."); 70 return null; 71 } 72 } 73 } 74 75 return connectionPool.getConnection(); 76 } 77 78 public void start() { 79 synchronized (initLock) { 81 82 try { 83 connectionPool = new ConnectionPool(driver, serverURL, username, 84 password, minConnections, maxConnections, connectionTimeout, 85 mysqlUseUnicode); 86 } 87 catch (IOException e) { 88 Log.error(e); 89 } 90 } 91 } 92 93 public void restart() { 94 destroy(); 96 loadProperties(); 98 start(); 100 } 101 102 public void destroy() { 103 if (connectionPool != null) { 104 try { 105 connectionPool.destroy(); 106 } 107 catch (Exception e) { 108 Log.error(e); 109 } 110 } 111 connectionPool = null; 113 } 114 115 public void finalize() { 116 destroy(); 117 } 118 119 125 public String getDriver() { 126 return driver; 127 } 128 129 135 public void setDriver(String driver) { 136 this.driver = driver; 137 saveProperties(); 138 } 139 140 145 public String getServerURL() { 146 return serverURL; 147 } 148 149 154 public void setServerURL(String serverURL) { 155 this.serverURL = serverURL; 156 saveProperties(); 157 } 158 159 165 public String getUsername() { 166 return username; 167 } 168 169 175 public void setUsername(String username) { 176 this.username = username; 177 saveProperties(); 178 } 179 180 186 public String getPassword() { 187 return password; 188 } 189 190 196 public void setPassword(String password) { 197 this.password = password; 198 saveProperties(); 199 } 200 201 207 public int getMinConnections() { 208 return minConnections; 209 } 210 211 217 public void setMinConnections(int minConnections) { 218 this.minConnections = minConnections; 219 saveProperties(); 220 } 221 222 229 public int getMaxConnections() { 230 return maxConnections; 231 } 232 233 240 public void setMaxConnections(int maxConnections) { 241 this.maxConnections = maxConnections; 242 saveProperties(); 243 } 244 245 252 public double getConnectionTimeout() { 253 return connectionTimeout; 254 } 255 256 264 public void setConnectionTimeout(double connectionTimeout) { 265 this.connectionTimeout = connectionTimeout; 266 saveProperties(); 267 } 268 269 public boolean isMysqlUseUnicode() { 270 return mysqlUseUnicode; 271 } 272 273 276 private void loadProperties() { 277 driver = JiveGlobals.getXMLProperty("database.defaultProvider.driver"); 278 serverURL = JiveGlobals.getXMLProperty("database.defaultProvider.serverURL"); 279 username = JiveGlobals.getXMLProperty("database.defaultProvider.username"); 280 password = JiveGlobals.getXMLProperty("database.defaultProvider.password"); 281 String minCons = JiveGlobals.getXMLProperty("database.defaultProvider.minConnections"); 282 String maxCons = JiveGlobals.getXMLProperty("database.defaultProvider.maxConnections"); 283 String conTimeout = JiveGlobals.getXMLProperty("database.defaultProvider.connectionTimeout"); 284 mysqlUseUnicode = Boolean.valueOf(JiveGlobals.getXMLProperty("database.mysql.useUnicode")).booleanValue(); 286 try { 287 if (minCons != null) { 288 minConnections = Integer.parseInt(minCons); 289 } 290 if (maxCons != null) { 291 maxConnections = Integer.parseInt(maxCons); 292 } 293 if (conTimeout != null) { 294 connectionTimeout = Double.parseDouble(conTimeout); 295 } 296 } 297 catch (Exception e) { 298 Log.error("Error: could not parse default pool properties. " + 299 "Make sure the values exist and are correct.", e); 300 } 301 } 302 303 306 private void saveProperties() { 307 308 JiveGlobals.setXMLProperty("database.defaultProvider.driver", driver); 309 JiveGlobals.setXMLProperty("database.defaultProvider.serverURL", serverURL); 310 JiveGlobals.setXMLProperty("database.defaultProvider.username", username); 311 JiveGlobals.setXMLProperty("database.defaultProvider.password", password); 312 313 JiveGlobals.setXMLProperty("database.defaultProvider.minConnections", 314 Integer.toString(minConnections)); 315 JiveGlobals.setXMLProperty("database.defaultProvider.maxConnections", 316 Integer.toString(maxConnections)); 317 JiveGlobals.setXMLProperty("database.defaultProvider.connectionTimeout", 318 Double.toString(connectionTimeout)); 319 } 320 } 321 | Popular Tags |