Author: mircea.markus
Date: 2009-02-26 09:26:18 -0500 (Thu, 26 Feb 2009)
New Revision: 7792
Added:
core/branches/flat/src/main/java/org/horizon/loader/jdbc/PooledConnectionFactory.java
core/branches/flat/src/main/java/org/horizon/loader/jdbc/SimpleConnectionFactory.java
Log:
ongoing FileCacheStoreWork
Added:
core/branches/flat/src/main/java/org/horizon/loader/jdbc/PooledConnectionFactory.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/PooledConnectionFactory.java
(rev 0)
+++
core/branches/flat/src/main/java/org/horizon/loader/jdbc/PooledConnectionFactory.java 2009-02-26
14:26:18 UTC (rev 7792)
@@ -0,0 +1,101 @@
+package org.horizon.loader.jdbc;
+
+import com.mchange.v2.c3p0.ComboPooledDataSource;
+import com.mchange.v2.c3p0.DataSources;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.horizon.loader.CacheLoaderException;
+
+import java.beans.PropertyVetoException;
+import java.net.URL;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Properties;
+
+/**
+ * Pooled connection factory based on C3P0. For a complete configuration reference, look
<a
+ *
href="http://www.mchange.com/projects/c3p0/index.html#configuration&...;.
The connection pool can be configured
+ * in various ways, as described <a
href="http://www.mchange.com/projects/c3p0/index.html#configuration_...;.
+ * The simplest way is by having an <tt>c3p0.properties</tt> file in the
classpath. If no such file is found, default,
+ * hardcoded valus will be used.
+ *
+ * @author Mircea.Markus(a)jboss.com
+ */
+public class PooledConnectionFactory extends ConnectionFactory {
+
+ private static Log log = LogFactory.getLog(PooledConnectionFactory.class);
+ private ComboPooledDataSource pooledDataSource;
+
+ @Override
+ public void start(JdbcCacheStoreConfig config) throws CacheLoaderException {
+ logFileOverride();
+ pooledDataSource = new ComboPooledDataSource();
+ pooledDataSource.setProperties(new Properties());
+ try {
+ pooledDataSource.setDriverClass(config.getDriverClass()); //loads the jdbc
driver
+ } catch (PropertyVetoException e) {
+ String message = "Error while instatianting JDBC driver: '" +
config.getDriverClass();
+ log.error(message, e);
+ throw new CacheLoaderException(message, e);
+ }
+ pooledDataSource.setJdbcUrl(config.getConnectionUrl());
+ pooledDataSource.setUser(config.getUserName());
+ pooledDataSource.setPassword(config.getPassword());
+ }
+
+ private void logFileOverride() {
+ URL propsUrl =
Thread.currentThread().getContextClassLoader().getResource("c3p0.properties");
+ URL xmlUrl =
Thread.currentThread().getContextClassLoader().getResource("c3p0-config.xml");
+ if (log.isInfoEnabled() && propsUrl != null) {
+ log.info("Found 'c3p0.properties' in classpath: " +
propsUrl);
+ }
+ if (log.isInfoEnabled() && xmlUrl != null) {
+ log.info("Found 'c3p0-config.xml' in classpath: " + xmlUrl);
+ }
+ }
+
+ @Override
+ public void stop() {
+ try {
+ DataSources.destroy(pooledDataSource);
+ if (log.isTraceEnabled()) {
+ log.debug("Sucessfully stopped PooledConnectionFactory.");
+ }
+ }
+ catch (SQLException sqle) {
+ log.warn("Could not destroy C3P0 connection pool: " +
pooledDataSource, sqle);
+ }
+ }
+
+ @Override
+ public Connection getConnection() throws CacheLoaderException {
+ try {
+ if (log.isTraceEnabled()) {
+ log.trace("DataSource before checkout (NumBusyConnectionsAllUsers) :
" + pooledDataSource.getNumBusyConnectionsAllUsers());
+ log.trace("DataSource before checkout (NumConnectionsAllUsers) : "
+ pooledDataSource.getNumConnectionsAllUsers());
+ }
+ Connection connection = pooledDataSource.getConnection();
+ if (log.isTraceEnabled()) {
+ log.trace("DataSource after checkout (NumBusyConnectionsAllUsers) :
" + pooledDataSource.getNumBusyConnectionsAllUsers());
+ log.trace("DataSource after checkout (NumConnectionsAllUsers) : " +
pooledDataSource.getNumConnectionsAllUsers());
+ log.trace("Connection checked out: " + connection);
+ }
+ return connection;
+ } catch (SQLException e) {
+ throw new CacheLoaderException("Failed obtaining connection from
PooledDataSource", e);
+ }
+ }
+
+ @Override
+ public void releaseConnection(Connection conn) {
+ try {
+ conn.close();
+ } catch (SQLException e) {
+ log.warn("Issues closing connection", e);
+ }
+ }
+
+ ComboPooledDataSource getPooledDataSource() {
+ return pooledDataSource;
+ }
+}
Property changes on:
core/branches/flat/src/main/java/org/horizon/loader/jdbc/PooledConnectionFactory.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Added:
core/branches/flat/src/main/java/org/horizon/loader/jdbc/SimpleConnectionFactory.java
===================================================================
--- core/branches/flat/src/main/java/org/horizon/loader/jdbc/SimpleConnectionFactory.java
(rev 0)
+++
core/branches/flat/src/main/java/org/horizon/loader/jdbc/SimpleConnectionFactory.java 2009-02-26
14:26:18 UTC (rev 7792)
@@ -0,0 +1,79 @@
+package org.horizon.loader.jdbc;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.horizon.loader.CacheLoaderException;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+/**
+ * // TODO: Mircea: Document this!
+ *
+ * @author
+ */
+public class SimpleConnectionFactory extends ConnectionFactory {
+
+ private static Log log = LogFactory.getLog(SimpleConnectionFactory.class);
+
+ private String connectionUrl;
+ private String userName;
+ private String password;
+
+ public void start(JdbcCacheStoreConfig config) throws CacheLoaderException {
+ loadDriver(config.getDriverClass());
+ this.connectionUrl = config.getConnectionUrl();
+ this.userName = config.getUserName();
+ this.password = config.getPassword();
+ }
+
+ public void stop() {
+ //do nothing
+ }
+
+ public Connection getConnection() throws CacheLoaderException {
+ try {
+ Connection connection = DriverManager.getConnection(connectionUrl, userName,
password);
+ if (connection == null)
+ throw new CacheLoaderException("Received null connection from the
DriverManager!");
+ return connection;
+ } catch (SQLException e) {
+ throw new CacheLoaderException("Could not obtain a new connection",
e);
+ }
+ }
+
+ public void releaseConnection(Connection conn) {
+ try {
+ conn.close();
+ } catch (SQLException e) {
+ log.warn("Failure while closing the connection to the database ", e);
+ }
+ }
+
+ private void loadDriver(String driverClass) throws CacheLoaderException {
+ try {
+ if (log.isTraceEnabled()) {
+ log.trace("Attempting to load driver " + driverClass);
+ }
+ Class.forName(driverClass).newInstance();
+ }
+ catch (Throwable th) {
+ String message = "Failed loading driver with class: '" +
driverClass + "'";
+ log.error(message, th);
+ throw new CacheLoaderException(message, th);
+ }
+ }
+
+ public String getConnectionUrl() {
+ return connectionUrl;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+}
Property changes on:
core/branches/flat/src/main/java/org/horizon/loader/jdbc/SimpleConnectionFactory.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF