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).
Cayenne also supports container-less runtime operation of the JNDI node. It works like this:
- JNDIDataSourceFactory attempts to locate a DataSource via a default JNDI provider.
- If the DataSource is not found, JNDIDataSourceFactory attempts to read local user preferences database, looking for a local DataSource matching the JNDI name.
- If such DataSource is found, it is used in the application as if it was obtained via JNDI.
This way Modeler preferences database works as a substitute of a JNDI provider, saving extra configuration steps in development mode, when an application may be run from the IDE. Requirements to use this feature:
- The name of the local DataSource in the Modeler preferences must match the JNDI name of the DataNode.
- cayenne-modeler.jar and hsqldb-x.x.x.jar must be in the application runtime CLASSPATH.
Deployment in Container
Depending on how the DataSource is mapped in the 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>
Below are sample DataSource configurations for Tomcat 5.5 and 5.0. The XML should be pasted into $CATALINA_HOME/conf/server.xml file between the <Host>...</Host> tags. Of course the application name and database parameters should be replaced with the correct values for the target environment.
Tomcat 5.5 Configuration
<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>
Tomcat 5.0 Configuration
<Context path="/myapp" docBase="myapp"> <Resource name="jdbc/myds" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/myds"> <parameter> <name>driverClassName</name> <value>oracle.jdbc.driver.OracleDriver</value> </parameter> <parameter> <name>url</name> <value>jdbc:oracle:thin:@127.0.0.1:1521:dbname</value> </parameter> <parameter> <name>username</name> <value>***</value> </parameter> <parameter> <name>password</name> <value>****</value> </parameter> <parameter> <name>maxActive</name> <value>20</value> </parameter> <parameter> <name>maxIdle</name> <value>10</value> </parameter> </ResourceParams> </Context>