1 23 24 package org.apache.slide.store.impl.rdbms; 25 26 import java.sql.Connection ; 27 import java.sql.Driver ; 28 import java.sql.DriverManager ; 29 import java.sql.SQLException ; 30 import java.util.Hashtable ; 31 32 import org.apache.commons.dbcp.DriverManagerConnectionFactory; 33 import org.apache.commons.dbcp.PoolableConnectionFactory; 34 import org.apache.commons.dbcp.PoolingDriver; 35 import org.apache.commons.pool.impl.GenericObjectPool; 36 import org.apache.slide.common.NamespaceAccessToken; 37 import org.apache.slide.common.ServiceInitializationFailedException; 38 import org.apache.slide.common.ServiceParameterErrorException; 39 import org.apache.slide.common.ServiceParameterMissingException; 40 import org.apache.slide.store.ContentStore; 41 import org.apache.slide.store.LockStore; 42 import org.apache.slide.store.NodeStore; 43 import org.apache.slide.store.RevisionDescriptorStore; 44 import org.apache.slide.store.RevisionDescriptorsStore; 45 import org.apache.slide.store.SecurityStore; 46 import org.apache.slide.util.logger.Logger; 47 48 57 public class JDBCStore 58 extends AbstractRDBMSStore 59 implements LockStore, NodeStore, RevisionDescriptorsStore, RevisionDescriptorStore, SecurityStore, ContentStore { 60 61 public static final String DBCP_URL = "jdbc:apache:commons:dbcp"; 62 63 public static final String TRANSACTION_NONE = "NONE"; 64 public static final String TRANSACTION_READ_UNCOMMITTED = "READ_UNCOMMITTED"; 65 public static final String TRANSACTION_READ_COMMITTED = "READ_COMMITTED"; 66 public static final String TRANSACTION_REPEATABLE_READ = "REPEATABLE_READ"; 67 public static final String TRANSACTION_SERIALIZABLE = "SERIALIZABLE"; 68 69 public static final int DEFAUT_ISOLATION_LEVEL = Connection.TRANSACTION_READ_COMMITTED; 70 71 protected static String isolationLevelToString(int isolationLevel) { 72 String levelString; 73 switch (isolationLevel) { 74 case Connection.TRANSACTION_NONE : 75 levelString = TRANSACTION_NONE; 76 break; 77 case Connection.TRANSACTION_READ_UNCOMMITTED : 78 levelString = TRANSACTION_READ_UNCOMMITTED; 79 break; 80 case Connection.TRANSACTION_READ_COMMITTED : 81 levelString = TRANSACTION_READ_COMMITTED; 82 break; 83 case Connection.TRANSACTION_REPEATABLE_READ : 84 levelString = TRANSACTION_REPEATABLE_READ; 85 break; 86 case Connection.TRANSACTION_SERIALIZABLE : 87 levelString = TRANSACTION_SERIALIZABLE; 88 break; 89 default : 90 levelString = "UNKNOWN"; 91 break; 92 } 93 return levelString; 94 95 } 96 97 protected static int stringToIsolationLevelToString(String levelString) { 98 if (TRANSACTION_NONE.equals(levelString)) { 99 return Connection.TRANSACTION_NONE; 100 } else if (TRANSACTION_READ_UNCOMMITTED.equals(levelString)) { 101 return Connection.TRANSACTION_READ_UNCOMMITTED; 102 } else if (TRANSACTION_READ_COMMITTED.equals(levelString)) { 103 return Connection.TRANSACTION_READ_COMMITTED; 104 } else if (TRANSACTION_REPEATABLE_READ.equals(levelString)) { 105 return Connection.TRANSACTION_REPEATABLE_READ; 106 } else if (TRANSACTION_SERIALIZABLE.equals(levelString)) { 107 return Connection.TRANSACTION_SERIALIZABLE; 108 } else { 109 return -1; 110 } 111 } 112 113 115 118 protected String driver; 119 120 123 protected String url; 124 125 128 protected String user = ""; 129 130 133 protected String password = ""; 134 135 protected boolean useDbcpPooling = false; 136 137 protected String dbcpPoolName = "dbcpPool" + System.identityHashCode(this); 138 139 protected int maxPooledConnections = -1; 140 141 protected int isolationLevel = DEFAUT_ISOLATION_LEVEL; 142 143 145 155 public void setParameters(Hashtable parameters) 156 throws ServiceParameterErrorException, ServiceParameterMissingException { 157 158 String value; 159 160 value = (String ) parameters.get("driver"); 162 if (value == null) { 163 throw new ServiceParameterMissingException(this, "driver"); 164 } else { 165 driver = value; 166 } 167 168 value = (String ) parameters.get("url"); 170 if (value == null) { 171 throw new ServiceParameterMissingException(this, "url"); 172 } else { 173 url = value; 174 } 175 176 value = (String ) parameters.get("user"); 178 if (value != null) { 179 user = value; 180 } 181 182 value = (String ) parameters.get("password"); 184 if (value != null) { 185 password = value; 186 } 187 188 value = (String ) parameters.get("isolation"); 189 if (value != null) { 190 isolationLevel = stringToIsolationLevelToString(value); 191 if (isolationLevel == -1) { 192 getLogger().log( 193 "Could not set isolation level '" 194 + value 195 + "', allowed levels are " 196 + TRANSACTION_NONE 197 + ", " 198 + TRANSACTION_READ_UNCOMMITTED 199 + ", " 200 + TRANSACTION_READ_COMMITTED 201 + ", " 202 + TRANSACTION_REPEATABLE_READ 203 + ", " 204 + TRANSACTION_SERIALIZABLE, 205 LOG_CHANNEL, 206 Logger.WARNING); 207 isolationLevel = DEFAUT_ISOLATION_LEVEL; 208 } 209 } 210 211 value = (String ) parameters.get("dbcpPooling"); 212 if (value != null) { 213 useDbcpPooling = "true".equals(value); 214 } 215 216 if (useDbcpPooling) { 217 value = (String ) parameters.get("maxPooledConnections"); 218 if (value != null) { 219 try { 220 maxPooledConnections = Integer.parseInt(value); 221 } catch (NumberFormatException nfe) { 222 getLogger().log( 223 "Could not set maximum pooled connections, parameter must be integer", 224 LOG_CHANNEL, 225 Logger.WARNING); 226 227 } 228 } 229 } 230 231 super.setParameters(parameters); 232 } 233 234 246 public synchronized void initialize(NamespaceAccessToken token) throws ServiceInitializationFailedException { 247 248 if (!alreadyInitialized) { 250 try { 251 getLogger().log("Loading and registering driver '" + driver + "'", LOG_CHANNEL, Logger.INFO); 253 Class driverClass = Class.forName(driver); 254 Driver driverInstance = (Driver ) driverClass.newInstance(); 255 256 String levelString = isolationLevelToString(isolationLevel); 257 258 getLogger().log("Setting isolation level '" + levelString + "'", LOG_CHANNEL, Logger.INFO); 259 260 if (useDbcpPooling) { 262 getLogger().log("Using DBCP pooling", LOG_CHANNEL, Logger.INFO); 263 GenericObjectPool connectionPool = new GenericObjectPool(null); 264 if (maxPooledConnections != -1) { 265 connectionPool.setMaxActive(maxPooledConnections); 266 } 267 getLogger().log( 268 "Number of connections set to " + connectionPool.getMaxActive(), 269 LOG_CHANNEL, 270 Logger.INFO); 271 272 DriverManagerConnectionFactory connectionFactory = 273 new DriverManagerConnectionFactory(url, user, password); 274 new PoolableConnectionFactory( 275 connectionFactory, 276 connectionPool, 277 null, 281 null, 282 false, 283 false, 284 isolationLevel); 285 PoolingDriver driver = new PoolingDriver(); 286 driver.registerPool(dbcpPoolName, connectionPool); 287 } else { 290 DriverManager.registerDriver(driverInstance); 291 getLogger().log("Not using DBCP pooling", LOG_CHANNEL, Logger.WARNING); 292 } 293 } catch (Exception e) { 294 getLogger().log( 295 "Loading and registering driver '" + driver + "' failed (" + e.getMessage() + ")", 296 LOG_CHANNEL, 297 Logger.ERROR); 298 throw new ServiceInitializationFailedException(this, e); 299 } finally { 300 alreadyInitialized = true; 301 } 302 } 303 304 } 305 306 protected Connection getNewConnection() throws SQLException { 307 308 Connection connection; 309 if (useDbcpPooling) { 310 try { 311 connection = DriverManager.getConnection(DBCP_URL + ":" + dbcpPoolName); 312 } catch (SQLException e) { 313 getLogger().log("Could not create connection. Reason: " + e, LOG_CHANNEL, Logger.EMERGENCY); 314 throw e; 315 } 316 } else { 317 try { 318 connection = DriverManager.getConnection(url, user, password); 319 } catch (SQLException e) { 320 getLogger().log("Could not create connection. Reason: " + e, LOG_CHANNEL, Logger.EMERGENCY); 321 throw e; 322 } 323 324 try { 325 if (connection.getTransactionIsolation() != isolationLevel) { 326 connection.setTransactionIsolation(isolationLevel); 327 } 328 } catch (SQLException e) { 329 getLogger().log( 330 "Could not set isolation level '" + isolationLevelToString(isolationLevel) + "'. Reason: " + e, 331 LOG_CHANNEL, 332 Logger.WARNING); 333 } 334 335 if (connection.getAutoCommit()) { 336 connection.setAutoCommit(false); 337 } 338 339 } 340 341 return connection; 342 } 343 344 protected boolean includeBranchInXid() { 345 return false; 346 } 347 } 348 | Popular Tags |