Linking the Datasource to Our Application


With the datasource in place, we need to tell our application to use it. The amount of effort involved depends on how coupled our application is to the datasource. If an application used the datasource directly, we would need to adjust the lookup code, or at least the environment references, and update our SQL statements to be compatible with MySQL. Fortunately for us, the ToDo application uses container-managed persistence and the database access is completely transparent. So, migrating the application will be quite simple.

How do I do that?

This one is going to be easy. JBoss looks for the container-managed persistence configuration in a jbosscmp-jdbc.xml deployment descriptor. The default datasource for the application is set in the defaults section of the file, as shown here:

     <!DOCTYPE jbosscmp-jdbc PUBLIC         "-//JBoss//DTD JBOSSCMP-JDBC 4.0//EN"         "http://www.jboss.org/j2ee/dtd/jbosscmp-jdbc_4_0.dtd">     <jbosscmp-jdbc>         <defaults>             <datasource>java:MySqlDS</datasource>             <datasource-mapping>mySQL</datasource-mapping>         </defaults>     </jbosscmp-jdbc> 

The datasource element contains the JNDI name of the datasource we specified in the mysql-ds.xml file earlier. The datasource-mapping name declares the type of the database. The database type determines how to generate SQL and how to map types for a particular database.

You can look at the conf/standardjbosscmp-jdbc.xml file to see the full set of datasource mappings available in JBoss. Inside the type-mappings section, you should find the mySQL type mapping referenced in the jbosscmp-jdbc.xml file:

     <type-mapping>         <name>mySQL</name>         ...     </type-mapping> 

The type mapping defines SQL templates for schema generation, EJB-QL, and JBOSS-QL function mappings, as well as the default mapping for standard Java types. Here is the mapping for java.lang.String:

     <mapping>         <java-type>java.lang.String</java-type>         <jdbc-type>VARCHAR</jdbc-type>         <sql-type>VARCHAR(250) BINARY</sql-type>     </mapping> 

If you need to change the mapping for strings for all MySQL databases in the system, you can edit the standardjbosscmp-jdbc.xml file. In general you'll probably want to edit the mapping on a field-by-field basis, but it is nice to know that you have the power to edit the systemwide mapping if need be.

The jbosscmp-jdbc.xml file is placed in the META-INF directory of the EJB JAR, right next to the ejb-jar.xml and jboss.xml deployment descriptors. In order to include the MySQL-specific jbosscmp-jdbc.xml file, add -Doptional.dd=mysql to the ant commands used to build the ToDo application, as shown here:

     $ ant -Doptional.dd=mysql main deploy 

This builds and deploys the MySQL version of the application.

What just happened?

The ToDo application is no longer speaking to the internal Hypersonic database. Instead it is speaking to the MySQL instance you set up. And just as before, JBoss automatically created the database schema appropriate for our entity beans. You can check to see that our tables were created correctly using the show tables command:

     mysql> use jbossdb;     Database changed     mysql> show tables;     +-------------------+     | Tables_in_jbossdb |     +-------------------+     | Comment           |     | Task              |     +-------------------+     2 rows in set (0.19 sec) 

You can check the generated schema for the table using the describe command:

     mysql> describe task;     +---------------+--------------+------+-----+---------+-------+     | Field         | Type         | Null | Key | Default | Extra |     +---------------+--------------+------+-----+---------+-------+     | id            | varchar(250) |      | PRI |         |       |     | name          | varchar(250) | YES  |     | NULL    |       |     | user          | varchar(250) | YES  |     | NULL    |       |     | startedDate   | datetime     | YES  |     | NULL    |       |     | completedDate | datetime     | YES  |     | NULL    |       |     +---------------+--------------+------+-----+---------+-------+     5 rows in set (0.42 sec) 

If you access the application and create a few tasks, you can easily query the data from the mysql tool and verify that everything is working:

     mysql> select name,user,startedDate from task;     +---------------------+-------+---------------------+     | name                | user  | startedDate         |     +---------------------+-------+---------------------+     | take over the world | brain | 2005-01-18 00:50:29 |     | eat the box         | pinky | 2005-01-18 00:51:22 |     +---------------------+-------+---------------------+     2 rows in set (0.20 sec) 



JBoss. A Developer's Notebook
JBoss: A Developers Notebook
ISBN: 0596100078
EAN: 2147483647
Year: 2003
Pages: 106

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