Jdbc Database Connecter Puddle Inward Saltation Framework – How To Setup Example
Setting upwardly JDBC Database Connection Pool inwards Spring framework is slow for whatever Java application, simply affair of changing few configuration inwards boundary configuration file.If yous are writing essence coffee application too non running on whatever spider web or application server similar Tomcat or Weblogic, Managing Database connectedness puddle using Apache Commons DBCP too Commons Pool along-with Spring framework is dainty pick but if yous possess got luxury of having spider web server too managed J2EE Container, reckon using Connection puddle managed yesteryear J2EE server those are improve alternative inwards damage of maintenance, flexibility too likewise aid to preclude java.lang.OutofMemroyError:PermGen Space inwards tomcat yesteryear avoiding loading of JDBC driver inwards web-app class-loader, Also keeping JDBC connectedness puddle information inwards Server makes it slow to alter or include settings for JDBC over SSL. In this article nosotros volition encounter how to setup Database connectedness puddle inwards boundary framework using Apache green DBCP too commons pool.jar
This article is inwards continuation of my tutorials on boundary framework too database similar LDAP Authentication inwards J2EE amongst Spring Security and manage session using Spring security If yous haven’t read those article than yous may notice them useful.
Spring Example JDBC Database Connection Pool
Spring framework provides convenient JdbcTemplate degree for performing all Database related operation. if yous are non using Hibernate than using Spring's JdbcTemplate is adept option. JdbcTemplate requires a DataSource which is javax.sql.DataSource implementation too yous tin acquire this straight using spring bean configuration or yesteryear using JNDI if yous are using J2EE spider web server or application server for managing Connection Pool. See How to setup JDBC connectedness Pool inwards tomcat too Spring for JNDI based connectedness pooling for to a greater extent than details. In club to setup Data source yous volition require next configuration inwards your applicationContext.xml (spring configuration) file:
//Datasource connectedness 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>
<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 connectedness puddle volition create xx database connectedness every bit initialSize is xx too goes upwardly to thirty Database connectedness if required every bit maxActive is 30. yous tin customize your database connectedness puddle yesteryear using dissimilar properties provided yesteryear Apache DBCP library. Above representative is creating connectedness puddle amongst Oracle 11g database too nosotros are using oracle.jdbc.driver.OracleDriver comes along with ojdbc6.jar or ojdbc6_g.jar , to larn to a greater extent than almost how to connect Oracle database from Java program encounter the link.
Java Code for using Connection puddle inwards Spring
Below is complete code representative of DAO degree which uses Spring JdbcTemplate to execute SELECT inquiry against database using database connectedness from Connection pool. If yous are non initializing Database connectedness puddle on start-up than it may accept a piece when yous execute your kickoff inquiry because it needs to create sure as shooting seat out of SQL connectedness too and hence it execute inquiry but i time connectedness 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 representative to role DBCP connectedness puddle amongst 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 seat 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;
}
}
package com.test
import javax.sql.DataSource;
import org.log4j.Logger;
import org.log4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* Java Program representative to role DBCP connectedness puddle amongst 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 seat 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. yous require to include oracle driver jounce similar ojdbc_6.jar inwards yous classpath.
2. Apache DBCP too green puddle jounce inwards application classpath.
That's all on how to configure JDBC Database connectedness puddle inwards Spring framework. As I said its pretty slow using Apache DBCP library. Just affair of few configuration inwards boundary applicationContext.xml too yous are ready. If yous desire to configure JDBC Connection puddle on tomcat (JNDI connectedness pool) too desire to role inwards boundary than encounter 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 yous are an experienced Java/JEE Program too desire to larn Spring Security end-to-end, I recommend Learn Spring Security course of pedagogy yesteryear Eugen Paraschiv, The definitive guide to secure your Java application. It's useful for both junior too experienced Java Web developers.
He is likewise writer of REST amongst Spring course, i of the best online course of pedagogy to larn RESTful WebServices using Spring framework.
Belum ada Komentar untuk "Jdbc Database Connecter Puddle Inward Saltation Framework – How To Setup Example"
Posting Komentar