Pooled Database – Java and Spring Examples http://localhost/wordpress Sat, 01 Jun 2019 14:53:50 +0000 fr-FR hourly 1 https://wordpress.org/?v=5.0.4 Example Spring 4 JDBC – Pooled Database http://localhost/wordpress/2019/06/01/example-spring-4-jdbc-pooled-database/ Sat, 01 Jun 2019 14:20:58 +0000 http://localhost/wordpress/?p=586 This example presents the basic concept of configuring pooled datasource in Spring JDBC.

The technologies used are :

    – Spring 4.3.18
    – H2 1.3
    – Lombok 1.18
    – JDK 1.80
    – Maven 3.3.9

You can convert this example to an Eclipse IDE project by going to folder where is the pom.xml is, and use the command :

mvn eclipse:eclipse

Inspired from « Beginning Spring » Mert Caliskan, Kenan Sevindik, Rod Johnson (Foreword by), Jürgen Höller (Foreword by).

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>javaspringexamples.springJDBC</groupId>
	<artifactId>springJdbcConfiguringPooledDatasource</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.18.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<version>1.3.175</version>
		</dependency>

		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>

		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5.3</version>
		</dependency>
	</dependencies>
</project>
package javaspringexamples.springJDBC.pooledDatasource;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
@Configuration
public class ConfigForBasicDataSource {

	@Bean(destroyMethod = "close")
	public DataSource dataSource() {
		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName("org.h2.Driver");
		dataSource.setUrl("jdbc:h2:tcp://localhost/~/javaspringexamples");
		dataSource.setUsername("sa");
		dataSource.setPassword("");
		return dataSource;
	}
}

package javaspringexamples.springJDBC.pooledDatasource;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
@Configuration
public class ConfigForComboPooledDataSource {

	@Bean(destroyMethod = "close")
	public DataSource dataSource() throws Exception {
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass("org.h2.Driver");
		dataSource.setJdbcUrl("jdbc:h2:tcp://localhost/~/javaspringexamples");
		dataSource.setUser("sa");
		dataSource.setPassword("");
		return dataSource;
	}
}

package javaspringexamples.springJDBC.pooledDatasource;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 
 * @author mounir.sahrani@gmail.com
 *
 */
public class Main {

	public static void main(String[] args) throws SQLException {
		// Instatiating for BasicDataSource
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
				ConfigForBasicDataSource.class);
		DataSource dataSource = applicationContext.getBean("dataSource", DataSource.class);

		Connection connection = dataSource.getConnection();
		System.out.println(connection.isClosed());
		connection.close();
		System.out.println(connection.isClosed());

		// Instatiating for ComboPooledDatasource
		applicationContext = new AnnotationConfigApplicationContext(ConfigForComboPooledDataSource.class);
		dataSource = applicationContext.getBean("dataSource", DataSource.class);

		connection = dataSource.getConnection();
		System.out.println(connection.isClosed());
		connection.close();
		System.out.println(connection.isClosed());
	}

}

Get the sources of the example from the following GitHub url

Or Download a .zip file

]]>