Thursday, April 8, 2010

Cayenne JNDI under JBoss

Cayenne JNDI under JBoss

I had already used Cayenne with JNDI under Tomcat without issues.  It is documented simply and is easy to implement.  But under JBoss if you have little experience with the product you run into a few more hurdles.

But here are the step I used under Cayenne 3 and JBoss 5.1

1. Copy the JDBC drivers to be used under server/default/lib

2. Cayenne modeler jar is required under WEB-INF/lib (excluding openmvc and jgoodies).  I used Maven to drive this by adding the following to your pom.xml:
        <dependency>
            <groupId>org.apache.cayenne</groupId>
            <artifactId>cayenne-modeler</artifactId>
            <version>3.0RC2</version>
            <exclusions>
                <exclusion>
                    <groupId>org.scopemvc</groupId>
                    <artifactId>scopemvc</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>jgoodies</groupId>
                    <artifactId>looks</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

3. For each DB connection create a file under ${jboss-home}\server\default\deploy create a *-ds.xml file:
<datasources>
    <local-tx-datasource>
        <jndi-name>BREDomain</jndi-name>
        <connection-url>jdbc:jtds:sqlserver://xxxxxxxxxx</connection-url>
        <driver-class>net.sourceforge.jtds.jdbc.Driver</driver-class>
        <user-name>jboss</user-name>
        <password>xxxxxxxxx</password>
    </local-tx-datasource>
</datasources>

It just needs the extension "-ds.xml" and JBoss will pick it up automatically

 
4. Using cayenne modeler select JNDIDataSourceFactory and set location to "jdbc/MyDS"

A couple of steps required in your web application (war file) 
5. WEB-INF/web.xml:
  <resource-ref>
        <res-ref-name>jdbc/MyDS</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
 
6. WEB-INF/jboss-web.xml:
<jboss-web>
    <resource-ref>
        <res-ref-name>jdbc/MyDS</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <jndi-name>java:/MyDS</jndi-name>
    </resource-ref>
 




No comments: