Spring Transaction
Integrate Spring Transaction
In Jimmer, all database operation APIs have two execution modes:
-
Execute on a specified JDBC connection
-
Execute without specifying a JDBC connection, but a
ConnectionManager
needs to be configured for Jimmer to teach Jimmer how to borrow and return connections.
Please refer to
for more details.So Jimmer itself does not provide connection/transaction management capabilities, such management capabilities completely rely on user customization of ConnectionManager
. ConnectionManager
is the key to integrate Jimmer with the connection/transaction management capabilities of any IOC framework (of course including Spring).
Using Spring Boot starter
If using the Spring Boot Starter provided by Jimmer, no extra work is needed. Jimmer will automatically integrate into Spring's transaction management mechanism.
Not Using Spring Boot Starter
If using Spring only, without the Spring Boot Starter provided by Jimmer. You need to code by yourself to integrate Jimmer into Spring's transaction management mechanism.
Developers need to create JSqlClient/KSqlClient
and set its ConnectionManager
. In ConnectionManager
, use Spring's org.springframework.jdbc.datasource.DataSourceUtils
to open and close connections.
- Java
- Kotlin
@Bean
public JSqlClient sqlClient(DataSource dataSource) {
return JSqlClient.newBuilder()
.setConnectionManager(
new ConnectionManager() {
@Override
public <R> R execute(
Function<Connection, R> block
) {
Connection con = DataSourceUtils
.getConnection(dataSource);
try {
return block.apply(con);
} finally {
DataSourceUtils
.releaseConnection(con, dataSource);
}
}
}
)
...Omit other configurations...
.build();
}
@Bean
fun sqlClient(dataSource: DataSource): KSqlClient =
newKSqlClient {
setConnectionManager {
val con = DataSourceUtils
.getConnection(dataSource)
try {
proceed(con)
} finally {
DataSourceUtils
.releaseConnection(con, dataSource)
}
}
...Omit other configurations...
}
Don't use normal methods to borrow (dataSource.getConnection) and return (con.close) connections from the connection pool.
You must use Spring's org.springframework.jdbc.datasource.DataSourceUtils
,
because this can be combined with Spring's transaction management mechanism.
Work with JdbcTemplate
Jimmer adopts minimalist design. The API entry point JSqlClient/KSqlClient
exposes stateless APIs.
Many database operation frameworks provide lightweight stateful wrappers for JDBC connections, such as
- JPA's EntityManager
- Hibernate's Session
- MyBatis's SqlSession.
And they also provide stateful wrappers for database transactions, such as
- JPA's EntityManager.getTransaction()
- Hibernate's Session.getTransaction()
- MyBatis's SqlSession.commit()
Jimmer doesn't have similar abstractions. The API entry point JSqlClient/KSqlClient
exposes stateless APIs. JDBC connection is the only low-level dependency of Jimmer.
This enables an important feature: Jimmer's transaction management is exactly the same as JdbcTemplate's transaction management.
Jimmer doesn't need to provide any APIs like createNativeQuery
-
For report queries that are not closely related to ORM, users expect to write complete Native SQL, then JdbcTemplate can be used directly, because Jimmer's transaction management is exactly the same as JdbcTemplate's.
-
For Jimmer's ORM-style queries, inject Native SQL expressions in strongly typed SQL DSL.
Multiple Data Sources
In the previous sections, we discussed the integration of Spring transactions in the single data source scenario. As for multiple data sources, please refer to this section.