Chapter 3. JDBC

Table of Contents

Introduction
Minerva Pools
Upgrading from JBoss 2.0 FINAL

Aaron Mulder <ammulder@alumni.princeton.edu>

Introduction

Data Sources

One of the most common requirements is to create one or more data sources for your EJBs. You must create a data source for CMP entity beans, and it is the recommended way to interact with a database for BMP entity beans and session beans.

JBoss data sources provide database connection pooling. This means that when your application closes a connection, it is not really closed, just returned to the "ready" state. The next time your application requests a database connection, it may reuse the same connection. This saves you the overhead of opening new database connections for every request, and since normal web applications use connections very often but for a very short period of time, the savings can be significant. However, there are some new issues raised such as the fact that a database connection that is left unused in the pool for a long period of time may timeout. The JBoss pools have a number of configuration parameters to address issues like this.

Supported Databases

JBoss supports any database with a JDBC driver. We recommend pure java drivers (type 3 or type 4), and specifically suggest you do not use the JDBC-ODBC bridge (type 1).

Mappings Available for CMP Entities

If we have not worked with a database product before we may need to work with you to generate a type mapping if you plan to use container managed persistence. The mappings we have tested extensively include PostgreSQL, InstantDB, Hypersonic SQL, Oracle 7, Oracle 8, Sybase, DB2, and InterBase. Additional contributed mappings include PointBase, SOLID, mySQL, MS SQL Server, and DB2/400. If you would like to support CMP for another DBMS, or have a working mapping to share, please contact the JBoss Mailing List.

Installing JDBC Drivers

To install a JDBC driver, it must be distributed as one or more zip or jar files.

  1. Copy driver jar or zip files to the lib/ext directory in your JBoss distribution.

  2. Add intruction to load driver in jboss.jcml file

    For example Figure 3.1. shows how JBoss two default database drivers are included. If you are using database other than default, add your driver class name to the list of drivers in this mbean element of your jboss.jcml.

    Figure 3.1. Default driver loading instruction from jboss.jcml

    <mbean code="org.jboss.jdbc.JdbcProvider" name="DefaultDomain:service=JdbcProvider">
     <attribute name="Drivers">org.hsql.jdbcDriver,org.enhydra.instantdb.jdbc.idbDriver</attribute>
    </mbean>
    					
    					

    If, for instance, you are using Oracle database and don't want to use two default databases , then only include reference to your Oracle driver like shown in Figure 3.2.

    Figure 3.2. Oracle driver loading instruction from jboss.jcml

    <mbean code="org.jboss.jdbc.JdbcProvider" name="DefaultDomain:service=JdbcProvider">
     <attribute name="Drivers">oracle.jdbc.driver.OracleDriver</attribute>
    </mbean>
    					
    					

    If you are using option of using more than one driver don't include more that one org.jboss.jdbc.JdbcProvider mbean. All drivers should be listed in one org.jboss.jdbc.JdbcProvider mbean.

The next time you start JBoss, you should see output like the following listing each driver that was loaded. If instead you see an error for the driver (also shown below), make sure that you installed the required ZIPs and/or JARs to the lib/ext directory.

[JDBC] Loaded JDBC-driver:oracle.jdbc.driver.OracleDriver	
[JDBC] Could not load driver:com.sybase.jdbc2.jdbc.SybDriver