6

It is said in Spring javadoc article about DriverManagerDataSource class, that this class is very simple and that it is recommended

to use a JNDI DataSource provided by the container. Such a DataSource can be exposed as a DataSource bean in a Spring ApplicationContext via JndiObjectFactoryBean

The question is: how to accomplish this?

For example if I wish to have DataSource bean to access my custo oracle database, what I require then? What to write in context configuration etc?

4 Answers 4

4

To access a JNDI data source you do something like:

<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="jdbc/MyDatabase"/> </bean> 

or look et the spring 'jee' schema.

The details of the database connection are configured in WebLogic, the application accesses the database through the jndi name.

Sign up to request clarification or add additional context in comments.

Comments

3

Or use Java based configuration and do it like this:

@Bean(destroyMethod = "") public DataSource dataSource() throws NamingException{ Context context = new InitialContext(); return (DataSource)context.lookup("jdbc.mydatasource"); } 

Comments

1

There is another option:

<jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/MyLocalDB" expected-type="javax.sql.DataSource" /> 

Comments

0

You can use following jndi configuration.

<beans:bean id="weblogicDataSource" class="org.springframework.remoting.rmi.JndiRmiProxyFactoryBean"> <beans:property name="jndiName" value="ConnectionPoolJNDINameAsConfigured"></beans:property> <beans:property name="jndiEnvironment"> <beans:props> <beans:prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</beans:prop> <beans:prop key="java.naming.provider.url">iiop://localhost:7001</beans:prop> </beans:props> </beans:property> <beans:property name="serviceInterface" value="javax.sql.DataSource"></beans:property> </beans:bean> 

and you make the reference to your injected class file as

<beans:bean id="xxxx" class="xxxxxxxx"> <beans:property name="wlDataSource" ref="weblogicDataSource" /> </beans:bean> 

and in your implemenation class, use

import javax.sql.DataSource; 

make an instance as private DataSource wlDataSource;

and corresponding setter. Now you are free to use JDBCTemplate or SimpleJDBCCall etc as per your implementation thinking.

  • Hope this will help.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.