You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ja...@apache.org on 2007/09/13 08:10:35 UTC

svn commit: r575181 - in /ofbiz/trunk/framework/entity: lib/commons-dbcp-20070730.jar src/org/ofbiz/entity/transaction/DBCPConnectionFactory.java

Author: jaz
Date: Wed Sep 12 23:10:34 2007
New Revision: 575181

URL: http://svn.apache.org/viewvc?rev=575181&view=rev
Log:
added DBCP connection pool and supporting class; need to modify ConnectionFactory to enable this; needs more testing before 'production' ready (snapshot of 1.3 DBCP)

Added:
    ofbiz/trunk/framework/entity/lib/commons-dbcp-20070730.jar   (with props)
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/transaction/DBCPConnectionFactory.java   (with props)

Added: ofbiz/trunk/framework/entity/lib/commons-dbcp-20070730.jar
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/lib/commons-dbcp-20070730.jar?rev=575181&view=auto
==============================================================================
Binary file - no diff available.

Propchange: ofbiz/trunk/framework/entity/lib/commons-dbcp-20070730.jar
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/transaction/DBCPConnectionFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/transaction/DBCPConnectionFactory.java?rev=575181&view=auto
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/transaction/DBCPConnectionFactory.java (added)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/transaction/DBCPConnectionFactory.java Wed Sep 12 23:10:34 2007
@@ -0,0 +1,125 @@
+package org.ofbiz.entity.transaction;
+
+import org.apache.commons.dbcp.ConnectionFactory;
+import org.apache.commons.dbcp.DriverConnectionFactory;
+import org.apache.commons.dbcp.PoolableConnectionFactory;
+import org.apache.commons.dbcp.managed.LocalXAConnectionFactory;
+import org.apache.commons.dbcp.managed.ManagedDataSource;
+import org.apache.commons.dbcp.managed.XAConnectionFactory;
+import org.apache.commons.pool.impl.GenericObjectPool;
+import org.ofbiz.base.util.Debug;
+import org.ofbiz.entity.GenericEntityException;
+import org.w3c.dom.Element;
+
+import javax.transaction.TransactionManager;
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.*;
+
+/**
+ * DBCPConnectionFactory
+ */
+public class DBCPConnectionFactory {
+
+    public static final String module = DBCPConnectionFactory.class.getName();
+
+    protected static Map dsCache = new HashMap();
+
+    public static Connection getConnection(String helperName, Element jotmJdbcElement) throws SQLException, GenericEntityException {
+        ManagedDataSource mds = (ManagedDataSource) dsCache.get(helperName);
+        if (mds != null) {
+            return TransactionFactory.getCursorConnection(helperName, mds.getConnection());
+        }
+
+        synchronized (DBCPConnectionFactory.class) {
+            mds = (ManagedDataSource) dsCache.get(helperName);
+            if (mds != null) {
+                return TransactionFactory.getCursorConnection(helperName, mds.getConnection());
+            }
+
+            // connection properties
+            TransactionManager txMgr = TransactionFactory.getTransactionManager();
+            String driverName = jotmJdbcElement.getAttribute("jdbc-driver");
+            String dbUri = jotmJdbcElement.getAttribute("jdbc-uri");
+            String dbUser = jotmJdbcElement.getAttribute("jdbc-username");
+            String dbPass = jotmJdbcElement.getAttribute("jdbc-password");
+
+            // pool settings
+            int maxSize, minSize;
+            try {
+                maxSize = Integer.parseInt(jotmJdbcElement.getAttribute("pool-maxsize"));
+            } catch (NumberFormatException nfe) {
+                Debug.logError("Problems with pool settings [pool-maxsize=" + jotmJdbcElement.getAttribute("pool-maxsize") + "]; the values MUST be numbers, using default of 20.", module);
+                maxSize = 20;
+            } catch (Exception e) {
+                Debug.logError(e, "Problems with pool settings", module);
+                maxSize = 20;
+            }
+            try {
+                minSize = Integer.parseInt(jotmJdbcElement.getAttribute("pool-minsize"));
+            } catch (NumberFormatException nfe) {
+                Debug.logError("Problems with pool settings [pool-minsize=" + jotmJdbcElement.getAttribute("pool-minsize") + "]; the values MUST be numbers, using default of 5.", module);
+                minSize = 2;
+            } catch (Exception e) {
+                Debug.logError(e, "Problems with pool settings", module);
+                minSize = 2;
+            }
+            int maxIdle = maxSize / 2;
+            maxIdle = maxIdle > minSize ? maxIdle : minSize;
+
+            // load the driver
+            System.setProperty("jdbc.drivers", driverName);
+            Driver jdbcDriver = DriverManager.getDriver(dbUri);
+
+            // connection factory properties
+            Properties cfProps = new Properties();
+            cfProps.put("user", dbUser);
+            cfProps.put("password", dbPass);
+
+            // create the connection factory
+            ConnectionFactory cf = new DriverConnectionFactory(jdbcDriver, dbUri, cfProps);
+
+            // wrap it with a LocalXAConnectionFactory
+            XAConnectionFactory xacf = new LocalXAConnectionFactory(txMgr, cf);
+                                                 
+            // configure the pool settings
+            GenericObjectPool pool = new GenericObjectPool();
+            pool.setTimeBetweenEvictionRunsMillis(600000);
+            pool.setMaxActive(maxSize);
+            pool.setMaxIdle(maxIdle);
+            pool.setMinIdle(minSize);
+            pool.setMaxWait(120000);            
+
+
+            // create the pool object factory
+            PoolableConnectionFactory factory = new PoolableConnectionFactory(xacf, pool, null, null, true, true);
+            factory.setValidationQuery("select example_type_id from example_type limit 1");
+
+            String transIso = jotmJdbcElement.getAttribute("isolation-level");
+            if (transIso != null && transIso.length() > 0) {
+                if ("Serializable".equals(transIso)) {
+                    factory.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
+                } else if ("RepeatableRead".equals(transIso)) {
+                    factory.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
+                } else if ("ReadUncommitted".equals(transIso)) {
+                    factory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
+                } else if ("ReadCommitted".equals(transIso)) {
+                    factory.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
+                } else if ("None".equals(transIso)) {
+                    factory.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE);
+                }
+            }
+            pool.setFactory(factory);
+
+            mds = new ManagedDataSource(pool, xacf.getTransactionRegistry());
+            mds.setAccessToUnderlyingConnectionAllowed(true);
+
+            // cache the pool
+            dsCache.put(helperName, mds);
+
+            return TransactionFactory.getCursorConnection(helperName, mds.getConnection());
+        }
+    }    
+}

Propchange: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/transaction/DBCPConnectionFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/transaction/DBCPConnectionFactory.java
------------------------------------------------------------------------------
    svn:keywords = "Date Rev Author URL Id"

Propchange: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/transaction/DBCPConnectionFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain