Cayenne User Documentation
Using JNDI

Cayenne can be setup to obtain a DataSource via JNDI, instead of using its own connection pool. To do that Cayenne DataNodes must be configured to use JNDIDataSourceFactory. This can be done in the modeler as shown on the pictures below.

1. Select JNDIDataSourceFactory:

2. Enter DataSource JNDI Name:

Development with JNDI DataNodes

To be able to connect to the database from CayenneModeler when JNDIDataSourceFactory is specified (and thus no explicit connection information is associated with the DataNode), you may configure a "local DataSource" (see a corresponding Modeler Guide chapter).

In runtime, a property-based mechanism to override Modeler DataSource definitions is used (actually the overrides take precedence of whether the DataSource is JNDI, DBCP, driver, etc). A quick configuration example is shown below:

-Dcayenne.jdbc.driver=com.mysql.jdbc.Driver -Dcayenne.jdbc.url=jdbc:mysql://localhost/mydb \
-Dcayenne.jdbc.username=user -Dcayenne.jdbc.password=password

For more details and configuration options see javadocs of org.apache.cayenne.configuration.server.PropertyDataSourceFactory.

Deployment in Container

A JNDI DataSource is mapped in a servlet container / application server as appropriate for a given container. Below is a Tomcat 5.5 example, which is an XML snippet normally placed in $CATALINA_HOME/conf/server.xml file between the <Host>...</Host> tags:

<Context path="/myapp" docBase="myapp">
  <Resource name="jdbc/myds" auth="Container"
          type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver"
          url="jdbc:oracle:thin:@127.0.0.1:1521:dbname"
          username="userName" password="secret" maxActive="5" maxIdle="2"/>
</Context>

Depending on how the DataSource is mapped in a container, you may optionally need to add a "resource-ref" entry to the 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>
.