Jdbc Database Connector Puddle Inwards Jump Framework – How To Setup Example

Setting upward JDBC Database Connection Pool inwards Spring framework is slow for whatever Java application, only thing of changing few configuration inwards leap configuration file.If y'all are writing heart as well as individual coffee application as well as non running on whatever spider web or application server similar Tomcat or  Weblogic,  Managing Database connector puddle using Apache Commons DBCP as well as Commons Pool along-with Spring framework is squeamish alternative but if y'all lead hold luxury of having spider web server as well as managed J2EE Container, regard using Connection puddle managed past times J2EE server those are improve alternative inwards damage of maintenance, flexibility as well as likewise assist to forestall java.lang.OutofMemroyError:PermGen Space inwards tomcat past times avoiding loading of JDBC driver inwards web-app class-loader, Also keeping JDBC connector puddle information inwards Server makes it slow to modify or include settings for JDBC over SSL. In this article nosotros volition run into how to setup Database connector puddle inwards leap framework using Apache common DBCP as well as commons pool.jar

This article is inwards continuation of my tutorials on leap framework as well as database similar LDAP Authentication inwards J2EE alongside Spring Security and  manage session using Spring security  If y'all haven’t read those article than y'all may detect them useful.

Spring Example JDBC Database Connection Pool

Spring framework provides convenient JdbcTemplate degree for performing all Database related operation. if y'all are non using Hibernate than using Spring's JdbcTemplate is proficient option. JdbcTemplate requires a DataSource which is javax.sql.DataSource implementation as well as y'all tin dismiss acquire this straight using spring bean configuration or past times using JNDI if y'all are using J2EE spider web server or application server for managing Connection Pool. See How to setup JDBC connector Pool inwards tomcat as well as Spring for JNDI based connector pooling for to a greater extent than details. In lodge to setup Data source y'all volition require next configuration inwards your applicationContext.xml (spring configuration) file:


//Datasource connector settings inwards Spring
<bean id="springDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" >
   <property name="url" value="jdbc:oracle:thin:@localhost:1521:SPRING_TEST" />
   <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
   <property name="username" value="root" />
   <property name="password" value="root" />
   <property name="removeAbandoned" value="true"/>
   <property name="initialSize" value="20" />
   <property name="maxActive" value="30" />
</bean>

//Dao degree configuration inwards spring
 <bean id="EmployeeDatabaseBean" class="com.test.EmployeeDAOImpl">
    <property name="dataSource" ref="springDataSource"/>
 </bean>

 
Below configuration of DBCP connector puddle volition create twenty database connector equally initialSize is twenty as well as goes upward to thirty Database connector if required equally maxActive is 30. y'all tin dismiss customize your database connector puddle past times using dissimilar properties provided past times Apache DBCP library. Above trial is creating connector puddle alongside Oracle 11g database as well as nosotros are using oracle.jdbc.driver.OracleDriver comes along with ojdbc6.jar or ojdbc6_g.jar ,  to acquire to a greater extent than well-nigh how to connect Oracle database from Java program run into the link.

Java Code for using Connection puddle inwards Spring

Setting upward JDBC Database Connection Pool inwards Spring framework JDBC Database Connection Pool inwards Spring Framework – How to Setup ExampleBelow is complete code trial of DAO degree which uses Spring JdbcTemplate to execute SELECT interrogation against database using database connector from Connection pool. If y'all are non initializing Database connector puddle on start-up than it may lead hold a piece when y'all execute your commencement interrogation because it needs to create sure enough pose out of SQL connector as well as and hence it execute interrogation but ane time connector puddle is created subsequent queries volition execute faster.

//Code for DAO Class using Spring JdbcTemplate
package com.test
import javax.sql.DataSource;
import org.log4j.Logger;
import org.log4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * Java Program trial to purpose DBCP connector puddle alongside Spring framework
 * @author Javin Paul
 */

public class EmployeeDAOImpl implements EmployeeDAO {

    private Logger logger = LoggerFactory.getLogger(EmployeeDAOImpl.class);
    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public boolean isEmployeeExists(String emp_id) {
        try {
            logger.debug("Checking Employee inwards EMP tabular array using Spring Jdbc Template");
            int pose out = this.jdbcTemplate.queryForInt("select count(*) from EMP where emp_id=?", emp_id);
            if (number > 0) {
                return true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }
}


Dependency:
1. y'all involve to include oracle driver jolt similar ojdbc_6.jar inwards y'all classpath.
2. Apache DBCP as well as common puddle jolt inwards application classpath.

That's all on how to configure JDBC Database connector puddle inwards Spring framework. As I said its pretty slow using Apache DBCP library. Just thing of few configuration inwards leap applicationContext.xml as well as y'all are ready. If y'all desire to configure JDBC Connection puddle on tomcat (JNDI connector pool) as well as desire to purpose inwards leap than run into here.


Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
4 JDBC performance tips for Java application

P.S. - If y'all are an experienced Java/JEE Program as well as desire to acquire Spring Security end-to-end, I recommend Learn Spring Security course of didactics past times Eugen Paraschiv, The definitive guide to secure your Java application. It's useful for both junior as well as experienced Java Web developers.

He is likewise writer of REST alongside Spring course, ane of the best online course of didactics to acquire RESTful WebServices using Spring framework.

0 Response to "Jdbc Database Connector Puddle Inwards Jump Framework – How To Setup Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel