Java Tutorial - Java Scipt : Configuring the DataSource in the EJB Container

Java Tutorial - Java Scipt :

Configuring the DataSource in the EJB Container


We will be using the JBoss EJB container for this application. The EJB container needs to be configured with a DataSource for our database. JBoss 3.0 supports databases using the Java Connector Architecture (JCA). However, most databases do not come with JCA support at this time. JBoss addresses this concern by creating a JCA wrapper around an existing JDBC driver. An XML file describing the information needed to deploy the database configures the JCA wrapper. Samples of these XML files can be found in the docs/ examples/jca directory below the directory where JBoss has been installed. The following XML file is based on the mysql-service.xml sample, but has been edited to make it easier to understand:

<?xml version=”1.0” encoding=”UTF-8”?>
<server>
<mbean
code=”org.jboss.resource.connectionmanager.LocalTxConnectionManager”
name=”jboss.jca:service=LocalTxCM,name=MySqlDS”>
<depends optional-attribute-name=”ManagedConnectionFactoryName”>
<!--embedded mbean-->
<mbean
code=”org.jboss.resource.connectionmanager.RARDeployment”
name=”jboss.jca:service=LocalTxDS,name=MySqlDS”>
<attribute name=”JndiName”>MySqlDS</attribute>
<attribute name=”ManagedConnectionFactoryProperties”>
<properties>
<config-property
name=”ConnectionURL”
type=”java.lang.String”>\
jdbc:mysql://localhost:3306/oldfriends</config-property>
<config-property
name=”DriverClass”
type=”java.lang.String”>\
com.mysql.jdbc.Driver</config-property>
<config-property
name=”UserName”
type=”java.lang.String”>dbuser</config-property>
<config-property
name=”Password”
type=”java.lang.String”>passwd</config-property>
</properties>
</attribute>
<depends
optional-attribute-name=”OldRarDeployment”>\
jboss.jca:service=\
RARDeployment,name=JBoss LocalTransaction JDBC Wrapper</depends>
</mbean>
</depends>
<depends optional-attribute-name=”ManagedConnectionPool”>
<!--embedded mbean-->
<mbean
code=”org.jboss.resource.connectionmanager.JBossManagedConnectionPool”
name=”jboss.jca:service=LocalTxPool,name=MySqlDS”>
<attribute name=”MinSize”>0</attribute>
<attribute name=”MaxSize”>50</attribute>
<attribute name=”BlockingTimeoutMillis”>5000</attribute>
<attribute name=”IdleTimeoutMinutes”>15</attribute>
<attribute name=”Criteria”>ByContainer</attribute>
</mbean>
</depends>
<depends
optional-attribute-name=”CachedConnectionManager”>
jboss.jca:service=CachedConnectionManager</depends>
<depends
optional-attribute-name=”JaasSecurityManagerService”>
jboss.security:service=JaasSecurityManager</depends>
<attribute
name=”TransactionManager”>\
java:/TransactionManager</attribute>
<!--make the rar deploy! hack till better deployment-->
<depends>jboss.jca:service=RARDeployer</depends>
</mbean>
</server>

There are five elements in this file that we are primarily interested in and areused to configure the database. All of these have a name attribute that is used to identify the element. All but one of these is a <config-property> element. The elements we are interested in, by name, are:

·         JndiName
·         ConnectionURL
·         DriverClass
·         UserName
·         Password

The JndiName specifies that name that will be used to look up the connection in the future. The other names correspond to the arguments that are needed to establish a JDBC connection. These values should be edited within the file to reflect the values in your system. In our case, the values are set as follows


·         JndiName is “MySqlDS”
·         ConnectionURL is “jdbc:mysql://localhost:3306/oldfriends”
·         DriverClass is “com.mysql.jdbc.Driver”
·         UserName is “dbuser”
·         Password is “passwd”

After the XML file has been edited, there are two steps to deploying it. First, copy the .jar file for the JDBC driver into the lib directory below your JBoss server configuration. This will most likely be jbossversion/server/ default/lib. Next, copy the edited XML file into the deploy directory. After this is done, JBoss should be restarted. We can easily check to see if the resource was installed by using the JBoss jmx-console application. Using a browser from the same machine as JBoss, enter in the following URL: http://localhost:8080/jmx-console Scroll down to the section titled jboss.jca and look for lines that begin with name=MySqlDS, as shown in Figure 13.4. Once this is successful, we are ready to start writing some Java code.