001: /*
002: * Copyright 2002 Gareth Cronin
003: * This software is subject to the GNU Public Licence
004: */
005: package vqwiki.db;
006:
007: import java.lang.reflect.Method;
008: import java.sql.Connection;
009: import java.sql.DriverManager;
010:
011: import javax.naming.Context;
012: import javax.naming.InitialContext;
013:
014: import org.apache.commons.dbcp.AbandonedConfig;
015: import org.apache.commons.dbcp.AbandonedObjectPool;
016: import org.apache.commons.dbcp.DriverManagerConnectionFactory;
017: import org.apache.commons.dbcp.PoolableConnectionFactory;
018: import org.apache.commons.dbcp.PoolingDriver;
019: import org.apache.commons.pool.impl.GenericObjectPool;
020: import org.apache.log4j.Logger;
021:
022: import vqwiki.Environment;
023:
024: public class DatabaseConnection {
025:
026: private static final Logger logger = Logger
027: .getLogger(DatabaseConnection.class);
028: private static boolean poolInitialized = false;
029:
030: /**
031: *
032: */
033: public static void setUpConnectionPool(String url, String userName,
034: String password) throws Exception {
035: Environment env = Environment.getInstance();
036: Class.forName(env.getDriver());
037: AbandonedConfig config = new AbandonedConfig();
038: config
039: .setRemoveAbandoned(env
040: .getBooleanSetting(Environment.PROPERTY_DBCP_REMOVE_ABANDONED));
041: config
042: .setLogAbandoned(env
043: .getBooleanSetting(Environment.PROPERTY_DBCP_LOG_ABANDONED));
044: config
045: .setRemoveAbandonedTimeout(env
046: .getIntSetting(Environment.PROPERTY_DBCP_REMOVE_ABANDONED_TIMEOUT));
047: GenericObjectPool connectionPool = new AbandonedObjectPool(
048: null, config);
049: connectionPool.setMaxActive(env
050: .getIntSetting(Environment.PROPERTY_DBCP_MAX_ACTIVE));
051: connectionPool.setMaxIdle(env
052: .getIntSetting(Environment.PROPERTY_DBCP_MAX_IDLE));
053: connectionPool
054: .setMinEvictableIdleTimeMillis(env
055: .getIntSetting(Environment.PROPERTY_DBCP_MIN_EVICTABLE_IDLE_TIME) * 1000);
056: connectionPool
057: .setTestOnBorrow(env
058: .getBooleanSetting(Environment.PROPERTY_DBCP_TEST_ON_BORROW));
059: connectionPool
060: .setTestOnReturn(env
061: .getBooleanSetting(Environment.PROPERTY_DBCP_TEST_ON_RETURN));
062: connectionPool
063: .setTestWhileIdle(env
064: .getBooleanSetting(Environment.PROPERTY_DBCP_TEST_WHILE_IDLE));
065: connectionPool
066: .setTimeBetweenEvictionRunsMillis(env
067: .getIntSetting(Environment.PROPERTY_DBCP_TIME_BETWEEN_EVICTION_RUNS) * 1000);
068: connectionPool
069: .setNumTestsPerEvictionRun(env
070: .getIntSetting(Environment.PROPERTY_DBCP_NUM_TESTS_PER_EVICTION_RUN));
071: connectionPool
072: .setWhenExhaustedAction((byte) env
073: .getIntSetting(Environment.PROPERTY_DBCP_WHEN_EXHAUSTED_ACTION));
074: DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
075: url, userName, password);
076: new PoolableConnectionFactory(
077: connectionFactory,
078: connectionPool,
079: null,
080: env
081: .getStringSetting(Environment.PROPERTY_DBCP_VALIDATION_QUERY),
082: false, true);
083: PoolingDriver driver = new PoolingDriver();
084: driver.registerPool("vqwiki", connectionPool);
085: poolInitialized = true;
086: }
087:
088: /**
089: *
090: */
091: public static Connection getConnection() throws Exception {
092: logger.debug("get Connection from pool.");
093: String url = Environment.getInstance().getUrl();
094: String userName = Environment.getInstance().getUserName();
095: String password = Environment.getInstance().getPassword();
096: if (url.startsWith("jdbc:")) {
097: if (!poolInitialized) {
098: setUpConnectionPool(url, userName, password);
099: }
100: return DriverManager
101: .getConnection("jdbc:apache:commons:dbcp:vqwiki");
102: } else {
103: // Use Reflection here to avoid a compile time dependency
104: // on the DataSource interface. It's not available by default
105: // on j2se 1.3.
106: Context ctx = new InitialContext();
107: Object dataSource = ctx.lookup(url);
108: Method m;
109: Object args[];
110: if (userName.length() == 0) {
111: m = dataSource.getClass().getMethod("getConnection",
112: null);
113: args = new Object[] {};
114: } else {
115: m = dataSource.getClass().getMethod("getConnection",
116: new Class[] { String.class, String.class });
117: args = new Object[] { userName, password };
118: }
119: return (Connection) m.invoke(dataSource, args);
120: }
121: }
122:
123: /**
124: *
125: */
126: public static void setPoolInitialized(boolean poolInitialized) {
127: DatabaseConnection.poolInitialized = poolInitialized;
128: }
129:
130: /**
131: *
132: */
133: public static void closeConnection(Connection conn) {
134: logger.debug("release connection to pool.");
135: if (conn == null) {
136: String msg = "Attempt to call DatabaseConnection.closeConnection() "
137: + "with null connection value";
138: logger.info(msg);
139: return;
140: }
141: try {
142: conn.close();
143: } catch (Exception e) {
144: logger.error("Failure while closing connection", e);
145: }
146: }
147: }
|