Java Reference
In-Depth Information
9.1.1
Using the
ConnectionFactory
Interface
The
resource.ConnectionFactory
interface implementation is DME-
specific, because each DME may differ in the way it establishes a
connection to the DME. To write a DME-independent application,
JDM recommends that the
ConnectionFactory
object implement the
javax.naming.Referenceable
and
javax.io.Serializable
interfaces, so that it
can be registered with a JNDI directory. The DME-specific connection
factory must be created and preregistered with the JNDI directory
such that a JDM application can look up a vendor's connection
factory object. Listing 9-1 illustrates how to get the JDM connection
factory from a JNDI registry. In this example, the
ConnectionFactory
object is registered with the name “
MyConnFactory
” under the direc-
tory “
java:comp/env/jdm
”.
Listing 9-1
Lookup ConnectionFactory object from JNDI
1. //Lookup ConnectionFactory
2. javax.datamining.resource.ConnectionFactory connFactory =
3. (ConnectionFactory) jndiContext.lookup("java:comp/env/jdm/MyConnFactory");
In practice, simple Java applications (non-J2EE) may not have a
JNDI directory in their application architecture. In those cases, the
vendor-specific
ConnectionFactory
class can be used. For example, the
Oracle implementation of JDM has the
oracle.dmt.jdm.resource.Ora-
ConnectionFactory
class that implements the JDM standard
Connec-
tionFactory
interface. Listing 9-2 illustrates the creation of the
ConnectionFactory
object when JNDI is not available. In this example,
when the
DMEVendor
attribute is
Oracle
, it uses an Oracle-specific
constructor; when it is
KXEN
, it uses a KXEN-specific constructor.
Note that only
ConnectionFactory
creation requires vendor-specific
code; the rest of a JDM application can be implemented in a vendor-
neutral approach. Chapter 13 illustrates more examples of
Connec-
tionFactory
creation in specific tool and application scenarios.
Listing 9-2
Create ConnectionFactory based on the type of the DME
1. javax.datamining.resource.ConnectionFactory connFactory = null;
2. //Based on the DME Vendor create the ConnectionFactory
3. if(DMEVendor == "Oracle")
4. connFactory = new oracle.dmt.jdm.OraConnectionFactory();
5. else if (DMEVendor == "KXEN")
6. connFactory = new com.kxen.kxJDMImpl.ConnectionFactoryImpl();
7. else if . . .
Search WWH ::
Custom Search