It is always possible to load a 'DBServlet' throuh the web.xml

somthing like:

Comments welcome!!!

db

<servlet>
    <servlet-name>database</servlet-name>
    <servlet-class>com.nomogen.demosite.db.DataAccess</servlet-class>
    <init-param>
      <param-name>password</param-name>
      <param-value>secret</param-value>
    </init-param>
    <init-param>
      <param-name>maxCount</param-name>
      <param-value>10</param-value>
    </init-param>
    <init-param>
      <param-name>jdbcURL</param-name>
      <param-value>jdbc:mysql://localhost/nomogen</param-value>
    </init-param>
    <init-param>
      <param-name>user</param-name>
      <param-value>admin</param-value>
    </init-param>
    <init-param>
      <param-name>description</param-name>
      <param-value>nomogen</param-value>
    </init-param>
    <init-param>
      <param-name>minCount</param-name>
      <param-value>5</param-value>
    </init-param>
    <init-param>
      <param-name>driverClass</param-name>
      <param-value>org.gjt.mm.mysql.Driver</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>


package  com.nomogen.demosite.db;


import  javax.servlet.http.HttpServlet;
import  java.sql.*;
import  javax.servlet.ServletException;
import  org.apache.struts.util.GenericDataSource;
import  java.util.*;


public class DataAccess extends HttpServlet {
    private GenericDataSource dataSource = null;
    private String description;

    
    public void init() throws ServletException {
        dataSource = new GenericDataSource();
        dataSource.setAutoCommit(true);
        dataSource.setDescription(getInitParameter("description"));
        dataSource.setDriverClass(getInitParameter("driverClass"));
        dataSource.setMaxCount(Integer.parseInt(getInitParameter("maxCount")));
        dataSource.setMinCount(Integer.parseInt(getInitParameter("minCount")));
        dataSource.setPassword(getInitParameter("password"));
        dataSource.setUrl(getInitParameter("jdbcURL"));
        dataSource.setUser(getInitParameter("user"));
        description = getInitParameter("description");
        try {
            dataSource.open();
            getServletContext().setAttribute("dataSource", dataSource);
            System.out.println(description + " Connection Pool Open");
            Pool.init(dataSource);
        }
        catch (Exception ex) {
            ex.printStackTrace();
            throw  new ServletException("Unable to open datasource");
        }
    }

   
    public void destroy() {
        try {
            dataSource.close();
            System.out.println(description + " Connection Pool Closed");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

package  com.nomogen.demosite.db;

import  java.sql.SQLException;
import  java.sql.Connection;
import  javax.sql.DataSource;


public class Pool {
  private DataSource ds;
  private static Pool mySelf;

 
  public Pool (DataSource ds) {
    this.ds = ds;
  }

 
  public static void init (DataSource ds) {
    mySelf = new Pool(ds);
  }

 
  public static Pool getInstance () {
    if (mySelf == null) {
      throw  new IllegalStateException("Pool not initialized");
    }
    return  mySelf;
  }

 
  public Connection getConnection () throws SQLException {
    return  ds.getConnection();
  }
}

> -----Original Message-----
> From: Bradley G Smith [mailto:[EMAIL PROTECTED]]
> Sent: 21 January 2003 19:17
> To: Struts Users Mailing List
> Subject: RE: db connection pool question
> 
> 
> 
> I believe you can define data sources in server.xml for tomcat at least,
> and make them available for multiple applications. But I have not done that
> yet (using Oracle connection pool classes).
> 
> Brad
> 
> 
> 
>                                                                                      
>                                
>                       david chan                                                     
>                                
>                       <david_tomcat@ya         To:      Struts Users Mailing List 
><[EMAIL PROTECTED]>   
>                       hoo.com>                 cc:                                   
>                                
>                                                Subject: RE: db connection pool 
>question                              
>                       21-01-03 09:43                                                 
>                                
                      Please respond                                                   
                              
>                       to "Struts Users                                               
>                                
>                       Mailing List"                                                  
>                                
>                                                                                      
>                                
>                                                                                      
>                                
> 
> 
> 
> 
> With the use data-sources in Struts-config.xml, How
> can I get the data-source in my initServlet that will
> be loaded during startup of tomcat (or websphere)? My
> initServlet will do one time loading of some data from
> Oracle database and store them into application scope
> and shared by different session. There are a couple of
> things need to be sure:
> 
> 1) struts datasource should be setup first (does
> setting servlet loading sequence in web.xml, i.e.
> ActionServlet will load first, then is my initServlet
> etc...)
> 2) how to get the datasource from initServlet?
> 3) In general, I would like to ask what's the better
> pratice to do web application initialization when
> using Struts framework. i.e. init datasource, log4j
> etc... So that I don't need to customize config for
> tomcat, webshpere etc..
> 
> (My current solution doesn't use data-source within
> Struts-config.xml, and I don't like that because I
> need to do some config in tomcat's server.xml which
> make it depends on app. server)
> 
> Thanks.
> David
> 
> --- Bradley G Smith <[EMAIL PROTECTED]> wrote:
> >
> > If the Oracle class has the getter and setter
> > methods, then yes, you can
> > set these properties. Struts is very nice about
> > conforming to JavaBean
> > standards. I seem to recall that there were a couple
> > of properties in the
> > Oracle class that did not conform to JavaBean
> > standards, but most did.
> >
> > Brad
> >
> >
> >
> >
> >
> >
> >                       david chan
> >
> >
> >                       <david_tomcat@ya         To:
> >    Struts Users Mailing List
> > <[EMAIL PROTECTED]>
> >                       hoo.com>                 cc:
> >
> >
> >
> > Subject: RE: db connection pool question
> >
> >                       21-01-03 09:13
> >
> >
> >                       Please respond
> >
> >
> >                       to "Struts Users
> >
> >
> >                       Mailing List"
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > Can I configure the MAX_LIMIT, MIN_LIMIT or
> > maxActive,
> > maxIdle property for Oracle datasource?
> > I look at the oracle817 JDBC API docs, there is only
> > one class "OracleOCIConnectionPool" with some
> > properties like these but not sure I can set them
> > within the data-sources element of my
> > Struts-config.xml?
> >
> > David
> >
> > --- "Raible, Matt" <[EMAIL PROTECTED]>
> > wrote:
> > > I'm assuming this will work for configuring a
> > > context in Tomcat as well?
> > >
> > >
> >
> http://www.mail-archive.com/[email protected]/msg51842.html
> > >
> > > Thanks,
> > >
> > > Matt
> > >
> > >
> > > > -----Original Message-----
> > > > From: Bradley G Smith
> > [mailto:[EMAIL PROTECTED]]
> > > > Sent: Tuesday, January 21, 2003 9:29 AM
> > > > To: Struts Users Mailing List
> > > > Cc: david chan; Struts Users Mailing List
> > > > Subject: Re: db connection pool question
> > > >
> > > >
> > > >
> > > > "select 1 from dual" is the standard way to
> > "ping"
> > > oracle for a valid
> > > > connection. I would also recommend using
> > Oracle's
> > > connection
> > > > pool. It will
> > > > do this in the background. We have several
> > tomcat
> > > and struts
> > > > applications
> > > > running for weeks without problems. Even with
> > > Oracle going
> > > > off line for
> > > > maintenance.
> > > >
> > > > I can supply and example struts configuration
> > > using Oracle's driver
> > > > connection pool driver. Or check the archives
> > for
> > > this
> > > > mailing list, as I
> > > > have posted it here. Search using my name or
> > > Oracle.
> > > >
> > > > Brad
> > > >
> > > >
> > > >
> > > >
> > >
> > > >
> > >
> > > >                       Rick Reumann
> > >
> > > >
> > >
> > > >                       <maillist@reuman
> > To:
> > >
> > > > "Struts Users Mailing List"
> > > <[EMAIL PROTECTED]>
> > > >                       n.net>
> > cc:
> > >      david
> > > > chan <[EMAIL PROTECTED]>
> > >
> > > >
> > > Subject: Re:
> > > > db connection pool question
> > >
> > > >                       21-01-03 07:19
> > >
> > > >
> > >
> > > >                       Please respond
> > >
> > > >
> > >
> > > >                       to "Struts Users
> > >
> > > >
> > >
> > > >                       Mailing List"
> > >
> > > >
> > >
> > > >
> > >
> > > >
> > >
> > > >
> > >
> > > >
> > >
> > > >
> > > >
> > > >
> > > >
> > > > On Tuesday, January 21, 2003, 9:56:09 AM, david
> > > wrote:
> > > >
> > > > dc> Hi,  I am using a connection pool from
> > > > dc>
> > org.apache.commons.dbcp.BasicDataSourceFactory
> > > and the driver is
> > > > dc> oracle.jdbc.driver.OracleDriver. It works
> > > great until if the
> > > > dc> server idle for a few days, then the
> > > connection object seems
> > > > dc> broken with this error: ==== begin error
> > mesg
> > > ===
> > > > dc> java.sql.SQLException: Io exception:
> > Software
> > > caused connection
> > > > dc> abort: socket wr ite error   at
> > > >
> > > > I believe I had similar problems here as well
> > but
> > > it usually only
> > > > happened when the DBA would for some reason
> > decide
> > > to kill connections
> > > > that were open. I went with configuring the
> > > pooling with whatever
> > > > comes packaged with Tomcat and configuring it in
> > > the server.xml
> > > > making sure to add the extra overhead of
> > > performing a validation query
> > > >
> > > > <parameter>
> > > >            <name>validationQuery</name>
> > > >            <value>SELECT 'CRAP' FROM
> > DUAL</value>
> > > > </parameter>
> >
> === message truncated ===
> 
> 
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
> http://mailplus.yahoo.com
> 
> --
> To unsubscribe, e-mail:   <
> mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <
> mailto:[EMAIL PROTECTED]>
> 
> 
> 
> 
> 
> 
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> 
> 

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to