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?
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.
JPA Configuration – Model, DAO and Service. With this, you have everything you need in order to use your JNDI datasource in your Spring application.
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.
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");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With