How to use JNDI DataSource provided by WebLogic in Spring?

24,283

Solution 1

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.

Solution 2

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");
}

Solution 3

There is another option:

<jee:jndi-lookup id="dbDataSource" jndi-name="jdbc/MyLocalDB"
        expected-type="javax.sql.DataSource" />
Share:
24,283
KItis
Author by

KItis

software engineer

Updated on July 13, 2022

Comments

  • KItis
    KItis almost 2 years

    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?