DataSource configuration in OC4J seems to be a confusing task for many, but once you get used to it, it is sound and simple...
There are three options to configure a DataSource in OC4J:
- Globally
- With the help of JDeveloper
- Manually per application
Configure a DataSource Globally
When you'd like to configure a DataSource globally, ie. per OC4J instance, you should use the Application Server Control. Simply go to the instance and follow this path Administration -> Services: JDBC Resources. On this page you have to create the Connection Pool and the Data Source (in that order). If you want to use this DataSource make sure that this is the only DataSource configured for your application (see below).Configure a DataSource Per Application
With the Help of JDeveloper
If you're developing with JDeveloper you're quite safe. JDeveloper automagically configures the application settings based on the configured database connections. This works very smooth but sometimes it is not exactly what you want.Manually
This is the most advanced solution and requires some understanding of the files used. In the META-INF directory of your application archive you have to provide the following three files.META-INF/data-sources.xml
This file contains the actual configuration of the DataSource you want to use in your application. It looks like this one:<?xml version='1.0'?>
<data-sources
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/data-sources-10_1.xsd">
<connection-pool name="pool-demo">
<connection-factory
factory-class="oracle.jdbc.pool.OracleDataSource"
user="demo" password="->DBDemo"
url="jdbc:oracle:thin:@demo:1521:XE"/>
</connection-pool>
<managed-data-source name="managed-demo"
jndi-name="jdbc/demoDS"
connection-pool-name="pool-demo"/>
</data-sources>
META-INF/jazn-data.xml
This file contains the username/password information for the application and can include the DataSource username/password.<?xml version='1.0' standalone='yes'?>
<jazn-data
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/jazn-data-10_0.xsd"
filepath="" OC4J_INSTANCE_ID="">
<jazn-realm>
<realm>
<name>jazn.com</name>
<users>
<user>
<name>DBDemo</name>
<credentials>!demo</credentials>
</user>
</users>
<roles/>
</realm>
</jazn-realm>
</jazn-data>
In the credentials tag we provide the real password in clear text prefixed with a '!'. This notation tells OC4J to encrypt the content of the credentials tag during deployment. Be aware that this will not be done within the original file contents in j2ee/<instancename>/applications. To be on the very save side, deploy it once, get the content of the credentials tag from j2ee/<instancename>/application-deployments/<application-name>/jazn-data.xml, and copy it into the jazn-data.xml you include in your EAR file.
META-INF/orion-application.xml
This file is the counterpart of the J2EE application.xml and contains OC4J-specific settings. Understand it as the wrapper for all the above. Here is a sample:<?xml version='1.0'?>
<orion-application
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://xmlns.oracle.com/oracleas/schema/orion-application-10_0.xsd"
default-data-source="jdbc/demoDS">
<data-sources path="./data-sources.xml"/>
<jazn location="./jazn-data.xml" provider="XML"/>
</orion-application>
Don't forget to tell JDeveloper not to update your data-sources.xml and jazn-data.xml. The two areas you should take care of are:
- Turning the automatic inclusion of the data-sources.xml in your application off (Tools->Preferences->Deployment->Uncheck "Bundle Default data-sources.xml During Deployment").
- Turning the content update of your own data-sources.xml (Right click on your data-sources.xml file, select Properties and tick off all check boxes).