Contents


Configure DB2 UDB with third-party application servers

Comments

Create a JDBC connection from a WebLogic or JBoss application server or on the JDeveloper IDE by configuring the application server/IDE for the database, and use this connection to the DB2 database in J2EE applications developed using application servers. This article shows you how to configure a JDBC connection from the WebLogic and JBoss servers, and explains the JDeveloper IDE.

The DB2 JDBC Type 4 Driver is used to configure a JDBC connection with DB2 database. The Type 4 JDBC driver (thin driver) has an advantage over the Type 2 Driver because the type 4 driver provides direct access to the database.

JBoss, an open source application server, by default is configured with the Hypersonic database. So, to use it with DB2 UDB, you need to configure DB2 for the JBoss application server. WebLogic and JDeveloper do not, however, use a default database configuration.

Preliminary setup

To get started, install the application servers and IDE with which you will configure a DB2 database connection. Install the WebLogic 8.1 application server from BEA and the JBoss 4.0 application server. Add <JBoss>/bin to the Path variable (<JBoss> is the directory in which JBoss 4.0 is installed). Next, install JDeveloper 10g from Oracle. Finally, install DB2 UDB, create an instance of the DB2 database (name it DB2). In this tutorial the DB2 UDB v8.2 Personal Developer's Edition is the DB2 database server. The DB2 JDBC Type 4 driver db2jcc.jar file and the DB2 database server license file db2jcc_license_cu.jar are available in the C:/Program Files/IBM/SQLLIB/java directory.

Configuring DB2 UDB with WebLogic

BEA WebLogic Server 8.1 is an application server for developing, integrating, securing, and managing distributed service-oriented applications. This section explains how to configure the WebLogic 8.1 server with the DB2 database. You should configure a connection pool, which is a set of connections with a database, in the WebLogic server with the DB2 database. Client/Server applications obtain a connection from the connection pool with a DataSource containing a JNDI binding. The DB2 driver classes are required to configure a connection with the DB2 database. Add db2jcc.jar file and db2jcc_license_cu.jar file to the Classpath variable in the <BEA>\user_projects\domains\mydomain\startWebLogic script. (<BEA> is the directory in which WebLogic 8.1 is installed.) Now, start the WebLogic server and access the WebLogic server Administration Console with the URL http://localhost:7001/console.

The WebLogic server Administration Console lets you configure a JDBC connection with the DB2 database (see Figure 1). Select the Services>JDBC node in the navigation frame, right-click on the Connection Pools node, and select Configure a New JDBC Connection Pool.

Figure 1. Configure a New JDBC Connection Pool
Configure a new JDBC Connection Pool
Configure a new JDBC Connection Pool

In the Configure a JDBC Connection Pool frame, go to the Database Type list and select DB2. Next, select Other from the Database Driver field. The JDBC connection with the DB2 database in this section is configured with the DB2 Type 4 driver, but you can use any of the other drivers listed. You should specify the driver used in the Classpath variable of the startWebLogic script. See Figure 2.

Figure 2. Database driver
Database driver
Database driver

Next, specify the driver class name, url, database user name, and password in the Connection Properties frame. Set the Driver Class Name to com.ibm.db2.jcc.DB2Driver, the DB2 JDBC Type 4 driver. Set the URL to jdbc:db2://localhost:50000/DB2. The format of the DB2 JDBC Type 4 driver is jdbc:db2://<server>:<port>/<database>.

  • <server> is the DB2 database server
  • <port> is the DB2 database server port
  • <database> is the DB2 database

Click on the Test Driver Configuration button to test the configuration of the DB2 driver with the DB2 database. If the connection with the DB2 database is established, you will see a Connection successful message. If more than one server is configured in a domain, select a server to which the connection pool should be deployed. Click on the Create and Deploy button to create and deploy the connection pool (see Figure 3).

Figure 3. Create and deploy
Create and deploy
Create and deploy

Now a connection pool is configured, and a node for the connection pool is added to the navigation frame. You can modify the connection pool settings by selecting the connection node in the navigation frame (see Figure 4). Some connection pool settings include initial capacity and maximum capacity: Initial Capacity is the initial number of connections in the connection pool; MaxCapacity is the maximum number of connections in the database.

Figure 4. Connection pool node
Connection pool node
Connection pool node

You also can configure a Data Source with the connection pool. A data source is a Java Naming and Directory Interface (JNDI) object used to provide access to a connection pool. To configure a data source, right-click on the JDBC>Data Sources node in the Administration Console and select Configure a new JDBCTxData Source. In the Configuration frame displayed, specify a JNDI name for the data source. In the Connect To Connection Pool frame, select a JDBC connection pool configured with the DB2 database. The data source then is connected to the selected connection pool. Next, in the Target the Data Source frame, select a target server for the data source. The data source then is deployed on the selected server, and a node for the data source is added to the Administration Console JDBC>Data Sources node. Now you have successfully configured your DB2 database with WebLogic.

Configuring DB2 UDB with JBoss

JBoss is an open source application server. To configure it for a DB2 database, first you must add the DB2 database driver classes to the CLASSPATH by copying the db2jcc.jar file and the db2jcc_license_cu.jar file to the <JBoss>/server/default/lib directory. (<JBoss> is the directory in which JBoss 4.0 server is installed.) To configure the JBoss 4.0 application server with the DB2 data source, copy the <JBoss>/docs/examples/jca/db2-ds.xml data source file to the <JBoss>/server/default/deploy directory. Modify the db2-ds.xml configuration file by setting <driver-class/> to com.ibm.db2.jcc.DB2Driver and <connection-url/> to jdbc:db2://localhost:50000/DB2 . Next, specify user-name and password in db2-ds.xml. The min-pool-size element specifies the minimum number of connections in the connection pool. See Listing 1.

Listing 1. db2-ds.xml
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>DB2DS</jndi-name>
<connection-url>jdbc:db2://localhost:50000/DB2 </connection-url>
<driver-class>com.ibm.db2.jcc.DB2Driver</driver-class>
<user-name></user-name>
<password></password>
<min-pool-size>5</min-pool-size>
<metadata>
<type-mapping>DB2</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

Next, modify the <JBoss>\server\default\conf\standardjaws.xml (or jaws.xml) configuration file to set <datasource> and <type-mapping>. The standardjaws.xml/jaws.xml is required to be modified only for CMP 1.1 entity Enterprise JavaBeans (EJBs). Now, set the type-mapping element value to DB2, which is the type mapping name for your DB2 database. See Listing 2.

Listing 2. standardjaws.xml
<jaws> 
<datasource>java:/DB2DS</datasource>
<type-mapping>DB2</type-mapping>
</jaws>

Modify the <JBoss>\server\default\confstandardjbosscmp-jdbc.xml (or jbosscmp-jdbc.xml) configuration file to set <datasource> and <datasource-mapping>. The datasource element specifies the data source JNDI name, java:/DB2DS, for the JDBC configuration with the DB2 database. Set the datasource-mapping value to the type mapping for the DB2 database; the type mappings are predefined in the standardjbosscmp-jdbc.xml/jbosscmp-jdbc.xml configuration file (see Listing 3). For the DB2 database, the type mapping name is DB2.

Listing 3. standardjbosscmp-jdbc.xml
<jbosscmp-jdbc>  
<defaults>
<datasource>java:/DB2DS</datasource>
<datasource-mapping>DB2</datasource-mapping>
</defaults>
</jbosscmp-jdbc>

The application-policy DB2DbRealm is required to log in to the DB2 database from the JBoss server. Add this <application-policy/> element to <JBoss>\server\default\conf\login-config.xml (see Listing 4). Now you have configured your DB2 database with JBoss.

Listing 4. login-config.xml
<application-policy name = "DB2DbRealm"> 
<authentication>
<login-module code ="org.jboss.resource.security.ConfiguredIdentityLoginModule"
flag = "required">
<module-option name = "principal"></module-option>
<module-option name = "userName"></module-option>
<module-option name = "password"></module-option>
<module-option name ="managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=DB2DS
</module-option>
</login-module>
</authentication>
</application-policy>

Configuring DB2 UDB with JDeveloper

Oracle JDeveloper is an integrated development environment for modeling, developing, debugging, optimizing, and deploying Java applications and Web services. This section explains how to configure JDeveloper 10g with a DB2 database. First, start JDeveloper and examine its console (see Figure 5).

Figure 5. JDeveloper 10g console
JDeveloper 10g console
JDeveloper 10g console

The DB2 driver classes are required in the Classpath, so create a new library and add it to the Default Project Properties. Select Tools>Default Project Properties in the JDeveloper Console, then select the Profiles>Libraries node in the navigation frame of the Default Project Properties frame. Click on the New button to create a new library for the DB2 driver classes.

Now specify a Library Name, Location, and the Classpath for the library (be sure to include the db2jcc.jar and the db2jcc_license_cu.jar files in the Classpath field). The JDBC Type 4 driver classes for DB2 database are located in the db2jcc.jar file, and the license file for the DB2 server is located in the db2jcc_license_cu.jar file. A new library is added to the Selected Libraries box. See Figure 6.

Figure 6. Select libraries
Select libraries
Select libraries

To create a JDBC connection, select the Connections-Navigator tab (see Figure 7). Select the Connections>Database node, then right-click on the Database node and select New Database Connection.

Figure 7. Database>New database connection
Database>New database connection
Database>New database connection

In the Type frame, specify a connection name (see Figure 8), set the Connection Type to Third Party JDBC Driver, and click on the Next button.

Figure 8. Type frame
Type frame
Type frame

Next, in the Authentication frame specify Username and Password and click on the Next button. If the DB2 database server was installed with the default settings the username is db2admin. In the Connection frame, click on the New button to add a Driver Class, then in the Register JDBC Driver frame, specify Driver Class as COM.ibm.db2.jdbc.app.DB2Driver and Library as DB2 (which was configured in the Tools>Default Project Properties). In the Connection Frame's URL field, specify the connection URL as jdbc:db2://localhost:50000/DB2 , and click on the Next button. See Figure 9.

Figure 9. Connection URL
Connection URL
Connection URL

Now in the Test frame, click on the Test Connection button to test the JDBC connection with the DB2 database. If a connection with the DB2 database is established, the message Success! is displayed. Click on the Finish button to complete the JDBC connection configuration. A node for the JDBC connection now is added to the Connection-Navigator frame. See Figure 10.

Figure 10. Connection pool node
Connection pool node
Connection pool node

In addition, a connection configured in the Connections Navigator is available as a datasource for JSP applications. For example, the JDBC connection DBConnection1 is available as datasource jdbc/DBConnection1DS. A connection from the datasource may be obtained as explained with this code:

InitialContext initialContext = new InitialContext();
    javax.sql.DataSource ds = (javax.sql.DataSource)
    initialContext.lookup("java:comp/env/jdbc/DBConnection1DS");
   java.sql.Connection conn = ds.getConnection();

Conclusion

Keep in mind that database applications developed with the JDeveloper IDE, the WebLogic, or the JBoss application server are not limited to the Oracle or Hypersonic database servers. There are many advantages of using DB2 UDB as your database server with JBoss or Weblogic. DB2 UDB v8.2 provides a full function type 4 JDBC driver that can be easily configured using these non-IBM application severs and developer tools.


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Information Management
ArticleID=77717
ArticleTitle=Configure DB2 UDB with third-party application servers
publish-date=04282005