Saturday, July 14, 2007

Configuring Datasource in Jboss using MySQL and Oracle

MySQL Datasource Configuration in Jboss App Server:

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 to com.mysql.jdbc.Driver and to jdbc:mysql:///, where is the MySQL host server and is the MySQL database.
Next, we need to set the and elements in the standardjaws.xml or jaws.xml file:

java:/MySqlDS
mySQL

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

java:/MySqlDS
mySQL

Finally, we modify login-config.xml with MySQL database settings. Add the following element to login-config.xml:

sa
sa

jboss.jca:service=LocalTxCM,name=MySqlDS

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.

In Client:

Hashtable ht=new Hashtable();
ht.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
ht.put(InitialContext.PROVIDER_URL,"jnp://localhost:1099");
ht.put(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
initialContext = new InitialContext(ht);
javax.sql.DataSource ds = (javax.sql.DataSource) initialContext.lookup("java:MySqlDS");
java.sql.Connection conn = ds.getConnection();
// do the necessary database operations on connection.



Troubleshooting:
---------------------

javax.naming.NamingException: MySql not bound.

Then make sure that you have following entry in mySql-ds.xml:

false


Similarly for configuring oracle datasource in jboss:
----------------------------------------------------------------

Oracle Configuration

Oracle is a very popular enterprise database used for its performance and reliability. To configure JBoss 4.0 with Oracle, we first need to put Oracle's driver classes in the CLASSPATH. Copy Oracle's JDBC driver .zip file /jdbc/lib/classes12.zip to the server/default/lib directory.
To use Oracle's transactional (XA) data source, copy /docs/examples/jca/oracle-xa-ds.xml to the /server/default/deploy directory. To configure with the non-XA data source, copy /docs/examples/jca/oracle-ds.xml instead, to /server/default/deploy dir.
Next, we need to modify the oracle-ds.xml configuration file. The and settings for Oracle are as follows:

Oracle OCI Type 2 Driver
Class: oracle.jdbc.driver.OracleDriver
URL: jdbc:oracle:oci8:@
Oracle OCI Thin Type 4 Driver
Class: oracle.jdbc.driver.OracleDriver
URL: jdbc:oracle:thin:@::
Oracle OCI XA Type 2 Driver
Class: oracle.jdbc.xa.client.OracleXADataSource
URL: jdbc:oracle:thin:@::
Oracle OCI Type 2 Driver
Class: oracle.jdbc.driver.OracleDriver
URL: jdbc:oracle:oci8:@

In the Connection URL setting, is the HOST value specified in the /network/ADMIN/tnsnames.ora file, and is the PORT value specified in the tnsnames.ora file, and is the database name.
Next, we modify the standardjaws.xml or jaws.xml configuration file. Set the and elements as follows:


java:/OracleDS
Oracle8


Next, we modify the standardjbosscmp-jdbc.xml or jbosscmp-jdbc.xml configuration file, setting the and elements to use Oracle:



java:/OracleDS
Oracle8



Finally, we need to modify login-config.xml to use Oracle. Add the following element to login-config.xml:




sa
sa


jboss.jca:service=LocalTxCM,name=OracleDS





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




No comments: