The Data Sources File

If your application is going to access data in a database (and since you re reading a book with Oracle in its title, I m assuming yours is), you need to set up a data source before your application can speak to your database. Most likely, you will be using a Java Database Connectivity (JDBC) driver to facilitate communications between your application and the Oracle database. Depending on how you have coded your application, you will need to specify how your application will reference a data source in the data-sources.xml file (discussed shortly).

While there are an infinite number of ways to code connection routines for your application, the listing that follows demonstrates a simple way of attempting to establish a connection to an Oracle database:

 private DataSource getDataSource() throws ConnectionException { DataSource ds = null; try {        Context initContext = new InitialContext();        ds = (DataSource) initContext.lookup ("jdbc/hockeyappDS"); } catch (NamingException e) {        e.printStackTrace();        throw new ConnectionException("Cannot establish connection " + e.getMessage()); } return ds; } 

The line

 ds = (DataSource) initContext.lookup ("jdbc/hockeyappDS"); 

is searching for an entry in the data-sources.xml file with a name of jdbc/hockeyappDS . Since this particular piece of code was implemented in a bean, Oracle will search the data-sources.xml file, attempting to match an entry with the ejb-location parameter.

During creation of an OC4J container, Oracle provides you with an example data source inside a default data-sources.xml file. You can use that as a template to create other data sources with the file. The location of the data-sources.xml file is <ORACLE_MID-TIER_HOME>/j2ee/<OC4J Container Name>/config. Here is a listing of the default data-sources.xml file that comes with Oracle Application Server 10 g :

 <?xml version="1.0" standalone=yes?> <!DOCTYPE data-sources PUBLIC "Orion data-sources" "http://xmlns.oracle.com/ias/dtds/ data-sources-9_04.dtd"> <data-sources>       <!             An example/default DataSource that uses             Oracle JDBC-driver to create the connections.             This tag creates all the needed kinds             of data-sources, transactional, pooled and EJB-aware sources.             The source generally used in application code is the "EJB"             one - it provides transactional safety and connection             pooling. Oracle thin driver could be used as well,             like below.             url="jdbc:oracle:thin:@host:port:sid"       >       <data-source             class="com.evermind.sql.DriverManagerDataSource"             name="OracleDS"             location="jdbc/OracleCoreDS"             xa-location="jdbc/xa/OracleXADS"             ejb-location="jdbc/OracleDS"             connection-driver="oracle.jdbc.driver.OracleDriver"             username="scott"             password="->pwForScott"             url="jdbc:oracle:thin:@localhost:1521:oracle"             inactivity-timeout="30"       /> 

Let s say we wanted to connect to an instance named hockey on a server named avalanche as user sakic with a password of captain using the data source specified in the preceding Java code segment (jdbc/hockeyappDS). We could copy the existing data-source section in the preceding file and create a new one that looks like this:

 <data-source             class="com.evermind.sql.DriverManagerDataSource"             name="hockeyappDS"             location="jdbc/hockeyappCoreDS"             xa-location="jdbc/xa/hockeyappXADS"             ejb-location="jdbc/hockeyappDS"             connection-driver="oracle.jdbc.driver.OracleDriver"             username="sakic"             password="captain"             url="jdbc:oracle:thin:@avalanche:1521:hockey"             inactivity-timeout="30"       /> 

As mentioned earlier, since the connection code listed here was part of a bean, Oracle will look at the ejb-location parameter attempting to find a match. If the code was implemented outside of a bean, it would attempt to find a match in the location parameter. Now that our data source is configured, it is time to deploy our application.

Tip  

You can modify the container s data-sources.xml file, or you can include a new one in your EAR or WAR. That will create datasources specific to your application.



Oracle Application Server 10g Web Development
Oracle Application Server 10g Web Development (Oracle Press)
ISBN: 0072255110
EAN: 2147483647
Year: 2004
Pages: 192

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net