I am developing a web application with Tomcat 8.5.4. I'm using a properties configured as Resource. So, my $TOMCAT_HOME/conf/server.xml contains
<GlobalNamingResources>
<Resource name="properties/global" auth="Container" type="java.util.Properties" />
</GlobalNamingResources>
The META-INF/context.xml of my webapp has
<Context antiResourceLocking="true" path="/mywebapp">
<ResourceLink global="properties/global" name="properties/global" type="java.util.Properties"/>
</Context>
I'm looking up the resource as follow
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
(Properties) envContext.lookup("properties/global");
However, I'm getting the exception
javax.naming.NameNotFoundException: Name [properties/global] is not bound in this Context. Unable to find [properties].
What's wrong? Thanks.
Related
This is my code:
Context ctx = new InitialContext();
Context envCtx = (Context)ctx.lookup("java:comp/env");
It usually works fine, but sometimes it throws this error:
Name java:comp is not bound in this Context
javax.naming.NameNotFoundException: Name java:comp is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at javax.naming.InitialContext.lookup(Unknown Source)
This is my context.xml:
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Environment name="fileLocation" value="dev" type="java.lang.String" override="false"/>
<Environment name="log4JFileLocation" override="false" type="java.lang.String" value="dev"/>
Any ideas of what is going on?
Thanks :)
I'm very confused why this isn't working. The environment is a Liferay 6.1 instance with Tomcat 7 and the database is NOT the default database from Liferay. It's a secondary server that is used for data. So I'm not sure if that matters with Liferay or not.
web.xml (located in webapps/conf in Tomcat)
<web-app>...
<resource-ref>
<description>My database</description>
<res-ref-name>jdbc/xxx</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
server.xml (located in webapps/conf in Tomcat)
<GlobalNamingResources>
<Resource name="jdbc/xxx" auth="Container" type="javax.sql.DataSource" username="a" password="y" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/xxx"
maxActive="200" maxIdle="25" />
</GlobalNamingResources>
context.xml (located in webapps/conf in Tomcat)
<context>
<ResourceLink global="jdbc/xxx" name="jdbc/xxx" type="javax.sql.DataSource" />
</context>
Code:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/xxx");
Connection conn = ds.getConnection();
Error:
2016-12-21 19:13:04 FATAL asdasdsd:128 - Exception thrown in (removed):
javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/xxx] is not bound in this Context. Unable to find [java:comp].
at org.apache.naming.NamingContext.lookup(NamingContext.java:820)
at org.apache.naming.NamingContext.lookup(NamingContext.java:168)
What am I missing?
Edit:
Also confirmed the following:
mysql connector is in the path
same jar is in the classpath of the portlet
Second Edit:
I created a brand new, fresh dynamic web application project with the same configuration and DAO layer and it worked 100%. I have a feeling it's related to Liferay now. Unfortunately..
Third Edit:
Tried everything, including this article: http://www.journaldev.com/2513/tomcat-datasource-jndi-example-java
This didn't work either. Same exception. The lack of information about this issue on the Liferay website is amazing to me. Documentation appears to be very much lacking.
For anyone that this helps in the future, because boy was this a pain and not well documented.
I had to add this to the portlet-ext.properties of the Liferay application:
portal.security.manager.strategy=none
Once I found this link: Liferay/Tomcat "hot-deploy" closes JNDI connection, how can I keep it open?
It ended up solving my problem.
The other solution would be (It works for me), Remove the resource-ref from web.xml, remove the Resource from server. xml and remove ResourceLink from context.xml.
Add the resource alone to context.xml,
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="20" maxIdle="10" maxWait="-1" name="jdbc/xxx" password="" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/xxx" username=""/>
Code (java:comp/env/ is required in this case):
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/xxx");
Connection conn = ds.getConnection();
I have a Java project and I've been trying to create a JNDI connection for my Ingres database but have been unsuccessful. I'm not sure if there is something specific to ingres that needs to be included but after quite a bit of research I haven't been able to get things to work.
In my project I have my datasource info in the web.xml file and the context.xml
context.xml has the following info
<Context>
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.Datasource"
username="myUser" password="password" driverClassName="com.ingres.jdbc.IngresDriver"
url="databaseURL" maxActive="8" maxIdle="4" maxWait="100" />
</Context>
My web.xml has the following info
<web-app>
<resource-ref>
<description>Project Descrip</description>
<res-ref-name>jdbc/myDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
In my java code I'm trying to get my connection using the following four lines
Context initContext = new InitialContext();
Context envContext = (Context) initContent.lookup("java:comp/env");
Datasource ds = (DataSource) envContext.lookup("jdbc/myDB");
return ds.getConnection();
After the third line is executed I get an exception that says: NamingException - Cannot create resource instance
I have found dozens of posts with this same exception and have tried the suggested solutions with no luck. I'm using a Tomcat 7 server and have made sure to include the necessary ingres jar (iijdbc.jar) to my WEB-INF/lib folder and to my tomcat lib folder.
Any help or suggestions would be greatly appreciated
I don't know much about Ingres, but if attempting to make a datasource is similar to JBOSS and DB2 or MySQL. I noticed that in your WEB.XML you define your resource-ref, but you don't mention the anything about the servlet parameters.
<servlet>
<description>Servlet Description</description>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>servlet.MyServlet</servlet-class>
<init-param>
<param-name>MyDB</param-name>
<param-value>java:comp/env/jdbc/MyDB</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Then you would need to code in your sevlet:
InitialContext initialContext = new InitialContext();
DataSource dataSource = (DataSource)initialContext.lookup(this.getInitParameter("MyDB"));
Hope this helps.
Can you try adding global="jdbc/myDB" attribute as in your Resource tag as follows:
<Context>
<Resource name="jdbc/myDB" auth="Container" type="javax.sql.Datasource"
username="myUser" password="password" driverClassName="com.ingres.jdbc.IngresDriver"
url="databaseURL" maxActive="8" maxIdle="4" maxWait="100" global="jdbc/myDB"/>
</Context>
I am trying to set up the database connection properties using JNDI for a Spring web application.
I am considering two approaches as below:
Approach 1:
In your Spring configuration you may have something like:
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/facs"/>
Then in your webapp /META-INF/context.xml file you should have something similar too:
<?xml version='1.0' encoding='utf-8'?>
<!-- antiResourceLocking="true" -->
<Context path="/podd-apn"
reloadable="true"
cachingAllowed="false"
antiResourceLocking="true"
>
<Resource name="jdbc/facs"
type="javax.sql.DataSource" username="${database.username}" password="${database.password}"
driverClassName="org.postgresql.Driver"
url="${database.url}"
maxActive="8" maxIdle="4"
global="jdbc/facs"
/>
</Context>
And in your web.xml you should something like:
<!-- JNDI -->
<resource-ref>
<description>FACs Datasource</description>
<res-ref-name>jdbc/facs</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Approach 2:
Setup in the Spring context like this:
<jee:jndi-lookup id="dbDataSource"
jndi-name="jdbc/DatabaseName"
expected-type="javax.sql.DataSource" />
You can declare the JNDI resource in Tomcat's server.xml using something like this:
<GlobalNamingResources>
<Resource name="jdbc/DatabaseName" auth="Container" type="javax.sql.DataSource"
username="dbUsername" password="dbPasswd"
url="jdbc:postgresql://localhost/dbname"
driverClassName="org.postgresql.Driver"
initialSize="5" maxWait="5000"
maxActive="120" maxIdle="5"
validationQuery="select 1"
poolPreparedStatements="true"/>
</GlobalNamingResources/>
And reference the JNDI resource from Tomcat's web context.xml like this:
<ResourceLink name="jdbc/DatabaseName"
global="jdbc/DatabaseName"
type="javax.sql.DataSource"/>
My question is where is the best place to keep database properties? Should they be placed in server.xml or context.xml?
Also, if I have 2 databases, should I use two configs?
Also, is it best practice to directly place them in either server.xml or context.xml? Or do I need to configure through Tomcat Manager GUI console?
Thanks!
I prefer a third approach that takes the best from
Approach 1 and Approach 2 described by user1016403.
Approach 3
Save database properties on the server.xml
reference the server.xml database properties from the web application META-INF/context.xml
Approach 3 benefits
While the first point is useful for security reasons the second point is useful for referencing server properties value from the web application, even if server properties values will change.
Moreover decoupling resource definitions on the server from their use by the web application makes such configuration scalable across organizations with various complexity where different teams work on different tiers/layers: the server administrators team can work without conflicting with developers team if the administrator shares the same JNDI name with the developer for each resource.
Approach 3 implementation
Define the JNDI name jdbc/ApplicationContext_DatabaseName.
Declare the jdbc/ApplicationContext_DatabaseName's various properties and values in Tomcat's server.xml using something like this:
<GlobalNamingResources>
<Resource name="jdbc/ApplicationContext_DatabaseName" auth="Container" type="javax.sql.DataSource"
username="dbUsername" password="dbPasswd"
url="jdbc:postgresql://localhost/dbname"
driverClassName="org.postgresql.Driver"
initialSize="5" maxWait="5000"
maxActive="120" maxIdle="5"
validationQuery="select 1"
poolPreparedStatements="true"/>
</GlobalNamingResources/>
Link the jdbc/ApplicationContext_DatabaseName's properties from web application META-INF/context.xml by an application-private JNDI context java:comp/env/ specified in the name attribute:
<Context path="/ApplicationContext" ... >
<!--
"global" attribute links to GlobalNamingResources in the ${catalina.base}/conf/server.xml (server administrator team)
"name" attribute is relative to the application-private JNDI context java:comp/env/ and is looked up from the java web application (application developer team)
-->
<ResourceLink global="jdbc/ApplicationContext_DatabaseName" name="jdbc/DatabaseName" type="javax.sql.DataSource"/>
</Context>
Finally, in order to use the JNDI resource, specify the JNDI name jdbc/DatabaseName in web application's deployment descriptor:
<resource-ref>
<description>DatabaseName's Datasource</description>
<res-ref-name>jdbc/DatabaseName</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
and in Spring context:
<jee:jndi-lookup id="DatabaseNameDataSource"
jndi-name="jdbc/DatabaseName"
expected-type="javax.sql.DataSource" />
Approach 3 drawbacks
If the JNDI name gets changed then both the server.xml and the META-INF/context.xml will have to be edited and a deploy would be necessary; nevertheless this scenario is rare.
Approach 3 variations
Many data sources used by one web application
Simply add configurations to Tomcat's server.xml:
<GlobalNamingResources>
<Resource name="jdbc/ApplicationContext_DatabaseName1" ... />
<Resource name="jdbc/ApplicationContext_DatabaseName2" ... />
...
</GlobalNamingResources/>
Add link web application META-INF/context.xml by an application-private JNDI context java:comp/env/ specified in the name attribute:
<Context path="/ApplicationContext" ... >
<ResourceLink global="jdbc/ApplicationContext_DatabaseName1" name="jdbc/DatabaseName1" ... />
<ResourceLink global="jdbc/ApplicationContext_DatabaseName2" name="jdbc/DatabaseName2" ... />
...
</Context>
Finally add JNDI resources usage in web application's deployment descriptor:
<resource-ref>
<description>DatabaseName1's Datasource</description>
<res-ref-name>jdbc/DatabaseName1</res-ref-name> ...
</resource-ref>
<resource-ref>
<description>DatabaseName2's Datasource</description>
<res-ref-name>jdbc/DatabaseName2</res-ref-name> ...
</resource-ref>
...
and in Spring context:
<jee:jndi-lookup id="DatabaseName1DataSource"
jndi-name="jdbc/DatabaseName1" ... />
<jee:jndi-lookup id="DatabaseName2DataSource"
jndi-name="jdbc/DatabaseName2" ... />
...
Many data sources used by many web application on the same server
Simply add configuration to Tomcat's server.xml:
<GlobalNamingResources>
<Resource name="jdbc/ApplicationContextX_DatabaseName1" ... />
<Resource name="jdbc/ApplicationContextX_DatabaseName2" ... />
<Resource name="jdbc/ApplicationContextY_DatabaseName1" ... />
<Resource name="jdbc/ApplicationContextY_DatabaseName2" ... />
...
</GlobalNamingResources/>
the others configuration should be deducible from previous variation case.
Many data sources to the same database used by many web application on the same server
In such case a Tomcat's server.xml configurations like:
<GlobalNamingResources>
<Resource name="jdbc/ApplicationContextX_DatabaseName" ... />
<Resource name="jdbc/ApplicationContextY_DatabaseName" ... />
ends up in two different web applications META-INF/context.xml like:
<Context path="/ApplicationContextX" ... >
<ResourceLink global="jdbc/ApplicationContextX_DatabaseName" name="jdbc/DatabaseName" ... />
</Context>
and like:
<Context path="/ApplicationContextY" ... >
<ResourceLink global="jdbc/ApplicationContextY_DatabaseName" name="jdbc/DatabaseName" ... />
</Context>
so someone might be worried about the fact that the same name="jdbc/DatabaseName" is looked up, and then used, by two different applications deployed on the same server: this is not a problem because the jdbc/DatabaseName is an application-private JNDI context java:comp/env/, so ApplicationContextX by using java:comp/env/ can't (by design) look up the resource linked to global="jdbc/ApplicationContextY_DatabaseName".
Of course if you felt more relaxed without this worry you might use a different naming strategy like:
<Context path="/ApplicationContextX" ... >
<ResourceLink global="jdbc/ApplicationContextX_DatabaseName" name="jdbc/applicationXprivateDatabaseName" ... />
</Context>
and like:
<Context path="/ApplicationContextY" ... >
<ResourceLink global="jdbc/ApplicationContextY_DatabaseName" name="jdbc/applicationYprivateDatabaseName" ... />
</Context>
YOUR_APP.xml file
I prefer Approach 2 (put everything (not only some attribute in the config), but instead of placing them in the global server.xml or global context.xml you should place it in the application-specific context.xml.default YOUR_APP.xml file in your Tomcat.
The YOUR_APP.xml file is located in $catalinaHome/conf/<engine>/<host> (for example conf/Catalina/localhost/YOUR_APP.xml).
The configuration in application specific YOUR_APP.xml is only available for the specific application.
See the guide published by MuleSoft. And see the official documentation, Tomcat Configuration Reference, page for The Context Container
Version 9
Version 8
To quote that documentation:
Individual Context elements may be explicitly defined:
• …
• In individual files (with a ".xml" extension) in the $CATALINA_BASE/conf/[enginename]/[hostname]/ directory. The context path and version will be derived from the base name of the file (the file name less the .xml extension).
• …
Approach 4
Instead of using JNDI I work with .properties files and build complex object during program initialization instead on configuration time.
You already use Spring and it is easy construct DataSource by:
<context:property-placeholder location="classpath:app.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:#${db.host}:${db.port}:${db.user}"/>
<property name="username" value="${db.user}"/>
<property name="password" value="${db.pass}"/>
</bean>
I completely agree with Ralph with using deployment descriptor in $CATALINA_BASE/conf/[enginename]/[hostname]/$APP.xmlbut instead JNDI I like plain key-value file!
With Spring injecting above properties into bean fields are easy:
#Value("${db.user}") String defaultSchema;
instead of JNDI:
#Inject ApplicationContext context;
Enviroment env = context.getEnvironment();
String defaultSchema = env.getProperty("db.user");
Note also that EL allow this (default values and deep recursive substitution):
#Value('${db.user:testdb}') private String dbUserName;
<property name='username' value='${db.user.${env}}'/>
To externalize .properties file I use modern Tomcat 7 that has org.apache.catalina.loader.VirtualWebappLoader:
<Loader className="org.apache.catalina.loader.VirtualWebappLoader"
virtualClasspath="/srv/web/app/"/>
So your devops fill virtualClasspath with local external full paths which is separate per application and put local app.propertiesto that dir.
See also:
Adding a directory to tomcat classpath
Can I create a custom classpath on a per application basis in Tomcat
Externalizing Tomcat webapp config from .war file
How to read a properties file outside my webapp context in Tomcat
Configure Tomcat to use properties file to load DB connection information
Externalize Tomcat configuration
step 1: context.xml
<Context path="/projectname">
<Resource auth="Container"
driverClassName="com.mysql.jdbc.Driver"
logAbandoned="true"
maxActive="100" ``
maxIdle="30"
maxWait="10000"
name="refname"
removeAbandoned="true"
removeAbandonedTimeout="60"
type="javax.sql.DataSource"
url="jdbc:mysql://localhost:8080/dbname"
username="root"
password="root"/>
</Context>
Step 2 : web.xml
<resource-ref>
<description>DB Connection</description>
<res-ref-name>refname</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Step 3 : create a class to get connection
Connection connection = null;
Context context = (Context) new InitialContext().lookup("java:comp/env");
DataSource ds = (DataSource) context.lookup("refname");
connection = ds.getConnection();
Everything is set
You also can use JNDI URL support for different application configuration for test, integration test, production.
<Context>
...
<Resource auth="Container" factory="com.benasmussen.jndi.url.URLFactory"
name="url/MyUrl" type="java.net.URL" url="file:///your/path/to/file"/>
...
</Context>
<jee:jndi-lookup id="myUrl" jndi-name="java:comp/env/url/MyUrl" expected-type="java.net.URL" />
Check out the GitHub project Tomcat JNDI URL Support to enable JNDI URL support for Tomcat servers.
I have the following resource tag in my context.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/myApp">
<Resource name="jdbc/myDS" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="1000"
username="user" password="passwd"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/myDB" />
</Context>
I am developing a Java web app using the Stripes framework in NetBeans.
How can I get this resource from within a Java class?
You need your bean to be instantiated by something (a dependecy injection framework) which knows how to handle the #Resrouce annotation. JSP itself doesn't know how.
In this case it would be simpler to locate the DataSource in the JNDI context:
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/myDS");
Thank you Bozho for the answer. I only had to change the lookup string to get it to work:
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/myDS");
Connection conn = ds.getConnection();