How to Make JBoss App talk to MySql

1. Configuring MySql in JBoss

MySQL is an open source database used by many open source projects and small organizations. To use JBoss 4.0 with MySQL, we first need to put the MySQL driver classes into the CLASSPATH. Copy the .jar file mysql-connector-java-3.0.9-stable-bin.jar to the /server/default/lib directory.

To use the MySQL data source, copy /docs/examples/jca/mysql-ds.xml to the /server/default/deploy directory. Modify the mysql-ds.xml configuration file by setting <driver-class/> to com.mysql.jdbc.Driver and <connection-url/> to jdbc:mysql://<mysqlhost>/<database>, where <mysqlhost> is the MySQL host server and <database> is the MySQL database.

After the file is copied to the Deploy folder, this can be monitored from MBean in JMX Console

Next, we need to set the <datasource> and <type-mapping> elements in the standardjaws.xml or jaws.xml file:

<jaws>
  <datasource>java:/MySqlDS</datasource> 
  <type-mapping>mySQL</type-mapping>
</jaws> 

We also need to set the <datasource> and <datasource-mapping> elements in the standardjbosscmp-jdbc.xml or jbosscmp-jdbc.xml file:

<jbosscmp-jdbc>
    <defaults>
       <datasource>java:/MySqlDS</datasource>
       <datasource-mapping>mySQL</datasource-mapping>
  </defaults>
</jbosscmp-jdbc>

Finally, we modify login-config.xml with MySQL database settings. Add the following <application-policy/> element to login-config.xml:

<application-policy name = "MySqlDbRealm"> 
   <authentication> 
      <login-module code =  
  "org.jboss.resource.security.ConfiguredIdentityLoginModule" 
                            flag = "required"> 
         <module-option name ="principal">sa</module-option> 
         <module-option name ="userName">sa</module-option> 
         <module-option name ="password"></module-option> 
         <module-option name ="managedConnectionFactoryName">  
            jboss.jca:service=LocalTxCM,name=MySqlDS 
         </module-option> 
      </login-module> 
   </authentication> 
</application-policy>  

By modifying the mysql-ds.xml, standardjaws.xml, standardjbosscmp-jdbc.xml, and login-config.xml files, the JBoss 4.0 server is configured to be used with a MySQL database.

Files Attached for reference

Click here for  more info

2. Programming to talk to MySql in JBoss

Advertisements

Leave a comment

Filed under j2ee, jboss

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s