How to use JNDI DataSource provided by Tomcat in Spring?

If using Spring’s XML schema based configuration, setup in the Spring context like this: <beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:jee=”http://www.springframework.org/schema/jee” xsi:schemaLocation=” http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd”> … <jee:jndi-lookup id=”dbDataSource” jndi-name=”jdbc/DatabaseName” expected-type=”javax.sql.DataSource” /> Alternatively, setup using simple bean configuration like this: <bean id=”DatabaseName” class=”org.springframework.jndi.JndiObjectFactoryBean”> <property name=”jndiName” value=”java:comp/env/jdbc/DatabaseName”/> </bean> You can declare the JNDI resource in tomcat’s server.xml using something … Read more

How should I connect to JDBC database / datasource in a servlet based application?

A common practice is to configure this as a DataSource in the servlet container in question. It will provide you connection pooling facilities which will greatly improve performance. Also a common practice is to externalize the raw settings in some configuration file which is been placed in the classpath. In case you’re using Tomcat as … Read more

Spring Boot Configure and Use Two DataSources

Here you go. Add in your application.properties file: #first db spring.datasource.url = [url] spring.datasource.username = [username] spring.datasource.password = [password] spring.datasource.driverClassName = oracle.jdbc.OracleDriver #second db … spring.secondDatasource.url = [url] spring.secondDatasource.username = [username] spring.secondDatasource.password = [password] spring.secondDatasource.driverClassName = oracle.jdbc.OracleDriver Add in any class annotated with @Configuration the following methods: @Bean @Primary @ConfigurationProperties(prefix=”spring.datasource”) public DataSource primaryDataSource() { return … Read more