tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Short" <dsh...@san.rr.com>
Subject RE: How to use oracle pool instead of using DBCP pool?
Date Fri, 02 Jul 2004 16:55:41 GMT
Try this.

Here's how I do it using Struts on W2K.  Modify names and paths to suit your
needs.

Upon startup, a listener servlet (ResourceManagerListener) is called (See
<listener> tag in the included web.xml source).

The listener servlet will create the connection pool based on your web.xml
parameters (See ResourceManagerListener.java).  Once started, the listener
servlet initializes an application scope variable (appDataSource), which
when called from your servlets/JSPs will return a DB DataSource object (DB
connection).

----------------------------------------------------------------------------
---------------------------

In your main servlet:

try
  {
  DataSource ds = (DataSource)
getServlet().getServletContext().getAttribute("appDataSource");
  xxxProcess = new xxxProcessBean();
  xxxInfo = new xxxInfoBean();
  xxxProcess.setDataSource(ds);
  xxxInfo = xxxProcess.getUser(userName, customerId);
  }

In your process bean:

public class xxxProcessBean implements Serializable
  {
  private DataSource dataSource;

  /**
   * Sets the dataSource property value.
   */
  public void setDataSource(DataSource dataSource)
    {
    this.dataSource = dataSource;
    }


  public xxxInfoBean getUser(String userName, String customerId)
    throws SQLException
    {
    // Get the user info from the database
    Connection conn = dataSource.getConnection();
    xxxInfoBean xxxResult = null;

    try
      {
      xxxResult = getUserName(userName, customerId, conn);  // Execute
actual SQL statement.
      }

    finally
      {
      try
        {
        conn.close();
        conn = null;
        }

    catch (SQLException e)
      {} // Ignore

    return xxxResult;

----------------------------------------------------------------------------
---------------------------

Change MachineNameHere to your machine name.
Change OracleSIDHere to your DB SID.
Change DBUserNameHere to your DB username.
Change DBPasswordHere to your DB password
Change ApplicationNameHere to a meaningful application designator.
Change xxx to your object name.

Copy Oracle's classes12.zip and nls_charset12.zip files (should live in
C:\OraHome\jdbc\lib) to C:\Tomcat\common\lib.  Depending on the version of
Tomcat, you may need to rename the .zip files to .jar.


I'll let you read/learn about Struts on your own.

Hoe this helps.

Dave

----------------------------------------------------------------------------
---------------------------
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com//dtd/web-app_2_3.dtd">

<web-app>
  <!-- Used by the JSTL I18N actions -->
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.fallbackLocale</param-name>
    <param-value>en</param-value>
  </context-param>

  <!-- Context parameters for application -->

  <!-- Oracle thin JDBC driver -->
  <!--

<param-value>jdbc:oracle:thin:@MachineNameHere:1521:OracleSIDHere</param-val
ue>
  -->
  <context-param>
    <param-name>jdbcURL</param-name>
    <!-- Oracle OCI JDBC driver -->
    <param-value>jdbc:oracle:oci8:@OracleSIDHere</param-value>
  </context-param>

  <context-param>
    <param-name>user</param-name>
    <param-value>DBUserNameHere</param-value>
  </context-param>

  <context-param>
    <param-name>password</param-name>
    <param-value>DBPasswordHere</param-value>
  </context-param>

  <context-param>
    <param-name>maxLimit</param-name>
    <param-value>50</param-value>
  </context-param>

  <!--
    will create 10 pooled connections.
  -->
  <context-param>
    <param-name>minLimit</param-name>
    <param-value>10</param-value>
  </context-param>

  <!-- Filter and listener configurations -->
  <filter>
    <filter-name>accessControl</filter-name>
    <filter-class>
      com.ApplicationNameHere.servlets.AccessControlFilter
    </filter-class>
    <init-param>
      <param-name>loginPage</param-name>
      <param-value>/jsp/login.jsp</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>accessControl</filter-name>
    <url-pattern>/protected/*</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>
      com.ApplicationNameHere.servlets.ResourceManagerListener
    </listener-class>
  </listener>

  <!-- Struts Controller servlet -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Servlet for handling both servlet and JSP errors -->
  <servlet>
    <servlet-name>errorDispatcher</servlet-name>

<servlet-class>com.ApplicationNameHere.servlets.ErrorDispatcherServlet</serv
let-class>
    <init-param>
      <param-name>errorPage</param-name>
      <param-value>/jsp/error/errorpage.jsp?debug=log</param-value>
    </init-param>
  </servlet>

  <!-- Struts Controller servlet mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>errorDispatcher</servlet-name>
    <url-pattern>/errorDispatcher</url-pattern>
  </servlet-mapping>

  <!-- Session timeout value (in minutes) -->
  <session-config>
    <session-timeout>1</session-timeout>
  </session-config>

  <!--
    Uncomment if you want all exceptions and 500 status codes to
    be handled by the customized error page.
  -->
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/errorDispatcher</location>
  </error-page>

  <error-page>
    <error-code>500</error-code>
    <location>/errorDispatcher</location>
  </error-page>

  <error-page>
    <exception-type>java.sql.SQLException</exception-type>
    <location>/errorDispatcher</location>
  </error-page>

  <resource-ref>
    <res-ref-name>jdbc/ApplicationNameHere</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

</web-app>

----------------------------------------------------------------------------
---------------------------
ResourceManagerListener.java
package com.ApplicationNameHere.servlets;

import javax.servlet.*;
import javax.servlet.http.*;

import oracle.jdbc.pool.*;

import javax.naming.*;
import javax.sql.*;

import java.sql.*;
import java.math.*;
import java.util.*;

/**
 * This class manages the DataSource resource for an application,
 * creating an Oracle DataSource with pooling capabilities
 * and makes it available when the application starts and removes it
 * when the application is shut down.
 *
 * @author David Short, Relational Concepts, Inc.
 * @version David Short     01/10/2004     initial release.
 */
public class ResourceManagerListener implements ServletContextListener
  {
  private OracleConnectionCacheImpl ds = null;
  private Context ctx = null;

  public void contextInitialized(ServletContextEvent sce)
    {
    ServletContext application  = sce.getServletContext();

    /*
     * Get the JDBC URL, user, password and limits from the web.xml
     * context init parameters
     */
    String jdbcURL  = application.getInitParameter("jdbcURL");
    String user     = application.getInitParameter("user");
    String password = application.getInitParameter("password");
    String minLimit = application.getInitParameter("minLimit");
    String maxLimit = application.getInitParameter("maxLimit");

    try
      {
      ds = new OracleConnectionCacheImpl();
      ds.setURL(jdbcURL);
      ds.setUser(user);
      ds.setPassword(password);
      ds.setMinLimit(Integer.parseInt(minLimit));
      ds.setMaxLimit(Integer.parseInt(maxLimit));
      }
    catch (Exception e)
      {
      application.log("Failed to create data source: " + e.getMessage());
      }

    /*
      Initialize the database connection pool.
    */
    try
      {
      ctx = new InitialContext();
      ctx.lookup("java:comp/env/jdbc/ApplicationNameHere");
      }
    catch (Exception e)
      {
      application.log("Failed to create database connection pool: " +
e.getMessage());
      }

    application.setAttribute("appDataSource", ds);
    }

  public void contextDestroyed(ServletContextEvent sce)
    {
    ServletContext application  = sce.getServletContext();
    application.removeAttribute("appDataSource");
    // Close the connections in the DataSource
    try
      {
      ds.close();
      }
    catch (java.sql.SQLException e)
      {}

    ds = null;
    }
  }

-----Original Message-----
From: Tim Funk [mailto:funkman@joedog.org]
Sent: Friday, July 02, 2004 8:36 AM
To: Tomcat Users List
Subject: Re: How to use oracle pool instead of using DBCP pool?


Nope. (Oracle's technical support /bulletin boards might be of more help)

-Tim

Claudio Carvalho wrote:

> Hi Tim,
>
> I'm looking for an alternative directly on the application server,
something
> like putting an "oracle-pool" jar into tomcat/common/lib directory, have
you
> heard anything like that?
>
> Claudio Carvalho.
>
> ----- Original Message -----
> From: "Tim Funk" <funkman@joedog.org>
> To: "Tomcat Users List" <tomcat-user@jakarta.apache.org>
> Sent: Friday, July 02, 2004 10:52 AM
> Subject: Re: How to use oracle pool instead of using DBCP pool?
>
>
>
>>An alternative is to look at the DBCP java-docs. Cast your Connection to a
>>DBCP's ppoled connection class (or approrpiate). That class has a method
>>called getDelegate() which returns the real connection from Oracle. Then
>
> cast
>
>>that to the appropriate Oracle class.
>>
>>-Tim
>>
>>Claudio Carvalho wrote:
>>
>>
>>>Cláudio CarvalhoHi,
>>>
>>>I'm trying to get the CLOB working in my application and I'm having
>
> problems
>
>>>with the Connection, so, does anybody knows how to use in Tomcat 5 the
>>>oracle pool instead of using the DBCP pool?
>>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message