blob: 4452495499ddcea3625e036bcb58b42ac08775f3 [file] [log] [blame]
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 &quot;local DataSource&quot; (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 &quot;resource-ref&quot; entry to the <TT>web.xml</TT> file:</P>
<DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent">
<PRE>&lt;resource-ref&gt;
&lt;res-ref-name&gt;jdbc/myds&lt;/res-ref-name&gt;
&lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
&lt;res-auth&gt;Container&lt;/res-auth&gt;
&lt;/resource-ref&gt;</PRE>
</DIV></DIV>
<P>Below are sample DataSource configurations for Tomcat 5.5 and 5.0. The XML should be pasted into <TT>$CATALINA_HOME/conf/server.xml</TT> file between the <TT>&lt;Host&gt;...&lt;/Host&gt;</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>&lt;Context path=&quot;/myapp&quot; docBase=&quot;myapp&quot;&gt;
&lt;Resource name=&quot;jdbc/myds&quot; auth=&quot;Container&quot;
type=&quot;javax.sql.DataSource&quot; driverClassName=&quot;oracle.jdbc.driver.OracleDriver&quot;
url=&quot;jdbc:oracle:thin:@127.0.0.1:1521:dbname&quot;
username=&quot;userName&quot; password=&quot;secret&quot; maxActive=&quot;5&quot; maxIdle=&quot;2&quot;/&gt;
&lt;/Context&gt;</PRE>
</DIV></DIV>
<H4><A name="UsingJNDI-Tomcat5.0Configuration"></A>Tomcat 5.0 Configuration</H4>
<DIV class="preformatted panel" style="border-width: 1px;"><DIV class="preformattedContent panelContent">
<PRE>&lt;Context path=&quot;/myapp&quot; docBase=&quot;myapp&quot;&gt;
&lt;Resource name=&quot;jdbc/myds&quot; auth=&quot;Container&quot; type=&quot;javax.sql.DataSource&quot;/&gt;
&lt;ResourceParams name=&quot;jdbc/myds&quot;&gt;
&lt;parameter&gt;
&lt;name&gt;driverClassName&lt;/name&gt;
&lt;value&gt;oracle.jdbc.driver.OracleDriver&lt;/value&gt;
&lt;/parameter&gt;
&lt;parameter&gt;
&lt;name&gt;url&lt;/name&gt;
&lt;value&gt;jdbc:oracle:thin:@127.0.0.1:1521:dbname&lt;/value&gt;
&lt;/parameter&gt;
&lt;parameter&gt;
&lt;name&gt;username&lt;/name&gt;
&lt;value&gt;***&lt;/value&gt;
&lt;/parameter&gt;
&lt;parameter&gt;
&lt;name&gt;password&lt;/name&gt;
&lt;value&gt;****&lt;/value&gt;
&lt;/parameter&gt;
&lt;parameter&gt;
&lt;name&gt;maxActive&lt;/name&gt;
&lt;value&gt;20&lt;/value&gt;
&lt;/parameter&gt;
&lt;parameter&gt;
&lt;name&gt;maxIdle&lt;/name&gt;
&lt;value&gt;10&lt;/value&gt;
&lt;/parameter&gt;
&lt;/ResourceParams&gt;
&lt;/Context&gt;</PRE>
</DIV></DIV>