1

How to bind a JDBC datasource to JNDI context java:comp/env/jdbc only using code-based approach.

We need to write resource-ref in the web.xml for binding a dataSource to JNDI local context java:comp/env/jdbc. But I want to use only org.springframework.web.context.AbstractContextLoaderInitializer instead of web.xml(the old approach).

We know the method InitialContext#createSubcontext. But some application servers(e.g. Websphere) do not accept to edit the context java:comp/env/jdbc/.

Any solutions?

Versions:

  • Spring 4.0.7
  • Servlet 3.0
sndyuk
  • 2,720
  • 2
  • 24
  • 32

2 Answers2

1

You could try a full programmatic approach using Spring's SimpleNamingContextBuilder:

  SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
  DataSource ds = new DriverManagerDataSource(...);
  builder.bind("java:comp/env/jdbc/myds", ds); // you control the datasource
  builder.activate();

It's mainly there for test-purposes. If you choose to use it then you need to provide your own connection pool (e.g. Apache's Jakarta Commons DBCP).

I once used for testing when I deployed to cloudbees. see this blogpost

This works in "plain" Tomcat. I don't have a EE server to test with, but you could try it and see what happens (I guess the JNDI binding name has to be unique to the server though).

thomas77
  • 1,100
  • 13
  • 27
0

What is your application server?

See if this helps.

How to use JNDI DataSource provided by Tomcat in Spring?

Community
  • 1
  • 1
  • I know how to use a datasource in Spring. I want to know how to bind a datasource to `java:comp/env/jdbc/..`. – sndyuk Mar 29 '15 at 07:27