Using Spring SimpleNamingContextBuilder and Apache BasicDataSource , you can do something like this (usually I have this in a static block in test classes that need JNDI):
BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(db_driver_name); dataSource.setUrl(db_connection_url); dataSource.setUsername(db_username); dataSource.setPassword(db_password); SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder(); builder.bind(jndi_name, dataSource); builder.activate();
The jndi_name value might look like this: java:comp/env/jdbc/my-db
Once this is set up, code that normally looks for a database connection through JNDI should work. The above code, for example, will work with this Spring config:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env/jdbc/my-db"/> </bean>
Lauri lehtinen
source share