ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzaha_5.4.0.1/udbdatsr.htm

146 lines
10 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="concept" />
<meta name="DC.Title" content="Use DataSources with UDBDataSource" />
<meta name="abstract" content="DataSource interfaces were designed to allow additional flexibility in using Java Database Connectivity (JDBC) drivers." />
<meta name="description" content="DataSource interfaces were designed to allow additional flexibility in using Java Database Connectivity (JDBC) drivers." />
<meta name="DC.Relation" scheme="URI" content="connects.htm" />
<meta name="DC.Relation" scheme="URI" content="db2drivr.htm" />
<meta name="DC.Relation" scheme="URI" content="conprop.htm" />
<meta name="DC.Relation" scheme="URI" content="dasrprop.htm" />
<meta name="DC.Relation" scheme="URI" content="otherdts.htm" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="udbdatsr" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Use DataSources with UDBDataSource</title>
</head>
<body id="udbdatsr"><a name="udbdatsr"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Use DataSources with UDBDataSource</h1>
<div><p>DataSource interfaces were designed to allow additional flexibility
in using Java™ Database Connectivity (JDBC) drivers.</p>
<p>The use of DataSources can be split into two phases:</p>
<ul><li><strong>Deployment</strong> <p>Deployment is a setup phase that occurs before a
JDBC application actually runs. Deployment usually involves setting up a DataSource
to have specific properties and then binding it into a directory service through
the use of the Java Naming and Directory Interface (JNDI). The directory
service is most commonly the Lightweight Directory Access Protocol (LDAP),
but could be a number of others such as Common Object Request Broker Architecture
(CORBA) Object Services, Java Remote Method Invocation (RMI), or
the underlying file system.</p>
</li>
<li><strong>Use</strong><p>By decoupling the deployment from the runtime use of the
DataSource, the DataSource setup can be reused by many applications. By changing
some aspect of the deployment, all the applications that use that DataSource
automatically pick up the changes.</p>
</li>
</ul>
<div class="note"><span class="notetitle">Note:</span> Keep in mind that using RMI can be a complex undertaking. Before you
choose RMI as a solution, be sure that you understand the ramifications of
doing so. </div>
<div class="p"> An advantage of DataSources is that they allow JDBC drivers to do work
on behalf of the application without having an impact on the application development
process directly. For more information, see the following:<ul><li><a href="poolwdts.htm">Connection pooling</a></li>
<li><a href="poolstat.htm">Statement pooling</a></li>
<li><a href="distrans.htm">Distributed transactions</a></li>
</ul>
</div>
<div class="section"><h4 class="sectiontitle">UDBDataSourceBind</h4><p>The <a href="udbdatasourcebind.htm">UDBDataSourceBind</a> program
is an example of creating a UDBDataSource and getting it bound with JNDI.
This program accomplishes all the basic tasks requested. Namely, it instantiates
a UDBDataSource object, sets properties on this object, retrieves a JNDI context,
and binds the object to a name within the JNDI context.</p>
<p>The deployment
time code is vendor-specific. The application must import the specific DataSource
implementation that it wants to work with. In the import list, the package-qualified
UDBDataSource class is imported. The most unfamiliar part of this application
is the work done with <a href="jndi.htm">JNDI</a> (for example, the
retrieval of the Context object and the call to bind). For additional information,
see <a href="http://java.sun.com/products/jndi" target="_blank">JNDI</a>
by Sun Microsystems, Inc.</p>
<p>Once this program has been run and has successfully
completed, there is a new entry in a JNDI directory service called SimpleDS.
This entry is at the location specified by the JNDI context. The DataSource
implementation is now deployed. An application program can make use of this
DataSource to retrieve database connections and JDBC-related work.</p>
</div>
<div class="section"><h4 class="sectiontitle">UDBDataSourceUse</h4><p>The <a href="udbdatasourceuse.htm">UDBDataSourceUse</a> program
is an example of a JDBC application that uses the previously deployed application.</p>
<p>The
JDBC application obtains an initial context as it did before binding the UDBDataSource
in the previous example. The lookup method is then used on that context to
return an object of type DataSource for the application to use.</p>
<div class="note"><span class="notetitle">Note:</span> The
runtime application is only interested in the methods of the DataSource interface,
so there is no need for it to be aware of the implementation class. This makes
applications portable.</div>
<p>Suppose that UDBDataSourceUse is a complex
application that runs a large operation within your organization. You have
a dozen or more similar large applications within your organization. You have
to change the name of one of the systems in your network. By running a deployment
tool and changing a single UDBDataSource property, you would be able to get
this new behavior in all your applications without changing the code for them.
One of the benefits of DataSources is that they allow you to consolidate system
setup information. Another major benefit is that they allow drivers to implement
functionality invisible to the application such as connection pooling, statement
pooling and support for distributed transactions.</p>
<p>After analyzing UDBDataSourceBind
and UDBDataSourceUse closely, you may have wondered how the DataSource object
knew what to do. There is no code to specify a system, a user ID, or a password
in either of these programs. The UDBDataSource class has defaults values for
all properties; by default, it connects to the local iSeries™ server with the user profile
and password of the running application. If you wanted to ensure that the
connection was made with the user profile cujo instead, you could have accomplished
this in two ways:</p>
<ul><li>Set the user ID and password as DataSource properties. See <a href="udbdatasourcebind2.htm">Example:
Create a UDBDataSourceBind and set DataSource properties</a> on how to
use this technique.</li>
<li>Use the DataSource getConnection method that takes a user ID and password
at runtime. See <a href="udbdatasourceuse2.htm">Example: Create a UDBDataSource,
and obtain a user ID and password</a> on how to use this technique.</li>
</ul>
<p>There are a number of properties that can be specified for the UDBDataSource
as there are properties that can be specified for connections created with
the DriverManager. Refer to <a href="dasrprop.htm">DataSource properties</a> for
a list of supported properties for the native JDBC driver.</p>
<p>While these
lists are similar, it is not certain to be similar in future releases. You
are encouraged to start coding to the DataSource interface.</p>
<div class="note"><span class="notetitle">Note:</span> The native
JDBC driver also has two <a href="otherdts.htm">other DataSource implementations</a>,
but direct use of them is not recommended.</div>
<ul><li>DB2DataSource</li>
<li>DB2StdDataSource</li>
</ul>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="connects.htm" title="The Connection object represents a connection to a data source in Java Database Connectivity (JDBC). It is through Connection objects that Statement objects are created for processing SQL statements against the database. An application program can have multiple connections at one time. These Connection objects can all connect to the same database or connect to different databases.">Connections</a></div>
</div>
<div class="relconcepts"><strong>Related concepts</strong><br />
<div><a href="db2drivr.htm" title="DriverManager is a static class in the Java 2 Software Development Kit (J2SDK). DriverManager manages the set of Java Database Connectivity (JDBC) drivers that are available for an application to use.">DriverManager</a></div>
<div><a href="otherdts.htm" title="There are two implementations of the DataSource interface that are included with the native JDBC driver. These DataSource implementations should be considered deprecated. While you can still use them, they are not enhanced with future improvements; for example, robust connection and statement pooling are not added to these implementations. These implementations exist until you adopt the UDBDataSource interface and its related functions.">Other DataSource implementations</a></div>
</div>
<div class="relref"><strong>Related reference</strong><br />
<div><a href="conprop.htm" title="This table contains valid JDBC driver connection properties, their values, and their descriptions.">Connection properties</a></div>
<div><a href="dasrprop.htm" title="This table contains valid data source properties, their values, and their descriptions.">DataSource properties</a></div>
</div>
</div>
</body>
</html>