| Title: Using JNDI |
| |
| <P>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 <TT>JNDIDataSourceFactory</TT>. This can be done in the modeler as shown on the pictures below.</P> |
| |
| <P>1. Select JNDIDataSourceFactory:<BR> |
| <SPAN class="image-wrap" style=""><IMG src="using-jndi.data/jndi-step1.jpg" style="border: 0px solid black"></SPAN></P> |
| |
| |
| <P>2. Enter DataSource JNDI Name:<BR> |
| <SPAN class="image-wrap" style=""><IMG src="using-jndi.data/jndi-step2.jpg" style="border: 0px solid black"></SPAN></P> |
| |
| |
| <H3><A name="UsingJNDI-DevelopmentwithJNDIDataNodes"></A>Development with JNDI DataNodes</H3> |
| |
| <P>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 <A href="local-datasources.html" title="Local DataSources">Modeler Guide chapter</A>).</P> |
| |
| <P>Cayenne also supports container-less runtime operation of the JNDI node. It works like this:</P> |
| |
| <UL> |
| <LI>JNDIDataSourceFactory attempts to locate a DataSource via a default JNDI provider.</LI> |
| <LI>If the DataSource is not found, JNDIDataSourceFactory attempts to read local user preferences database, looking for a local DataSource matching the JNDI name.</LI> |
| <LI>If such DataSource is found, it is used in the application as if it was obtained via JNDI.</LI> |
| </UL> |
| |
| |
| <P>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:</P> |
| |
| <UL> |
| <LI>The name of the local DataSource in the Modeler preferences must match the JNDI name of the DataNode.</LI> |
| <LI><TT>cayenne-modeler.jar</TT> and <TT>hsqldb-x.x.x.jar</TT> must be in the application runtime CLASSPATH.</LI> |
| </UL> |
| |
| |
| |
| <H3><A name="UsingJNDI-DeploymentinContainer"></A>Deployment in Container</H3> |
| |
| <P>Depending on how the DataSource is mapped in the container, you may optionally need to add a "resource-ref" entry to the <TT>web.xml</TT> file:</P> |
| |
| <DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent"> |
| <PRE><resource-ref> |
| <res-ref-name>jdbc/myds</res-ref-name> |
| <res-type>javax.sql.DataSource</res-type> |
| <res-auth>Container</res-auth> |
| </resource-ref></PRE> |
| </DIV></DIV> |
| |
| <P>Below are sample DataSource configurations for Tomcat 5.5 and newer. The XML should be pasted into <TT>$CATALINA_HOME/conf/server.xml</TT> file between the <TT><Host>...</Host></TT> tags. Of course the application name and database parameters should be replaced with the correct values for the target environment. </P> |
| |
| |
| |
| <H4><A name="UsingJNDI-Tomcat5.5Configuration"></A>Tomcat 5.5 Configuration</H4> |
| |
| <DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent"> |
| <PRE><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></PRE> |
| </DIV></DIV> |
| |