I am using WebSphere Liberty 17.0.0.2.
The end product is an ear that contains a jar which is using JPA to access database.
The EntityManager is annotated with #PersistenceContext with persistence unit name defined. In the persistence.xml under appropriate persistence-unit the jta-data-source tag contains datasource name (direct lookup) specified in server.xml. With this setup everything is working fine.
Now need arose that I need to switch from direct lookup to indirect lookup regarding datasource JNDI lookup method. As I understand indirect lookup is something like OS environment variables. I use a name to get the configured value, so I can switch datasources without touching my code to rename JDNI names there.
Switching from direct to indirect I'd need to append 'java:comp/env' in my persistence.xml for the jta-data-source.
How can I connect datasource name with indirect lookup name? I tried to specify it in server.xml using resource-ref tag but with no luck.
The main goal here is to using indirect lookup in the code but be able to change datasources in the applicaton server configuration, so I don't have to change my application when this is happning.
Configuration snippets:
server.xml
<library id="oraclelib">
<jdbcDriver id="oracledriver" libraryRef="oraclelib">
<dataSource jndiName="jdbc/oradb" jdbcDriverRef="oracledriver" id="oradbds">
<resource-ref name="jdbc/oradb" binding-name="jdbc/mydb">
persistence.xml
<jta-data-source>java:comp/env/jdbc/mydb</jta-data-source>
When running this setup a javax.naming.NameNotFoundException is thrown.
Update #1
server.xml after swapping name, binding-name
<?xml version="1.0" encoding="UTF-8"?>
<server description="app server">
<library id="OracleLib">
<fileset dir="/oracle" includes="ojdbc6.jar" />
</library>
<jdbcDriver id="OracleJDBCDriver" libraryRef="OracleLib" />
<dataSource jndiName="jdbc/oradb" jdbcDriverRef="OracleJDBCDriver" id="dbDataSource">
<properties.oracle URL="jdbc:oracle:thin:#//dbhost:port/SID" user="dbuser" password="dbpassword" />
</dataSource>
<application id="Myapp_ear" location="/path/myapp.ear" name="Myapp_ear" type="ear">
<application-bnd>
<resource-ref name="jdbc/mydb" binding-name="jdbc/oradb" />
</application-bnd>
</application>
</server>
jta-data-source is java:comp/env/jdbc/mydb
Solution
It turned out that the bean that was used to get EntityManager was a CDI bean. As it is modified to be an EJB bean, the ejb-jar.xml, ibm-ejb-jar-bnd.xml did the trick.
It should be the other way around:
not
<resource-ref name="jdbc/oradb" binding-name="jdbc/mydb">
but
<resource-ref name="jdbc/mydb" binding-name="jdbc/oradb">
name - is resource ref name, and binding-name is jndi name in the server configuration.
Related
We have a project that have quite a lot of dependencies. I am confused in Websphere resource binding and resource definition at all now.
ejb-jar.xml describes resources.
persistance.xml describes database resources.
We can have web.xml, where we describes resources also.
We can have ibm-web-bnd.xml, where we can have binding for resources.
Lets say, I have a *.jar that has definition of data source in persistance.xml (jta-data-source), same data source in ejb-jar.xml (enterprise-beans/session/resource-ref), and also has binding for this data source ibm-web-bnd.xml (resource-ref).
Now I want to use this *.jar in my *.war.
Lets assume that I have configured all resources on my Websphere Liberty server correctly.
During application start up, I've got following error for this *.jar:
Unable to use JDBC Connection to create Statement
java.sql.SQLException: Unsupported use of GenericConnection. A
GenericConnection is provided during application start when creating an
EntityManagerFactory for a persistence unit which has configured one of its
datasource to be in the component naming context; java:comp/env. During
application start, the component naming context will not exist, and the
correct datasource cannot be determined. When the persistence unit is used,
the proper datasource and connection will be obtained and used.
Questions:
Why my *.jar do not see Data Source, that I have defined on my Websphere Libery (in server.xml)? Do I miss some binding in my *.war?
Why do we describe resources in web.xml? I thought that this is definition of web application, like a servlet mapping, filters, etc.
Why same data source is described in both persistance.xml and ejb-jar.xml (is it not enough with persistance.xml?)?
Does resource binding delay somehow dependency injection?
Code in *.jar that I can't modify, but have to use in my *.war:
ejb-jar.xml
...
<enterprise-beans>
<session id="MyEntityManagerBean">
<ejb-name>MyEntityManagerBean</ejb-name>
<ejb-class>somepackage.MyEntityManagerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref id="some_id_goes_here">
<res-ref-name>jdbc/my_ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</session>
</enterprise-beans>
...
persistance.xml
<persistence-unit name="MyPersistentUnit" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:comp/env/jdbc/my_ds</jta-data-source>
...
</persistence-unit>
ibm-ejb-jar-bnd.xml
...
<session name="MyEntityManagerBean">
<resource-ref name="jdbc/my_ds" binding-name="jdbc/my_ds"/>
</session>
<session name="MyEntityManagerBean2">
<resource-ref name="jdbc/my_ds" binding-name="jdbc/my_ds"/>
</session>
...
MyEntityManagerBean.java (also same *.jar)
#PersistenceContext(unitName="MyPersistentUnit")
protected EntityManager entityManager;
And problem starts, when I add this *.jar as Maven dependency to my *.war.
Thanks in advance.
I have a WAR that I've set as the default web app in Tomcat's server.xml:
Inside Server.xml
<Host...>
<Context docBase="mywar" path="" />
In addition, I have a META-INF/context.xml file where I set an environment variable.
<?xml version="1.0" encoding="utf-8"?>
<Context debug="0" reloadable="true">
<Environment
name="my.name"
type="java.lang.String"
value="donrhummy"
/>
</Context>
But it's throwing an exception javax.naming.NameNotFoundException: Name my.name is not bound in this Context.
How can I get that context variable in my app?
NOTE: That variable exists when I go to http://localhost/mywar
The only solution i could find was:
Rename my WAR as ROOT.war (Case-sensitive)
Remove the <Context...> from server.xml
Tomcat will treat that as the root WAR (i.e. for the "/" path) and let it use the context form it's own META-INF directory.
I would like to configure a series of environment-dependant settings as an external resource, so that the same WAR artifact can be configured in any application server.
In Glassfish I rememeber that I just created a .properties file using the built-in Properties Resource JNDI Factory, and it worked like a charm, but there's no such thing in Tomcat.
I tried defining a java.net.URL and a java.lang.String to define just the path that this file is held in, but I get the following error:
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/c://app.properties]
So what would be a good way to create an environment-agnostic set of properties for my application?
PS: this is how I define my properties in Spring:
<jee:jndi-lookup id="appProperties" jndi-name="java:comp/env/url/urlAppProperties" />
<bean id="application-properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>#{ appProperties }</value>
</list>
</property>
</bean>
You can add external configuration values to the JNDI context using Tomcat's resource configuration elements. As described in the Tomcat documentation, you can define a <GlobalNamingResources> element in your server.xml listing a set of configuration values which are available to all contexts (web applications) on that server:
Example taken from the Tomcat docs:
<GlobalNamingResources ...>
...
<Environment name="maxExemptions" value="10"
type="java.lang.Integer" override="false"/>
...
</GlobalNamingResources>
These environment variables are then available via JDNI in the java:comp/env context. In order to use such variables in a web application, you have to link to them in the individual context.xml configuration files using the <ResourceLink> element.
<Context>
<ResourceLink name="maxExemptions" global="maxExemptions" type="java.lang.Integer"/>
</Context>
With this set up, you can access these values through the standard JNDI API.
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.
To connect to derby database using tomcat as the server initially I added the following to conf/context.xml of Tomcat :
<Resource name="jdbc/PollDatasource" auth="Container" type="javax.sql.DataSource"
driverClassName="org.apache.derby.jdbc.ClientDriver"
url="jdbc:derby://localhost:1527/poll database;create=true"
username="suhail" password="suhail"
maxActive="20" maxIdle="10" maxWait="-1" />
and the Resource-ref tag in web.xml of the WEB-INF/web.xml of the project.
<resource-ref>
<description>my connection</description>
<res-ref-name>jdbc/PollDatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
But when I ran a servlet that had to connect to the database,the statement :
connection = dataSource.getConnection();
caused an exception :
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
I haven't understood the reason for this exception yet.
After the exception I removed the Resource tag from the conf/context.xml of the Tomcat and placed it in the META-INF/context.xml of my project.
When I tried the servlet again,it worked without any exception !
What could be the reason I am getting the exception when I place the Resource tag inside the global context.xml file (i.e inside conf/context.xml) but do not get the exception when I place it inside the context.xml specific to my application ?. (i.e inside META-INF/context.xml)
Adding <Resource> to conf/context.xml will make a copy of that resource available for every webapp you deploy on your server -- probably not what you want to do. If you want a <Resource> to be globally available, it's more appropriate to put it into conf/server.xml under <GlobalNamiongResources>.
I would expect that the error you are getting is because you don't have your JDBC driver in the right place. Defining the <Resource> in conf/context.xml may result in a different ClassLoader being used to load your <Resource>.