Re: DriverManager vs. DataSource?

From: Oscar Kind (oscar_at_danwa.net)
Date: 01/21/04

  • Next message: Noons: "Re: choices regarding where to place code - in the database or middletier"
    Date: 21 Jan 2004 09:25:09 GMT
    
    

    hank barta <hbarta@comcast.net> wrote:
    > I've been working with some code that accesses a MySQL database with
    > plans to use it in some web applications (either servlets or JSPs.) I
    > started with some old code that uses the java.sql.DriverManager
    > class to establish the connection. Looking at some code in the
    > J2EE samples, it looks like (and I quote from the documentation)
    > "The use of a DataSource object is the preferred means of connecting
    > to a data source."
    >
    > This leaves me with some questions.
    >
    > Is it even possible to use DriverManager in a servlet or JSP? I
    > started looking at the documentation because I could not figure out
    > how to get my JSP to find mysql.jar.

    Place it in the directory WEB-INF/lib. All .jar (and IIRC also .zip) files
    in that directory are automatically added to the classpath of the
    application.

    If it is a driver for all applications, you may instead choose to install
    it in the entire application server (for Tomcat 4.x by placing it in the
    directory common/lib). This has two obvious consequences:
    - That version of the driver is available to all applications.
    - Installation becomes more difficult than trivial (i.e. you have to
      copy files to two locations, often cannot use a web interface, etc.).

    > Can I use DataSource in a console app? It looks like it requires a
    > JNDI server (again, according to the docs "An object that implements
    > the DataSource interface will typically be registered with a JNDI
    > service provider.") I presume that means that I need to run the
    > servlet/JSP container at a minimum to use a data source (whether
    > for a console app or JSP.)

    Yes, by using a context provider (the JNDI server of the application
    server).

    > I presume that once I have a Connection, it doesn't matter whether
    > it came from a DriverManager or DataSource, the behavior is pretty
    > much the same. Is that correct?

    Yes. Behaviour is identical. There is however, one important difference:
    getting a connection from a datasource is significantly faster.

    > I would like to retain the capability to test my database code
    > from a console application rather than from within a JSP. Will
    > it be necessary to use different connection methods depending on
    > the environment or can I switch to the DataSource and use that in
    > both environments.

    Since you can use a datasource from a console application, you can use the
    same method for both.

    > Finally, if these are routine questions that are answered somewhere,
    > please point me to that resource!

    This is a question I leave to others, since I learned this by doing.

    Oscar

    -- 
    No trees were harmed in creating this message.
    However, a large number of electrons were terribly inconvenienced.
    

  • Next message: Noons: "Re: choices regarding where to place code - in the database or middletier"

    Relevant Pages

    • Re: DriverManager vs. DataSource?
      ... > class to establish the connection. ... it is possible to use the DriverManager interface. ... JDBC driver jars go in a 'common' directory in the container. ... > the DataSource interface will typically be registered with a JNDI ...
      (comp.lang.java.databases)
    • Re: Connection Pooling for multiple JSPs
      ... > I'm not sure how to reap the same benefits with multiple JSP pages. ... connection pool on initialization and then provides a facade to it (i.e. ... datasource that simply call the corresponding methods from the datasource). ... You probably want the method getInstance to read a properties file that ...
      (comp.lang.java.help)
    • Re: Which OAS?
      ... COTS web application front-ends and very modest custom ... applications (JSP or PL/SQL gateway). ...
      (comp.databases.oracle.server)
    • Re: newbie: sample Java applications?
      ... I am going to learn programming database ... applications with web interface (JSP, ... sample applications (called Starter Kits) which can be dowloaded from ...
      (comp.lang.java.programmer)
    • Re: newbie: sample Java applications?
      ... applications with web interface (JSP, Servlets). ... sample applications (called Starter Kits) which can be dowloaded from ...
      (comp.lang.java.programmer)