Skip to content

Spring Boot Data Source

Data Source

Exclude Spring boot auto-configuration

  • DataSourceAutoConfiguration
  • DataSourceTransactionManagerAutoConfiguration
  • JdbcTemplateAutoConfiguration
Java
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, JdbcTemplateAutoConfiguration.class})
public class SpringBootApplication {
// omit other code

  @Bean
  @ConfigurationProperties("bar.properties")
  public DataSourceProperties barDataSourceProperties() {
      return new DataSourceProperties();
  }

  @Bean
  public DataSource barDataSource() {
      var dataSourceProps = barDataSourceProperties();
      return dataSourceProps.initializeDataSourceBuilder().build();
  }

  @Bean
  @Resource
  public PlatformTransactionManager barTxManager(DataSource ds) {
      return new DataSourceTransactionManager(ds);
  }

  // configure another
}

Connection Pool

HikariCP

This is the default in SpringBoot 2.
Why fast? * Bytecode optimization with JavaAssist * Replace ArrayList with FastStatementList * No lock ConcurentBag * Proxy class optimization (replace invokestatic replace invokevirtual)

General configurations. * spring.datasource.hikari.maximumPoolSize=10 * spring.datasource.hikari.minimumIdle=10 * spring.datasource.hikari.idleTimeout=600000 * spring.datasource.hikari.connectionTimeout=30000 * spring.datasource.hikari.maxLifeTime=1800000

Alibaba Druid

TBD

SQL

JdbcTemplate

  • Insert, delete, update, query
  • batchUpdate
  • batchPreparedStamentSetter

NamedParameterJdbcTemplate

  • batchUpdate
  • SqlPatameterSourceUtils.createBatch

Spring Transaction

Consistent Transaction Model

  • JDBC/Hibernate/myBaits
  • DataSource/JTA

Transaction Core Interface

  • PlantformTransactionManager
  • DataSourceTransactionManager
  • HibernateTransactionManager
  • JtaTransactionManager

  • Transaction Definition

  • Propagation (Add 7)
  • Isolation (database isolation)
  • Timeout
  • Read-only status

Examples.

Java
void commit(TransactionStatus status) throws TransactionException;
void rollback(TransactionStatus status) throws TransactionException;
TransactionStatus getTransaction(@Nullable TransactionDefinition definition) throws TransactionException;

Programmatic Transaction

  • TransactionTemplate
  • TransactionCallback
  • TransactionCallbackWithoutResult

  • PlatformTransactionManager

  • Customize it with TransactionDefinition

Declarative Transaction

Refer to https://docs.spring.io/spring-framework/reference/data-access/transaction/declarative/tx-decl-explained.html.

  • Start annotation transaction
  • @EnableTransactionManager
  • <tx:annotation-driven/>

  • Others Configurations

  • proxyTargetClass (true or false)

    Has interface, use proxy. Otherwise use cglib

  • mode

    Default use java * order

    Set the order, default is low priority, so, user defined AOP can work

  • @Transactional

  • transactionManager
  • propagation
  • isolation
  • timeout
  • how to rollback?
    When some exception occur

Spring JDBC Exception

How Spring Know the Error Code

SQLErrorCodeSQLExceptionTranslator

Error Code

  • org/springframework/jdbc/support/sql-error-codes.xml
  • sql-error-codes.xml under classpath