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

Setting upwards JDBC Database Connection Pool inwards Spring framework is slow for whatever Java application, but thing of changing few configuration inwards jump configuration file.If you lot are writing nub coffee application in addition to non running on whatever spider web or application server similar Tomcat or  Weblogic,  Managing Database connecter puddle using Apache Commons DBCP in addition to Commons Pool along-with Spring framework is prissy alternative but if you lot direct hold luxury of having spider web server in addition to managed J2EE Container, reckon using Connection puddle managed past times J2EE server those are improve alternative inwards price of maintenance, flexibility in addition to besides 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 connecter puddle information inwards Server makes it slow to alter or include settings for JDBC over SSL. In this article nosotros volition come across how to setup Database connecter puddle inwards jump framework using Apache park DBCP in addition to commons pool.jar

This article is inwards continuation of my tutorials on jump framework in addition to database similar LDAP Authentication inwards J2EE alongside Spring Security and  manage session using Spring security  If you lot haven’t read those article than you lot may uncovering them useful.

Spring Example JDBC Database Connection Pool

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


//Datasource connecter 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 flat configuration inwards spring
 <bean id="EmployeeDatabaseBean" class="com.test.EmployeeDAOImpl">
    <property name="dataSource" ref="springDataSource"/>
 </bean>

 
Below configuration of DBCP connecter puddle volition create twenty database connecter equally initialSize is twenty in addition to goes upwards to thirty Database connecter if required equally maxActive is 30. you lot tin customize your database connecter puddle past times using dissimilar properties provided past times Apache DBCP library. Above example is creating connecter puddle alongside Oracle 11g database in addition to nosotros are using oracle.jdbc.driver.OracleDriver comes along with ojdbc6.jar or ojdbc6_g.jar ,  to larn to a greater extent than virtually how to connect Oracle database from Java program come across the link.

Java Code for using Connection puddle inwards Spring

Setting upwards JDBC Database Connection Pool inwards Spring framework JDBC Database Connection Pool inwards Spring Framework – How to Setup ExampleBelow is complete code example of DAO flat which uses Spring JdbcTemplate to execute SELECT enquiry against database using database connecter from Connection pool. If you lot are non initializing Database connecter puddle on start-up than it may accept a spell when you lot execute your start enquiry because it needs to create certainly position out of SQL connecter in addition to and hence it execute enquiry but ane time connecter 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 example to role DBCP connecter 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 position 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. you lot ask to include oracle driver outrage similar ojdbc_6.jar inwards you lot classpath.
2. Apache DBCP in addition to park puddle outrage inwards application classpath.

That's all on how to configure JDBC Database connecter puddle inwards Spring framework. As I said its pretty slow using Apache DBCP library. Just thing of few configuration inwards jump applicationContext.xml in addition to you lot are ready. If you lot desire to configure JDBC Connection puddle on tomcat (JNDI connecter pool) in addition to desire to role inwards jump than come across 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 you lot are an experienced Java/JEE Program in addition to desire to larn Spring Security end-to-end, I recommend Learn Spring Security class past times Eugen Paraschiv, The definitive guide to secure your Java application. It's useful for both junior in addition to experienced Java Web developers.

He is besides writer of REST alongside Spring course, ane of the best online class to larn RESTful WebServices using Spring framework.


Sumber https://javarevisited.blogspot.com/

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

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel