struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "David Bolsover" <da...@bolsover.com>
Subject RE: DB connection pool
Date Wed, 28 Nov 2001 17:22:43 GMT
Hi

The solution I use is to use a WEB.xlm configured servlet to establish a
connection pool as follows:

-----snip-----
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);  //Note init Pool object
        }
        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();
        }
    }
-----snip-----

I then use a Pool object to get connections to the database

-----snip-----
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();
  }
}
-----snip-----

Then in my Action methods:

-----snip-----
Pool pool = Pool.getInstance();
  Connection con = null;
  try {
    con = pool.getConnection();
    UserDAO ud = new UserDAO(con); // or any DAO you need
-----snip-----

This way the Action methods know nothing of the servlet or the database.

I see no reason why you could not have multiple (slightly different)
DataAccess servlets for each of the required database connections.

David Bolsover
T:+44(0)1614276920
F:+44(0)1614276922
M:07773428118
E:david@bolsover.com

> -----Original Message-----
> From: Arnaud Héritier [mailto:aheritier@sopragroup.com]
> Sent: 28 November 2001 14:59
> To: 'Struts Users Mailing List'
> Subject: RE: DB connection pool
>
>
> Yes I understand and It is what I done in my application.
>
> I retreive datasources from actions and passes it to my DAO.
>
> This response was for Tom who asked how to declare several data-sources
>
> > -----Message d'origine-----
> > De:	Otavio C. Decio [SMTP:decio@mindspring.com]
> > Date:	mercredi 28 novembre 2001 15:32
> > À:	Struts Users Mailing List
> > Objet:	Re: DB connection pool
> >
> > Yes, I know, but I don't want my DAO to know anything about
> Actions. I like
> > Tom Lister's idea, to simply pass the datasorce to the DAO.
> >
> >  Thanks!
> >
> > Otavio
> > ----- Original Message -----
> > From: "Arnaud Héritier" <aheritier@sopragroup.com>
> > To: "'Struts Users Mailing List'" <struts-user@jakarta.apache.org>
> > Sent: Wednesday, November 28, 2001 9:36 AM
> > Subject: RE: DB connection pool
> >
> >
> > In your struts-config.xml you declare all your connections pool :
> >
> > <data-sources>
> > <data-source>
> > <set-property property="key" value="db1"/>
> > ...
> > </data-source>
> > <data-source>
> > <set-property property="key" value="db2"/>
> > ...
> > </data-source>
> > <data-source>
> > <set-property property="key" value="db3"/>
> > ...
> > </data-source>
> > </data-sources>
> >
> > Then in your Action classes you retreive your connections like this :
> >
> > DataSource myDataSource1 = servlet.findDataSource("db1");
> > Connection myConnection1 = myDataSource1.getConnection();
> >
> > DataSource myDataSource2 = servlet.findDataSource("db2");
> > Connection myConnection2 = myDataSource2.getConnection();
> >
> > Arno
> >
> > > -----Message d'origine-----
> > > De: Tom Lister [SMTP:Tom.Lister@ABSL.COM]
> > > Date: mercredi 28 novembre 2001 15:14
> > > À: 'Struts Users Mailing List'
> > > Objet: RE: DB connection pool
> > >
> > > Does anyone know how to work with multiple connection pools
> to different
> > > databases.
> > > :-)
> > > Regards
> > > Tom_Lister@absl.com <mailto:Tom_Lister@absl.com>
> > > Advanced Banking Solutions Limited
> > > +44 (0)20 7448 9146
> > > -----Original Message-----
> > > From: Viet Kevin [SMTP:vietk@activia.net]
> > > Sent: 28 November 2001 13:43
> > > To: Struts Users Mailing List
> > > Subject: Re: DB connection pool
> > >
> > >
> > > //take the DataSource from the servlet context
> > >
> > > DataSource datasource =
> > > getServlet().getServletContext(Action.DATASOURCE_KEY);
> > >
> > > // and the connection
> > > Connection connection = datasource.getConnection();
> > >
> > > Don't forget to configure your database int  the struts-config.xml
> > >
> > > Ciao
> > >
> > >
> > >
> > > =============================================================
> > > -- KeV --
> > > =============================================================
> > >
> > >
> > >
> > > --
> > > To unsubscribe, e-mail:
> > > <mailto:struts-user-unsubscribe@jakarta.apache.org>
> > > For additional commands, e-mail:
> > > <mailto:struts-user-help@jakarta.apache.org>
> > >
> > > --
> > > To unsubscribe, e-mail:
> > <mailto:struts-user-unsubscribe@jakarta.apache.org>
> > > For additional commands, e-mail:
> > <mailto:struts-user-help@jakarta.apache.org>
> >
> >
> > --
> > To unsubscribe, e-mail:
> > <mailto:struts-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> > <mailto:struts-user-help@jakarta.apache.org>
> >
> >
> >
> > --
> > To unsubscribe, e-mail:
> <mailto:struts-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> <mailto:struts-user-help@jakarta.apache.org>
>
>
> --
> To unsubscribe, e-mail:
> <mailto:struts-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:struts-user-help@jakarta.apache.org>
>
>
>


--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message