Re: DriverManager vs. DataSource?

From: Lee Fesperman (firstsql_at_ix.netcom.com)
Date: 01/21/04


Date: Wed, 21 Jan 2004 09:21:32 GMT

hank barta 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.

Yes, it is possible to use the DriverManager interface. DataSources are preferred
because they make it easy for the JSP/servlet container to provide connection pooling
and other services automatically (under the covers).

JDBC driver jars go in a 'common' directory in the container. The container docs should
specify where to put those jars so that they are accessible.

> 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.)

You can use DataSource in a 'non-managed' (outside the container server) configuration.
You will need to have the JDBC 2.0 Standard Extension jar or the J2EE jar on the
classpath (to reference javax.sql.DataSource), unless you're using JDK 1.4.x. You don't
need JNDI. You just instantiate the appropriate javax.sql.DataSource implementation, set
connection properties and get the connection (java.sql.Connection). See the javadocs on
javax.sql.DataSource and the docs for your JDBC driver for details.

> 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, that is correct for simple JSP/Servlets.

-- 
Lee Fesperman, FirstSQL, Inc. (http://www.firstsql.com)
==============================================================
* The Ultimate DBMS is here!
* FirstSQL/J Object/Relational DBMS  (http://www.firstsql.com)


Relevant Pages

  • Re: Standard way to setup JDBC connection pooling?
    ... If you want to do connection pooling without any special environment, ... The following code shows how to create a DataSource with DBCP that uses a connection-pool for the connections: ... create a pooling JDBC Driver and register it with the DriverManager: ...
    (comp.lang.java.programmer)
  • Re: Commons DBCP pooling problem
    ... connection and it's subobjects method-level objects. ... ever calls DriverManager calls, ... public static final String DB_PROPERTIES = ... public static synchronized DataSource getDataSourcethrows Exception { ...
    (microsoft.public.sqlserver.jdbcdriver)
  • Re: Accessing in a static way
    ... >So for Connection logic I wrote code to support both DataSource (to be used ... >by J2EE app) and DriverManager. ... > public class DriverManagerConnectionManager implements ConnectionManager { ...
    (comp.lang.java.programmer)
  • Re: Connection for Data Source in SSAS 2005 Tutorial Lesson 6
    ... double click on the datasource and then click on ... I noticed that my server explorer showed a connection to ... When connecting to SQL Server 2005, this failure may be caused by the ... not be made to the data source with the DataSourceID of 'Adventure ...
    (microsoft.public.sqlserver.olap)
  • Re: Strange Repadmin objects
    ... Open the ADSI Edit MMC snap-in. ... In the Connection Settings dialog box, in the Name field, enter a name for the ADSI connection. ... Right-click the object or container, click Delete, and then click Yes. ... This posting is provided "AS IS" with no warranties, ...
    (microsoft.public.windows.server.active_directory)