I have a lot of Websphere servers with different JNDI name of my DB connection so I have to build many ears files for each server. I'd like to do something like this:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${SPECIFIC_JNDI_NAME}"/>
</bean>
How can I create this SPECIFIC_JNDI_NAME variable?
The proper Java EE way to do it, is using resource references in your code like: java:comp/env/jdbc/myDSRef, then this resource reference is bind to actual JNDI name during the installation process.
You either define references via #Resource tag, or entry in the deployment descriptor (web.xml or ejb-jar.xml).
You map it to the JNDI name via admin console, wsadmin installation script, or ibm-web-bnd.xml file placed in the WEB-INF folder.
It is possible to use references with Spring.
This is the wrong way to go about it. One advantage of JNDI is that you can bind objects (in this case a datasource) under one JNDI name without a care for where it came from, how it was instantiated, etc. as long as it was there at the time it was first accessed.
You (or whoever configures the JNDI names) are basically trying to take away that advantage by binding different datasources on different JNDI names.
A workaround could be to bind the 'custom' name to a 'standard' JNDI name such that your application can still refer to the 'standard' name and the onus for providing the right bean is on those who configure the JNDI but really, if you go that far you can also just give the datasource the standard name. Also, I'm not sure that is even possible in JNDI, I just know that it used to be possible in Spring's own configuration.
Related
Currently, we store our application's environment properties in a .properties file in the WEB-INF. We want to move them to a database table. But we still want to specify the jndi name, and when running in our test environment locally, we want to be able to override certain properties just for our workspace for test and development.
Apache commons' DatabaseConfigurator seemed nice, but wouldn't play nice with the jndi name being defined as a property in the file. Nothing I did to ask it to look at the property file first worked.
I decided to subclass apache commons' AbstractConfiguration to try to create a single configurator that would check the file and database as I wished, but again, it didn't really work. Spring wants that jndi name absolutely first, probably because the data source has to be passed into the configurator as a parameter.
How can I get what I am after here? Mostly properties in the database, but those that are in the file override them. And jndi name for the datasource should not have to be hardcoded in the spring config.
Why don't you write a ApplicationContext listener that will read the configuration from your DB and inject them in the JNDI? Then you can override the configuration in the JNDI with a context.xml file that will be placed in the src/local/webapp/META-INF/.
This is how we get this working in our webapp.
Found couple of blogs providing steps to define jndi datasources and then accessing from Spring Container.
Method 1 :
1. Creating a file context.xml in src\main\webapp\META-INF folder and defining all
relevant jndi datasources.
2. Accessing jndi datasource from spring application context.xml file by using
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/MyDB"/>
</bean>
I tried above configuration and this works.
I found some other approaches too.
Method 2 : http://makecodeeasy.blogspot.in/2013/05/jndi-datasource-in-spring.html and Tomcat 6/7 JNDI with multiple datasources where
1. Datasources are defined in server.xml file (tomcat) and refered in web.xml file.
2. Accessing jndi datasources from spring application context.xml file
using JndiObjectFactoryBean with an additional resource ref attribute.
Again. what about using below line to achieve Step 2 in above two methods.
<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/MyDB" />
Basically I want to know which one is better server connection pooling mechanism. Is there any advantage of one approach over other. Which one is advanced and why?
==EDITED==
Once spring application context gets datasource related info through jndi defined in context.xml, how do Junit testcases which run Standalone not in Web Container would access JNDI resources ?
Can someone help me in figuring out these.
Looking at this documentation, it seems that both methods do exactly the same thing.
As far as connection pooling is concerned, JNDI lookups have nothing to do with that. Whatever connection pool you declare in context.xml (or whatever equivalent you use) will be looked up and used by Spring. If you declare BoneCP as your connection pool then that will be used, likewise if you use the Tomcat Connection pool or whatever else.
Recently I started a maven project to build an application for integrating
Spring, JPA, JSF
But in the automatically generated folder structure I can see one file named as
db.properties
and also I have one
persistence.xml
Now my question is that Database connection can be defined in either of these files, Can anybody tell me
1. Which way is better and why ?
2. Why there is db.properties file automatically generated while I already have persistence.xml ?
db.properties file is like messages.properties which is used to define key value pair. And after that we will use keys in expression language. So configurations will only be done in
persistence.xml or dataSource.xml
whichever is preferred choice but the values we will take from db.properties in the form of expression language eg.
driverClassName=com.mysql.jdbc.Driver
this is an entry in your db.properties. and you will use it in persistence.xml as follows.
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
I assume, from the fact that you mention JSF, that you are building a web application for deployment to an application server. I also caveat this question in that I don't know about db.properties or where it comes from.
When deploying to an application server, it is always best to configure your database connections in the container and expose them to the application via JNDI. This allows the container to manage connection pooling and credentials and keeps this information out of your WAR/EAR files. It also ensures that your WAR/EAR files are agnostic to the particular database instance, so can be deployed to a container in any environment without modification.
Therefore, I recommend against configuring your datasource in persistence.xml.
See also Difference between configuring data source in persistence.xml and in spring configuration files which is a similar question- the accepted answer there expresses the solution in more detail.
I would like to have optionally supplied information available to my webapp when deployed to Websphere (we are using 8.5) via the application's JNDI context.
I know that I can put a resource-ref or resource-env-ref in my web.xml but when I do that WAS will require me to supply a binding for it and deployment time.
My application will look in JNDI for certain values and adjust its behavior if found, but will function fine with default behaviour if it does not find values in JNDI. How, in WAS, do I supply a binding (just for a string or a URL) for my webapp without declaring a dependency on it in a resource-ref or resource-env-ref in my web.xml.
I know how to do this in Tomcat, I just put a Environment entry in the context.xml, like this:
<Environment
name="com.myorg.config"
value="http://localhost:8081/suff"
type="java.lang.String"/>
You can use an #Resource String lookupName; to accomplish the same in a portable manner. You will be prompted for a value at deployment time, but you can specify nothing, which will cause no value to be injected.
I would like to configure a DataSource using JNDI in a Java SE app. What is the best way to do this?
So far, I've come across 2 projects:
Apache Naming. The project page has a specific example for configuring a data source, but it looks like the project is super old and no longer active.
JBossNS. It looks like it's easy to configure a local-only JNDI using LocalOnlyContextFactory, but I haven't found any docs on how to actually configure a data source.
If possible, I would like to also configure the data source with a JTA transaction manager (using JOTM?).
Why are you using JNDI for this? It's not that it's a bad solution if you have a provider but there are alternatives such as dependency injection (IoC: via Spring or Guice).
The Spring JDBC data access is described here. The great thing is that you can use Spring to inject a DataSource into your code:
<bean class="com.my.Persister">
<property name="dataSource" ref="dataSource" />
</bean>
The data source can be defined using a JNDI-lookup:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/MyDataSource" />
In a test environment, you could inject the data source directly:
<bean id="dataSource" class="apache.db.PoolingDataSource">
<!-- config goes here -->
</bean>
These references are pretty old but may help to use jnpserver (JBoss Naming Service provider):
Working With JNDI In A J2SE Application
Standalone JNDI server using jnpserver.jar
A very easy to use solution for stand-alone JNDI is simple-jndi. It works like a charm as long as you only need it within a single JVM, since it's a library no network server.
The Simple-JNDI version, referenced by klenkes74, is not under active development any more. Because I encountered some issues with it I forked it, did some bug fixes and implemented some new features. I already used the old version not only for testing but in production too because I prefer a Service Locator pattern over Dependency Injection although the latter one is more trendy nowadays.
You can easily use Simple-JNDI to define a DataSource or a connection pool declaratively and get it bound to a JNDI Context.
Define a jndi.properties file in your classpath:
java.naming.factory.initial=org.osjava.sj.SimpleContextFactory
org.osjava.sj.root=[absolute_or_relative_path_to_a_property_file]
The property file looks like:
type=javax.sql.DataSource
driver=org.gjt.mm.mysql.Driver
url=jdbc:mysql://localhost/testdb
user=testuser
password=testing
Now you can access your DataSource from inside your code this way:
InitialContext ctxt = new InitialContext();
DataSource ds = (DataSource) ctxt.lookup("name_of_your_datasource");
For more information see https://github.com/h-thurow/Simple-JNDI