> CATEGORIES

This article explains the roles and use cases of spring-session-jdbc, spring-boot-starter-data-jdbc, and spring-boot-starter-jdbc in a Spring Boot project.
1οΈβ£ org.springframework.session:spring-session-jdbc
β Features:
β’ A library that enables storing Spring Sessions using JDBC.
β’ By default, it stores session data in a database (DB), allowing session persistence even after server restarts.
β Use Cases:
β’ When sharing sessions across multiple servers in a distributed environment
β’ When database-based session storage is needed instead of Spring Boot's built-in session store (default In-Memory)
β Example Configuration (application.yml)
spring:
session:
store-type: jdbc # Use JDBC-based session storage
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: user
password: passπ Usage Example
Used when storing HTTP sessions in a DB to share sessions across multiple instances in a session management service.
@Controller
public class SessionController {
@GetMapping("/session")
public String getSession(HttpSession session) {
session.setAttribute("username", "john_doe");
return "Session stored in DB!";
}
}2οΈβ£ org.springframework.boot:spring-boot-starter-data-jdbc
β Features:
β’ Simplifies database access using Spring Data JDBC
β’ Unlike Spring Data JPA, it is optimized for simple CRUD operations based on JDBC
β’ Useful for creating a lightweight data access layer compared to JPA
β Use Cases:
β’ When using JDBC lightly without JPA (Hibernate)
β’ When Repository pattern-based data access is needed without complex entity mapping
β Example (Repository Pattern)
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
List<User> findByUsername(String username);
}π Usage Example
Useful for simple CRUD processing using JDBC instead of JPA.
@Service
public class UserService {
private final JdbcTemplate jdbcTemplate;
public UserService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public List<User> findAllUsers() {
return jdbcTemplate.query("SELECT * FROM users", (rs, rowNum) ->
new User(rs.getLong("id"), rs.getString("username")));
}
}3οΈβ£ org.springframework.boot:spring-boot-starter-jdbc
β Features:
β’ Provides basic configuration for JDBC-based database connection and SQL execution
β’ Includes DataSource, JdbcTemplate, etc., enabling direct SQL execution
β’ Allows lower-level JDBC usage compared to spring-boot-starter-data-jdbc
β Use Cases:
β’ When directly using JDBC in Spring Boot
β’ When executing SQL directly using JdbcTemplate
β’ When SQL needs to be executed directly without ORM (JPA)
β Example (Using JdbcTemplate)
@Autowired
private JdbcTemplate jdbcTemplate;
public void insertUser(String username) {
jdbcTemplate.update("INSERT INTO users (username) VALUES (?)", username);
}π Usage Example
β’ When SQL needs to be executed directly without ORM
β’ When fast SQL execution is required (optimization needed for JOIN, GROUP BY, etc.)
π Summary
| Library | Role | When to Use? |
|---|---|---|
| spring-session-jdbc | Store sessions in DB | When session persistence and sharing across servers |
| spring-boot-starter-data-jdbc | Spring Data JDBC support | When using simple Repository pattern without JPA |
| spring-boot-starter-jdbc | Basic JDBC support | When executing SQL directly (using JdbcTemplate) |
π Which one should you use?
β’ Basic JDBC usage in Spring Boot β spring-boot-starter-jdbc
β’ Simple CRUD with Repository pattern β spring-boot-starter-data-jdbc
β’ Session sharing & persistence across servers β spring-session-jdbc
π’ Conclusion
β’ spring-boot-starter-jdbc is a library for basic JDBC support.
β’ spring-boot-starter-data-jdbc makes it easy to apply Spring Data JDBC-based Repository patterns.
β’ spring-session-jdbc is used for storing and managing session data in a database.
Choose the appropriate library based on your project's requirements! π
[![](
| Header | Header |
|---|---|
| Cell | Cell |
| Cell | Cell |
| Cell | Cell |
jkljl
jjkjlkj
jkl
ljkjkl
nkjjkh'
jkljlk
lkjjlk
hjkhjk
kljlk
jklj
123
yyy
jhj
jlkjkl
jkljlkj