Tuesday, October 6, 2009

Using JNDI in JRuby on Rails for JBoss

Its very easy to user JNDI with JBoss in your JRuby on Rails application. At first you will need to install all the required gems/plugins and for that please see my previous blog JRuby on Rails: using JDBC driver. Now if you have everything okay that is specified in that post you will have to configure followings.

I am assuming you don't have any JNDI datasource created in your JBoss.

To create a JNDI datasource in your JBoss create a file my-oracle-ds.xml with following content and save it in JBOSS_HOME/server/default/deploy folder:

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>MyDS</jndi-name>
<connection-url>jdbc:oracle:thin:@172.189.171.149:1521:MYDB</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>dbuser</user-name>
<password>dbpassword</password>
<use-java-context>false</use-java-context>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>Oracle10g</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>

In your jboss-web.xml file:

<jboss-web>
.......
<resource-ref>
<res-ref-name>jdbc/MyDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<jndi-name>MyDS</jndi-name>
</resource-ref>
</jboss-web>

In your web.xml file:

<resource-ref>
<res-ref-name>jdbc/MyDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Now to access the database in your database.yml use following configuration:

development:
adapter: jdbc
jndi: java:comp/env/jdbc/MyDS
driver: oracle.jdbc.driver.OracleDriver

No comments: