<p>The Weblogic servers we are using have been configured to allow JNDI datasource names like "appds".</p>

<p>For development (localhost), we might be running Tomcat and when declared in the &lt;context&gt; section of server.xml, Tomcat will hang JNDI datasources on "java:comp/env/jdbc/*" in the JNDI tree.</p>

<p><strong>Problem:</strong> in Weblogic, the JNDI lookup is "appds" whilst in Tomcat, it seems that that I must provide the formal "java:comp/env/jdbc/appds". I'm afraid the Tomcat version is an implicit standard but unfortunately, I can't change Weblogic's config ... so that means we end up with two different spring config files (we're using spring 2.5) to facilitate the different environments.</p>

<p>Is there an elegant way to address this. Can I look JNDI names up directly in Tomcat? Can Spring take a name and look in both places? Google searches or suggestions would be great.</p>

[{'answer_id': 48172, 'author': 'Marcio Aguiar', 'author_id': 4213, 'author_profile': 'https://Stackoverflow.com/users/4213', 'pm_score': 1, 'selected': False, 'text': "<p>How about an evironment variable? Set developers machines with the tomcat name and production with the Weblogic name. You can even set your code to use a default one (WebLogic) in case the variable don't exist.</p>\n"}, {'answer_id': 50552, 'author': 'Binil Thomas', 'author_id': 3973, 'author_profile': 'https://Stackoverflow.com/users/3973', 'pm_score': 0, 'selected': False, 'text': '<p>My application also had a similar problem and this is how I solved it:</p>\n\n<p>1) <code>WEB-INF/classes/application.properties</code> contains the entry:</p>\n\n<pre>\nds.jndi=java:comp/env/jdbc/tcds\n</pre>\n\n<p>2) On the WLS machine, I have an entry in the <code>/etc/sysenv</code> file:</p>\n\n<pre>\nds.jndi=wlsds\n</pre>\n\n<p>3) I configured spring to lookup the JNDI vis the property <code>${ds.jndi}</code>, using a <code>PropertyPlaceholderConfigurer</code> bean with <code>classpath:application.properties</code> and <code>file:/etc/sysenv</code> as locations. I also set the <code>ignoreResourceNotFound</code> to <code>true</code> so that developers need not have <code>/etc/sysenv</code> on their machines.</p>\n\n<p>4) I run an integration test using Cargo+Jetty and I could not properly set up a JNDI environment there. So I have a fallback <code>BasicDataSource</code> configured too using the <code>defaultObject</code> property of <code>JndiObjectFactoryBean</code>.</p>\n'}, {'answer_id': 444367, 'author': 'martsraits', 'author_id': 55036, 'author_profile': 'https://Stackoverflow.com/users/55036', 'pm_score': 5, 'selected': True, 'text': '<p><code>JndiLocatorSupport</code> has a property <code>resourceRef</code>. When setting this true, "java:comp/env/" prefix will be prepended automatically. So I believe it would be correct to differentiate this parameter when moving from Tomcat to Weblogic.</p>\n'}, {'answer_id': 444553, 'author': 'harmanjd', 'author_id': 54321, 'author_profile': 'https://Stackoverflow.com/users/54321', 'pm_score': 1, 'selected': False, 'text': '<p>How are you referencing the resource in spring?</p>\n\n<p>This is what we have for tomcat:</p>\n\n<p><strong>context:</strong></p>\n\n<pre><code>&lt;Resource name="jms/ConnectionFactory" auth="Container" type="org.apache.activemq.ActiveMQConnectionFactory" description="\nJMS Connection Factory"\n        factory="org.apache.activemq.jndi.JNDIReferenceFactory" brokerURL="tcp://localhost:61615" brokerName="StandaloneAc\ntiveMQBroker"/&gt;\n</code></pre>\n\n<p><strong>spring:</strong></p>\n\n<pre><code>    &lt;beans xmlns="http://www.springframework.org/schema/beans"\n       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n       xmlns:util="http://www.springframework.org/schema/util"\n       xmlns:aop="http://www.springframework.org/schema/aop"\n       xmlns:jee="http://www.springframework.org/schema/jee"\n       xsi:schemaLocation="\nhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd\nhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd\nhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd\nhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"&gt;\n\n&lt;jee:jndi-lookup jndi-name="jms/ConnectionFactory" id="connectionFactory" resource-ref="true"\n                         expected-type="javax.jms.ConnectionFactory" lookup-on-startup="false"/&gt;\n</code></pre>\n\n<p>The jee namespace comes from:</p>\n\n<p><a href="http://www.springframework.org/schema/jee" rel="nofollow noreferrer">http://www.springframework.org/schema/jee</a> <a href="http://www.springframework.org/schema/jee/spring-jee-2.0.xsd" rel="nofollow noreferrer">http://www.springframework.org/schema/jee/spring-jee-2.0.xsd</a></p>\n'}, {'answer_id': 1017300, 'author': 'duffymo', 'author_id': 37213, 'author_profile': 'https://Stackoverflow.com/users/37213', 'pm_score': 2, 'selected': False, 'text': '<p>I\'ve managed the trick with Tomcat and WebLogic using Spring.  <a href="http://craicpropagation.blogspot.com/2009/02/how-to-use-same-jndi-resource-name-on.html" rel="nofollow noreferrer">Here</a> is a description of how it worked for me.</p>\n'}, {'answer_id': 4270169, 'author': 'Leonel', 'author_id': 15649, 'author_profile': 'https://Stackoverflow.com/users/15649', 'pm_score': 4, 'selected': False, 'text': '<p><strong>How to use a single JNDI name in your web app</strong></p>\n\n<p>I\'ve struggled with this for a few months myself. The best solution is to make your application portable so you have the same JNDI name in both Tomcat and Weblogic.</p>\n\n<p>In order to do that, you change your <code>web.xml</code> and <code>spring-beans.xml</code> to point to a single jndi name, and provide a mapping to each vendor specific jndi name.</p>\n\n<p>I\'ve placed each file below.</p>\n\n<p>You need:</p>\n\n<ul>\n<li>A <code>&lt;resource-ref /&gt;</code> entry in web.xml for your app to use a single name</li>\n<li>A file <code>WEB-INF/weblogic.xml</code> to map your jndi name to the resource managed by WebLogic</li>\n<li>A file <code>META-INF/context.xml</code> to map your jndi name to the resource managed by Tomcat\n\n<ul>\n<li>This can be either in the Tomcat installation or in your app.</li>\n</ul></li>\n</ul>\n\n<p>As a general rule, prefer to have your jndi names in your app like <code>jdbc/MyDataSource</code> and <code>jms/ConnFactory</code> and avoid prefixing them with <code>java:comp/env/</code>.</p>\n\n<p>Also, data sources and connection factories are best managed by the container and used with JNDI. It\'s a <a href="https://stackoverflow.com/questions/3111992/difference-between-configuring-data-source-in-persistence-xml-and-in-spring-confi/4138327#4138327">common mistake to instantiate database connection pools in your application</a>.</p>\n\n<p><em>spring</em></p>\n\n<pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;\n&lt;beans xmlns="http://www.springframework.org/schema/beans"\n       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n       xmlns:jee="http://www.springframework.org/schema/jee"\n       xsi:schemaLocation="\nhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\nhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"&gt;\n\n&lt;jee:jndi-lookup jndi-name="jdbc/appds"\n                 id="dataSource" /&gt;\n&lt;/beans&gt;\n</code></pre>\n\n<p><em>web.xml</em></p>\n\n<pre><code>&lt;resource-ref&gt;\n    &lt;description&gt;My data source&lt;/description&gt;\n    &lt;res-ref-name&gt;jdbc/appds&lt;/res-ref-name&gt;\n    &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;\n    &lt;res-auth&gt;Container&lt;/res-auth&gt;\n&lt;/resource-ref&gt;\n</code></pre>\n\n<p><em>weblogic.xml</em></p>\n\n<pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;\n&lt;weblogic-web-app\n    xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app"\n    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n    xsi:schemaLocation="\n        http://xmlns.oracle.com/weblogic/weblogic-web-app http://http://www.oracle.com/technology/weblogic/weblogic-web-app/1.1/weblogic-web-app.xsd"&gt;\n\n&lt;resource-description&gt;\n    &lt;jndi-name&gt;appds&lt;/jndi-name&gt;\n    &lt;res-ref-name&gt;jdbc/appds&lt;/res-ref-name&gt;\n&lt;/resource-description&gt;\n&lt;/weblogic-web-app&gt;\n</code></pre>\n\n<p><em>META-INF/context.xml</em> (for Tomcat)</p>\n\n<pre><code>&lt;Context&gt;\n    &lt;ResourceLink global="jdbc/appds" name="jdbc/appds" type="javax.sql.DataSource"/&gt;\n&lt;/Context&gt;\n</code></pre>\n'}, {'answer_id': 4820808, 'author': 'Konstantin', 'author_id': 592771, 'author_profile': 'https://Stackoverflow.com/users/592771', 'pm_score': 1, 'selected': False, 'text': '<p>Setting up DataSource in the application itself is not that crazy :) I would say that is even mandatory if application is meant to be deployed on a grid. River, GigaSpaces, or similar. </p>\n\n<p>Note: I do not say connection settings have to be hardcoded inside of WAR, they need to be supplied at deployment time/runtime. This simplifies management of cloud instances since there is only on place to configure.</p>\n\n<p>Configuring resources at the container makes sense only if multiple applications are deployed there and they can use shared resource. </p>\n\n<p>Again, in cloud type of deployments there is only one application per servlet container instance.  </p>\n'}, {'answer_id': 7001749, 'author': 'Val Blant', 'author_id': 652634, 'author_profile': 'https://Stackoverflow.com/users/652634', 'pm_score': 2, 'selected': False, 'text': '<p>The following config works in Tomcat and Weblogic for me.</p>\n\n<p>In Spring:</p>\n\n<pre><code>&lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;\n   &lt;!-- This will prepend \'java:comp/env/\' for Tomcat, but still fall back to the short name for Weblogic --&gt;\n   &lt;property name="resourceRef" value="true" /&gt; \n   &lt;property name="jndiName" value="jdbc/AgriShare" /&gt;\n&lt;/bean&gt;\n</code></pre>\n\n<p>In Weblogic Admin Console create a JDBC resource named <code>jdbc/AgriShare</code>. Under \'Targets\', <b>MAKE SURE YOU TARGET THE DATASOURCE TO THE SERVER YOU ARE DEPLOYING YOUR APP TO!</b>. This particular point cost me some time just now...</p>\n'}]
['https://Stackoverflow.com/questions/47676', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4910/']