In this week's part of a multi-part series covering OC4J and J2EE applications, you will learn how to deploy J2EE applications, create data sources, and more. This article is excerpted from chapter eight of the Oracle 10g Application Server Exam Guide, written by Sam Alapati (McGraw-Hill; ISBN: 0072262710).
I’ll briefly describe how you can create data sources using the Application Server Control. Here are the steps:
Go to the OC4J home page, click the Administration tab, and then click Data Sources.
In the Application Data Sources page, click the Create button (you can also click the Create Like button if you want to pattern a new data source after an existing one).
There are several sections in the Data Source Properties page (actually titled Create Data Source), which appear next. In the General section, fill in the name and description of the data source as well as the following information:
Data source class
JDBC URL
JDBC driver
Schema
On the Job: Your default data-sources.xml file provides predefined data sources that may suit your needs.
In the Data Source Username and Password section, specify the username and password using the schema you chose in the previous section.
In the JNDI Locations section, you specify the JNDI location string that is used for JNDI tree lookups when the application is retrieving the data source. Of the three attributes—Location, Transactional (XA) Location, and EJB Location—you are required to specify only the Location attribute. If you plan on having the EJBs use the database for Container Managed Persistence (CMP), you must specify the XA and EJB Location attributes. For a non-emulated data source, you must specify all three JNDI location attributes, although only the ejb-location is actually used.
In the Connection Attributes section, you specify various connection-related attributes, such as the following:
Connection retry interval
Maximum connection attempts
Inactivity timeout period
Maximum and minimum number of open connections
In the Properties section, you can specify properties for any custom or third-party data source you may be configuring.
Once you have made the necessary configuration, click the Create button to create your new data source.
A DataSource object is used by developers to provide a logical representation of a database. It’s the job of the deployer to map this logical representation to a physical database. Using the logical DataSource object to look up the JNDI, the J2EE applications will retrieve the database names so they can connect to them. The deployer first uses the J2EE application file (EAR file), to locate the DataSource references in the deployment descriptor. The deployer will then create mappings between the database schemas matching the DataSource requirements, so the application can connect to it.
You must use an emulated data source for a single-phase commit, and you can utilize the default data source (data-sources.xml) for this purpose. Here’s the default data-sources.xml file:
Plug in your database URL information instead of the default URL shown in the data-sources.xml file. You must do this for each of the databases that are part of the transaction. The data source retrieval, as mentioned earlier, is done through a JNDI lookup. The JNDI name is shown by the ejb-location attribute.
Retrieving the Data Source Connection Your first task before using the database for queries is to retrieve a connection to the database. To retrieve the database connection, you can perform a lookup on the JNDI name bound to the data source definition. Note that you may also perform a JNDI lookup by looking up the logical name that you defined in the environment of the bean container. You retrieve the database connection using the getConnection method. Using the data source definition, for example, you retrieve a database connection as follows:
Context ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup("jdbc/OracleDS"); Connection conn = ds.getConnection();
Because the data source already contains the username and password attributes, you can use the getConnection method without the username/password attributes.