allow the underlying database implementation to use a JCA DataSource loaded
via JNDI, if configured that way
This commit is contained in:
		
							parent
							
								
									2966ab703a
								
							
						
					
					
						commit
						9e5d4f9f57
					
				@ -33,6 +33,10 @@
 | 
			
		||||
 | 
			
		||||
  <!-- This section is used to configure the database pool system. -->
 | 
			
		||||
  <database>
 | 
			
		||||
    <!-- If this is specified, Venice looks for a JCA DataSource with this name and delegates all connection
 | 
			
		||||
         pooling work to it.  All the other paramerers are ignored. -->
 | 
			
		||||
    <!-- <jca-datasource>VeniceDB</jca-datasource> -->
 | 
			
		||||
 | 
			
		||||
    <!-- The fully-qualified Java classname of the JDBC driver we wish to load. -->
 | 
			
		||||
    <driver>org.gjt.mm.mysql.Driver</driver>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -9,9 +9,9 @@
 | 
			
		||||
 * 
 | 
			
		||||
 * The Original Code is the Venice Web Communities System.
 | 
			
		||||
 * 
 | 
			
		||||
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 | 
			
		||||
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
 | 
			
		||||
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 | 
			
		||||
 * Copyright (C) 2001-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 | 
			
		||||
 * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 | 
			
		||||
 * 
 | 
			
		||||
 * Contributor(s): 
 | 
			
		||||
 */
 | 
			
		||||
@ -19,6 +19,8 @@ package com.silverwrist.venice.db;
 | 
			
		||||
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.sql.*;
 | 
			
		||||
import javax.naming.*;
 | 
			
		||||
import javax.sql.*;
 | 
			
		||||
import org.apache.log4j.*;
 | 
			
		||||
import org.w3c.dom.*;
 | 
			
		||||
import com.silverwrist.util.DOMElementHelper;
 | 
			
		||||
@ -48,6 +50,7 @@ public class DataPool implements Runnable
 | 
			
		||||
   *--------------------------------------------------------------------------------
 | 
			
		||||
   */
 | 
			
		||||
 | 
			
		||||
  private DataSource m_dsource = null; // JCA DataSource (overrides everything else)
 | 
			
		||||
  private String driver;               // name of JDBC driver class
 | 
			
		||||
  private String url;                  // URL for JDBC connection
 | 
			
		||||
  private String username;             // username for JDBC connection
 | 
			
		||||
@ -83,6 +86,33 @@ public class DataPool implements Runnable
 | 
			
		||||
 | 
			
		||||
    // Use a DOMElementHelper to read off the configuration for the DataPool.
 | 
			
		||||
    DOMElementHelper cfgx = new DOMElementHelper(cfg);
 | 
			
		||||
    String dsname = cfgx.getSubElementText("jca-datasource");
 | 
			
		||||
    if (dsname!=null)
 | 
			
		||||
    { // they specified a JCA DataSource name - try to bind to it
 | 
			
		||||
      if (logger.isDebugEnabled())
 | 
			
		||||
	logger.debug("DataPool JCA data source: " + dsname);
 | 
			
		||||
      try
 | 
			
		||||
      { // get the datasource
 | 
			
		||||
	Context ctx = new InitialContext();
 | 
			
		||||
	m_dsource = (DataSource)(ctx.lookup("java:/" + dsname));
 | 
			
		||||
	if (m_dsource!=null)
 | 
			
		||||
	  return;  // initialization is effectively done
 | 
			
		||||
	else
 | 
			
		||||
	  logger.warn("No data source found with name \"" + dsname + "\"; trying regular initialization");
 | 
			
		||||
 | 
			
		||||
      } // end try
 | 
			
		||||
      catch (NamingException e)
 | 
			
		||||
      { // output an error message
 | 
			
		||||
	logger.warn("Error getting data source \"" + dsname + "\"; trying regular initialization",e);
 | 
			
		||||
	m_dsource = null;
 | 
			
		||||
 | 
			
		||||
      } // end catch
 | 
			
		||||
 | 
			
		||||
    } // end if
 | 
			
		||||
    else
 | 
			
		||||
      logger.info("no JCA data source name configured - init the old way");
 | 
			
		||||
 | 
			
		||||
    // proceed with regular initialization
 | 
			
		||||
    driver = cfgx.getSubElementText("driver");
 | 
			
		||||
    if (driver==null)
 | 
			
		||||
    { // no driver found!
 | 
			
		||||
@ -152,7 +182,7 @@ public class DataPool implements Runnable
 | 
			
		||||
    } // end catch
 | 
			
		||||
 | 
			
		||||
    if (logger.isDebugEnabled())
 | 
			
		||||
      logger.debug("DataPool initial connections: " + String.valueOf(initial_conns));
 | 
			
		||||
      logger.debug("DataPool initial connections: " + initial_conns);
 | 
			
		||||
 | 
			
		||||
    try
 | 
			
		||||
    { // retrieve the max-conns figure and make it an integer
 | 
			
		||||
@ -181,7 +211,7 @@ public class DataPool implements Runnable
 | 
			
		||||
    } // end catch
 | 
			
		||||
 | 
			
		||||
    if (logger.isDebugEnabled())
 | 
			
		||||
      logger.debug("DataPool maximum connections: " + String.valueOf(max_conns));
 | 
			
		||||
      logger.debug("DataPool maximum connections: " + max_conns);
 | 
			
		||||
 | 
			
		||||
    wait_if_busy = cfgx.hasChildElement("wait-if-busy");
 | 
			
		||||
    if (logger.isDebugEnabled())
 | 
			
		||||
@ -295,7 +325,7 @@ public class DataPool implements Runnable
 | 
			
		||||
  private final static void closeConnections(Vector vconn)
 | 
			
		||||
  {
 | 
			
		||||
    if (logger.isDebugEnabled())
 | 
			
		||||
      logger.debug("closeConnections(" + String.valueOf(vconn.size()) + " to be closed)");
 | 
			
		||||
      logger.debug("closeConnections(" + vconn.size() + " to be closed)");
 | 
			
		||||
 | 
			
		||||
    for (int i=0; i<vconn.size(); i++)
 | 
			
		||||
    { // loop over the entire vector
 | 
			
		||||
@ -366,9 +396,11 @@ public class DataPool implements Runnable
 | 
			
		||||
   */
 | 
			
		||||
  public synchronized int numConnections()
 | 
			
		||||
  {
 | 
			
		||||
    if (m_dsource!=null)
 | 
			
		||||
      return 1;
 | 
			
		||||
    int rc = avail_connections.size() + busy_connections.size();
 | 
			
		||||
    if (logger.isDebugEnabled())
 | 
			
		||||
      logger.debug("numConnections() => " + String.valueOf(rc));
 | 
			
		||||
      logger.debug("numConnections() => " + rc);
 | 
			
		||||
    return rc;
 | 
			
		||||
 | 
			
		||||
  } // end numConnections
 | 
			
		||||
@ -383,6 +415,9 @@ public class DataPool implements Runnable
 | 
			
		||||
   */
 | 
			
		||||
  public synchronized Connection getConnection() throws SQLException
 | 
			
		||||
  {
 | 
			
		||||
    if (m_dsource!=null)
 | 
			
		||||
      return m_dsource.getConnection();
 | 
			
		||||
 | 
			
		||||
    for (;;)
 | 
			
		||||
    { // loop until we get a connection or throw an exception
 | 
			
		||||
      if (avail_connections.isEmpty())
 | 
			
		||||
@ -393,7 +428,7 @@ public class DataPool implements Runnable
 | 
			
		||||
	  makeBackgroundConnection();  // try to create a new connection
 | 
			
		||||
	else if (!wait_if_busy)
 | 
			
		||||
	{ // don't want to wait?  tough, we're h0sed!
 | 
			
		||||
	  logger.error("exhausted maximum connection limit (" + String.valueOf(max_conns) + ")");
 | 
			
		||||
	  logger.error("exhausted maximum connection limit (" + max_conns + ")");
 | 
			
		||||
	  throw new SQLException("connection limit reached");
 | 
			
		||||
 | 
			
		||||
	} // end else if
 | 
			
		||||
@ -447,6 +482,23 @@ public class DataPool implements Runnable
 | 
			
		||||
   */
 | 
			
		||||
  synchronized void releaseConnection(Connection c)
 | 
			
		||||
  {
 | 
			
		||||
    if (m_dsource!=null)
 | 
			
		||||
    { // just close the connection
 | 
			
		||||
      try
 | 
			
		||||
      { // close the connection
 | 
			
		||||
	c.close();
 | 
			
		||||
 | 
			
		||||
      } // end try
 | 
			
		||||
      catch (SQLException e)
 | 
			
		||||
      { // log exception and continue
 | 
			
		||||
	logger.error("c.close() exception",e);
 | 
			
		||||
 | 
			
		||||
      } // end catch
 | 
			
		||||
 | 
			
		||||
      return;
 | 
			
		||||
 | 
			
		||||
    } // end if
 | 
			
		||||
 | 
			
		||||
    if (c!=null)
 | 
			
		||||
    { // can release this connection
 | 
			
		||||
      if (c instanceof WrappedConnection)
 | 
			
		||||
@ -482,6 +534,8 @@ public class DataPool implements Runnable
 | 
			
		||||
   */
 | 
			
		||||
  public synchronized void closeAllConnections()
 | 
			
		||||
  {
 | 
			
		||||
    if (m_dsource!=null)
 | 
			
		||||
      return;
 | 
			
		||||
    if (logger.isDebugEnabled())
 | 
			
		||||
      logger.debug("closing ALL connections!");
 | 
			
		||||
    closeConnections(avail_connections);
 | 
			
		||||
@ -498,6 +552,8 @@ public class DataPool implements Runnable
 | 
			
		||||
   */
 | 
			
		||||
  public synchronized String toString()
 | 
			
		||||
  {
 | 
			
		||||
    if (m_dsource!=null)
 | 
			
		||||
      return "DataPool " + m_dsource.toString();
 | 
			
		||||
    StringBuffer info = new StringBuffer();
 | 
			
		||||
    info.append("DataPool(\"").append(url).append("\",\"").append(username).append("\"), ");
 | 
			
		||||
    info.append(avail_connections.size()).append(" avail, ").append(busy_connections.size());
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user