Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the Transaction Isolation Level through Spring Boot properties

I have a spring boot application which I have configured most of the properties through the properties file. However, I was looking if there is a way to set the TRANSACTION_ISOLATION_LEVEL through the Spring boot properties. Could someone help me on this.

I'm initializing the data source bean in the following way:

@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource dataSource() {
    return buildDataSource("spring.datasource");
}

private DataSource buildDataSource(String propPrefix) {
 Stirng driverClassName = env.getProperty(propPrefix + ".driver-class-name");
 return DataSourceBuilder.create()
        .driverClassName(driverClassName)
        .build();
}

Could someone please help me on how to specify the TRANSACTION_ISOLATION_LEVEL either through properties or during the data source initialization.

like image 761
Sri Avatar asked Oct 23 '25 14:10

Sri


1 Answers

So, the javax.sql.DataSource does not provide a way to set default Transaction Isolation Level. Still, you can do it, but you must strict to particular implementation. Let me give you c couple of examples:

  1. In case you use Apache BasicDataSource implementation of DataSource, then you can use this. This is for DBCP.
  2. If you are using Apache BasicDataSource, but for DBCP2, you can do something like this.
  3. But in most cases, if we are talking about Spring, we use Hikari Connection Pool. So, in case of HikariCP, you can use this method.

The same is applicable to Spring Boot. Let me explain - using Spring Boot properties file, you can set default transaction isolation level, but for specific DataSource, I mean this property:

spring.datasource.hikari.transaction-isolation

as you probably noticed, let you set default transaction isolation level for HikariCP (if you are using one) And this property:

spring.datasource.dbcp2.default-transaction-isolation

allow you to configure default isolation level for DBCP2.

Hope it helped, have a nice day! :)

like image 105
misha2048 Avatar answered Oct 26 '25 04:10

misha2048



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!