incubator-connectors-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kwri...@apache.org
Subject svn commit: r1335409 - /incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/
Date Tue, 08 May 2012 10:37:27 GMT
Author: kwright
Date: Tue May  8 10:37:27 2012
New Revision: 1335409

URL: http://svn.apache.org/viewvc?rev=1335409&view=rev
Log:
New home-grown JDBC pool.

Added:
    incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPool.java
  (with props)
    incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPoolManager.java
  (with props)
    incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/WrappedConnection.java
  (with props)
Modified:
    incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnection.java
    incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnectionFactory.java

Added: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPool.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPool.java?rev=1335409&view=auto
==============================================================================
--- incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPool.java
(added)
+++ incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPool.java
Tue May  8 10:37:27 2012
@@ -0,0 +1,124 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.manifoldcf.crawler.connectors.jdbc;
+
+import java.sql.*;
+import javax.naming.*;
+import javax.sql.*;
+import java.util.*;
+import org.apache.manifoldcf.crawler.system.Logging;
+
+/** The class that defines a connection pool.
+*/
+public class ConnectionPool
+{
+  public static final String _rcsid = "@(#)$Id$";
+
+  protected String dbURL;
+  protected String userName;
+  protected String password;
+  protected volatile int freePointer;
+  protected volatile int activeConnections;
+  protected Connection[] freeConnections;
+  
+  /** Constructor */
+  public ConnectionPool(String dbURL, String userName, String password, int maxConnections)
+  {
+    this.dbURL = dbURL;
+    this.userName = userName;
+    this.password = password;
+    this.freeConnections = new Connection[maxConnections];
+    this.freePointer = 0;
+    this.activeConnections = 0;
+  }
+  
+  /** Obtain a connection from the pool.
+  * This will wait until a connection is free, if the pool is already completely tapped.
+  * The connection is returned by the "close" operation, executed on the connection.
+  * (This requires us to wrap the actual connection object).
+  */
+  public WrappedConnection getConnection()
+    throws SQLException, InterruptedException
+  {
+    while (true)
+    {
+      synchronized (this)
+      {
+        if (freePointer > 0)
+        {
+          Connection rval = freeConnections[--freePointer];
+          freeConnections[freePointer] = null;
+          return new WrappedConnection(this,rval);
+        }
+        if (activeConnections == freeConnections.length)
+        {
+          // Wait until kicked; we hope something will free up...
+          this.wait();
+          continue;
+        }
+        // Increment active connection counter, because we're about to mint a new connection,
and break out of our loop
+        activeConnections++;
+        break;
+      }
+    }
+    
+    // Create a new connection.  If we fail at this we need to restore the number of active
connections, so catch any failures
+    Connection rval2 = null;
+    try
+    {
+      if (userName != null)
+        rval2 = DriverManager.getConnection(dbURL, userName, password);
+      else
+        rval2 = DriverManager.getConnection(dbURL);
+    }
+    finally
+    {
+      if (rval2 == null)
+        activeConnections--;
+    }
+    return new WrappedConnection(this,rval2);
+  }
+  
+  /** Close down the pool.
+  */
+  public synchronized void closePool()
+  {
+    for (int i = 0 ; i < freePointer ; i++)
+    {
+      try
+      {
+        freeConnections[i].close();
+      }
+      catch (SQLException e)
+      {
+        Logging.connectors.warn("Error closing pooled connection: "+e.getMessage(),e);
+      }
+      freeConnections[i] = null;
+    }
+  }
+  
+  protected synchronized void releaseConnection(Connection connection)
+  {
+    freeConnections[freePointer++] = connection;
+    notifyAll();
+  }
+  
+}
+
+

Propchange: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPool.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPool.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPoolManager.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPoolManager.java?rev=1335409&view=auto
==============================================================================
--- incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPoolManager.java
(added)
+++ incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPoolManager.java
Tue May  8 10:37:27 2012
@@ -0,0 +1,60 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.manifoldcf.crawler.connectors.jdbc;
+
+import javax.naming.*;
+import javax.sql.*;
+import java.util.*;
+
+
+/** An instance of this class manages a number of (independent) connection pools.
+*/
+public class ConnectionPoolManager
+{
+  public static final String _rcsid = "@(#)$Id$";
+
+  protected Map<String,ConnectionPool> poolMap;
+
+  public ConnectionPoolManager(int count)
+  {
+    poolMap = new HashMap<String,ConnectionPool>(count);
+  }
+  
+  /** Look for a pool with a given key.
+  */
+  public synchronized ConnectionPool getPool(String poolKey)
+  {
+    return poolMap.get(poolKey);
+  }
+  
+  /** Set up a pool with a given key.
+  */
+  public synchronized ConnectionPool addAlias(String poolKey, String driverClassName, String
dbURL,
+    String userName, String password, int maxSize)
+    throws ClassNotFoundException, InstantiationException, IllegalAccessException
+  {
+    Class.forName(driverClassName);
+    ConnectionPool cp = new ConnectionPool(driverClassName,userName,password,maxSize);
+    poolMap.put(poolKey,cp);
+    return cp;
+  }
+  
+}
+
+

Propchange: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPoolManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/ConnectionPoolManager.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnection.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnection.java?rev=1335409&r1=1335408&r2=1335409&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnection.java
(original)
+++ incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnection.java
Tue May  8 10:37:27 2012
@@ -229,7 +229,7 @@ public class JDBCConnection
     {
       try
       {
-        Connection tempConnection = JDBCConnectionFactory.getConnection(jdbcProvider,host,databaseName,userName,password);
+        WrappedConnection tempConnection = JDBCConnectionFactory.getConnection(jdbcProvider,host,databaseName,userName,password);
         JDBCConnectionFactory.releaseConnection(tempConnection);
       }
       catch (Throwable e)
@@ -306,10 +306,10 @@ public class JDBCConnection
     {
       try
       {
-        Connection tempConnection = JDBCConnectionFactory.getConnection(jdbcProvider,host,databaseName,userName,password);
+        WrappedConnection tempConnection = JDBCConnectionFactory.getConnection(jdbcProvider,host,databaseName,userName,password);
         try
         {
-          execute(tempConnection,query,params,false,0);
+          execute(tempConnection.getConnection(),query,params,false,0);
         }
         finally
         {
@@ -791,7 +791,7 @@ public class JDBCConnection
 
   protected class JDBCResultSet implements IDynamicResultSet
   {
-    protected Connection connection;
+    protected WrappedConnection connection;
     protected Statement stmt;
     protected ResultSet rs;
     protected ResultSetMetaData rsmd;
@@ -907,7 +907,7 @@ public class JDBCConnection
     protected String query;
 
     protected Throwable exception = null;
-    protected Connection connection = null;
+    protected WrappedConnection connection = null;
     protected Statement stmt = null;
     protected ResultSet rs = null;
     protected ResultSetMetaData rsmd = null;
@@ -926,7 +926,7 @@ public class JDBCConnection
       {
         connection = JDBCConnectionFactory.getConnection(jdbcProvider,host,databaseName,userName,password);
         // lightest statement type
-        stmt = connection.createStatement();
+        stmt = connection.getConnection().createStatement();
         stmt.execute(query);
         rs = stmt.getResultSet();
         rsmd = rs.getMetaData();
@@ -1003,7 +1003,7 @@ public class JDBCConnection
       return exception;
     }
 
-    public Connection getConnection()
+    public WrappedConnection getConnection()
     {
       return connection;
     }
@@ -1031,7 +1031,7 @@ public class JDBCConnection
 
   protected class JDBCPSResultSet implements IDynamicResultSet
   {
-    protected Connection connection;
+    protected WrappedConnection connection;
     protected PreparedStatement ps;
     protected ResultSet rs;
     protected ResultSetMetaData rsmd;
@@ -1184,7 +1184,7 @@ public class JDBCConnection
     protected ArrayList params;
     protected String query;
 
-    protected Connection connection = null;
+    protected WrappedConnection connection = null;
     protected Throwable exception = null;
     protected PreparedStatement ps = null;
     protected ResultSet rs = null;
@@ -1204,7 +1204,7 @@ public class JDBCConnection
       try
       {
         connection = JDBCConnectionFactory.getConnection(jdbcProvider,host,databaseName,userName,password);
-        ps = connection.prepareStatement(query);
+        ps = connection.getConnection().prepareStatement(query);
         loadPS(ps, params);
         rs = ps.executeQuery();
         rsmd = rs.getMetaData();
@@ -1278,7 +1278,7 @@ public class JDBCConnection
       return exception;
     }
 
-    public Connection getConnection()
+    public WrappedConnection getConnection()
     {
       return connection;
     }

Modified: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnectionFactory.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnectionFactory.java?rev=1335409&r1=1335408&r2=1335409&view=diff
==============================================================================
--- incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnectionFactory.java
(original)
+++ incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/JDBCConnectionFactory.java
Tue May  8 10:37:27 2012
@@ -29,8 +29,6 @@ import javax.naming.*;
 import javax.sql.*;
 import java.util.*;
 
-import com.bitmechanic.sql.*;
-
 /** This class creates a connection
 */
 public class JDBCConnectionFactory
@@ -65,7 +63,7 @@ public class JDBCConnectionFactory
   }
 
 
-  public static Connection getConnection(String providerName, String host, String database,
String userName, String password)
+  public static WrappedConnection getConnection(String providerName, String host, String
database, String userName, String password)
     throws ManifoldCFException, ServiceInterruption
   {
     if (database.length() == 0)
@@ -113,31 +111,31 @@ public class JDBCConnectionFactory
         // to be displayed.
         poolKey += "/" + userName + "/" + ManifoldCF.hash(password);
 
+        ConnectionPool cp;
         synchronized (_pool)
         {
-          ConnectionPool cp = null;
-          try
-          {
-            cp = _pool.getPool(poolKey);
-          }
-          catch (Exception e)
-          {
-          }
+          cp = _pool.getPool(poolKey);
           if (cp == null)
           {
-            _pool.addAlias(poolKey, driverClassName, dburl,
-              userName, password, 25, 300, 3600, 30, false);
-            // cp = _pool.getPool(poolKey);
+            // Register the driver here
+            Class.forName(driverClassName);
+            System.out.println("Class name '"+driverClassName+"'; URL = '"+dburl+"'");
+            cp =_pool.addAlias(poolKey, driverClassName, dburl,
+              userName, password, 30);
           }
         }
-        return DriverManager.getConnection(
-          ConnectionPoolManager.URL_PREFIX + poolKey, null, null);
+        return cp.getConnection();
       }
       else
         throw new ManifoldCFException("Can't get connection since pool driver did not initialize
properly");
     }
+    catch (InterruptedException e)
+    {
+      throw new ManifoldCFException(e.getMessage(),ManifoldCFException.INTERRUPTED);
+    }
     catch (java.sql.SQLException e)
     {
+      e.printStackTrace();
       // Unfortunately, the connection pool manager manages to eat all actual connection
setup errors.  This makes it very hard to figure anything out
       // when something goes wrong.  So, we try again, going directly this time as a means
of getting decent error feedback.
       try
@@ -173,17 +171,10 @@ public class JDBCConnectionFactory
     }
   }
 
-  public static void releaseConnection(Connection c)
+  public static void releaseConnection(WrappedConnection c)
     throws ManifoldCFException, ServiceInterruption
   {
-    try
-    {
-      c.close();
-    }
-    catch (java.sql.SQLException e)
-    {
-      throw new ManifoldCFException("Error releasing connection: "+e.getMessage(),e);
-    }
+    c.release();
   }
 
 }

Added: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/WrappedConnection.java
URL: http://svn.apache.org/viewvc/incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/WrappedConnection.java?rev=1335409&view=auto
==============================================================================
--- incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/WrappedConnection.java
(added)
+++ incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/WrappedConnection.java
Tue May  8 10:37:27 2012
@@ -0,0 +1,55 @@
+/* $Id$ */
+
+/**
+* Licensed to the Apache Software Foundation (ASF) under one or more
+* contributor license agreements. See the NOTICE file distributed with
+* this work for additional information regarding copyright ownership.
+* The ASF licenses this file to You under the Apache License, Version 2.0
+* (the "License"); you may not use this file except in compliance with
+* the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+package org.apache.manifoldcf.crawler.connectors.jdbc;
+
+import java.sql.*;
+
+/** The class that represents a connection from a pool.
+*/
+public class WrappedConnection
+{
+  public static final String _rcsid = "@(#)$Id$";
+
+  protected Connection connection;
+  protected ConnectionPool owner;
+  
+  /** Constructor */
+  public WrappedConnection(ConnectionPool owner, Connection connection)
+  {
+    this.owner = owner;
+    this.connection = connection;
+  }
+  
+  /** Get the JDBC connection object.
+  */
+  public Connection getConnection()
+  {
+    return connection;
+  }
+  
+  /** Release the object into its pool.
+  */
+  public void release()
+  {
+    owner.releaseConnection(this.connection);
+    this.connection = null;
+  }
+}
+
+

Propchange: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/WrappedConnection.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/lcf/branches/CONNECTORS-96/connectors/jdbc/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/jdbc/WrappedConnection.java
------------------------------------------------------------------------------
    svn:keywords = Id



Mime
View raw message