Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JNDI DataSource provided by WebLogic in Spring?

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?

like image 975
KItis Avatar asked Dec 03 '12 23:12

KItis


People also ask

What is the use of JNDI in WebLogic?

JNDI provides a common-denominator interface to many existing naming services, such as LDAP (Lightweight Directory Access Protocol) and DNS (Domain Name System). These naming services maintain a set of bindings, which relate names to objects and provide the ability to look up objects by name.

Is JNDI used in Spring?

JPA Configuration – Model, DAO and Service. With this, you have everything you need in order to use your JNDI datasource in your Spring application.


2 Answers

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.

like image 190
Assen Kolov Avatar answered Sep 17 '22 19:09

Assen Kolov


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");
}
like image 36
Scott Leonard Avatar answered Sep 17 '22 19:09

Scott Leonard