Tuesday, 29 January 2019

What is DataSource? | Spring JDBC tutorial | Spring JDBC

🚀 Level Up Your Spring & Java Skills!

Want to master enterprise Java, Spring Framework, and database connectivity? Subscribe to Ram N Java for more clear, practical tutorials!

🔔 Subscribe to Ram N Java

What is a DataSource in Spring JDBC?

In standard Java and Spring database programming, a DataSource is a standardized interface defined in the javax.sql package. It serves as a factory for acquiring connections to a physical data source (such as relational databases like MySQL, PostgreSQL, or Oracle).

While traditional JDBC applications rely on DriverManager.getConnection(), modern enterprise Java applications use a DataSource because it provides higher abstraction, connection pooling, and seamless transaction management.

Why Use DataSource Instead of DriverManager?

  • Connection Pooling: Opening a new database connection for every single query is expensive. A pooled DataSource keeps a pool of reusable connections alive, drastically improving application throughput.
  • Decoupled Configuration: Database connection details (driver, URL, username, password) are managed in configuration files or bean definitions rather than hardcoded into your data access logic.
  • Distributed Transaction Support: DataSource implementations can integrate with JTA (Java Transaction API) for distributed, multi-resource transactions.

Common DataSource Implementations in Spring

Spring supports several DataSource options depending on your environment:

  1. DriverManagerDataSource: A simple Spring-provided implementation that creates a new connection on every call. Ideal for testing and lightweight demos, but not recommended for production.
  2. HikariCP (HikariDataSource): The default, lightning-fast, production-ready connection pool used across modern Spring Boot applications.
  3. Apache Commons DBCP / Tomcat JDBC Pool: Widely adopted connection pooling implementations for enterprise environments.

Configuring a DataSource in Spring XML

Here is an example of defining a database DataSource bean in Spring configuration:

<bean id="dataSource" 
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/mydb" />
    <property name="username" value="root" />
    <property name="password" value="password" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
</bean>

Recommended Related Tutorials

Continue mastering Spring JDBC with these related tutorials from Ram N Java:


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=0Blj-jZ4Xoc&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge: 
What is DataSource? | Spring JDBC tutorial | Spring JDBC

What is DataSource? | Spring JDBC tutorial | Spring JDBC

What is DataSource? | Spring JDBC tutorial | Spring JDBC

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment

    Tutorials