All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.sql.springdata.ScalarDbJdbcConfiguration Maven / Gradle / Ivy

package com.scalar.db.sql.springdata;

import com.scalar.db.sql.springdata.txmgr.ScalarDbSuspendableTransactionManager;
import com.scalar.db.sql.springdata.txmgr.ScalarDbTransactionManager;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * An extended ${@link AbstractJdbcConfiguration} to inject some custom classes to support ScalarDB
 * SQL.
 */
@Configuration
@EnableScalarDbRepositories
public class ScalarDbJdbcConfiguration extends AbstractJdbcConfiguration {
  /**
   * Return a {@link JdbcTemplate} instance as a default bean. This is for vanilla Spring Data JDBC
   * usage. This default bean is needed as follows - {@code
   * org.springframework.boot.autoconfigure.jdbc.JdbcTemplateConfiguration} usually provides a
   * default {@link JdbcTemplate} instance - but it doesn't if any other {@link JdbcTemplate} bean
   * is created - we add the custom {@code scalarDbJdbcTemplate} bean and the above issue happens
   *
   * @param dataSource a data source
   * @return a default bean
   */
  @Bean
  @Primary
  public JdbcTemplate jdbcTemplate(DataSource dataSource) {
    return new JdbcTemplate(dataSource);
  }

  /**
   * Return a customized {@link ScalarDbJdbcTemplate} instance. This is for "Spring Data JDBC for
   * ScalarDB".
   *
   * @param dataSource a data source
   * @return a customized bean
   */
  @Bean
  public ScalarDbJdbcTemplate scalarDbJdbcTemplate(DataSource dataSource) {
    return new ScalarDbJdbcTemplate(dataSource);
  }

  /**
   * Return a {@link NamedParameterJdbcTemplate} instance as a default bean. This is for vanilla
   * Spring Data JDBC usage. This default bean is needed as follows - {@code
   * org.springframework.boot.autoconfigure.jdbc.NamedParameterJdbcTemplateConfiguration} usually
   * provides a default {@link NamedParameterJdbcTemplate} instance - but it doesn't if any other
   * {@link NamedParameterJdbcTemplate} bean is created - we add the custom {@code
   * scalarDbNamedParameterJdbcTemplate} bean and the above issue happens
   *
   * @param dataSource a data source
   * @return a default bean
   */
  @Bean
  @Primary
  public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
    return new NamedParameterJdbcTemplate(dataSource);
  }

  /**
   * Return a customized {@link ScalarDbJdbcTemplate} instance. This is for "Spring Data JDBC for
   * ScalarDB". {@link EnableScalarDbRepositories#jdbcOperationsRef()} uses this by default.
   *
   * @param dataSource a data source
   * @return a customized bean
   */
  @Bean
  public NamedParameterJdbcTemplate scalarDbNamedParameterJdbcTemplate(DataSource dataSource) {
    return new ScalarDbNamedParameterJdbcTemplate(dataSource);
  }

  /**
   * Return a {@link PlatformTransactionManager} instance as a default bean. This is for vanilla
   * Spring Data JDBC usage. This default bean is needed as follows - {@code
   * org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration}
   * usually provides a default {@link PlatformTransactionManager} instance - but it doesn't if any
   * other {@link PlatformTransactionManager} bean is created - we add the custom {@code
   * scalarDbTransactionManager} bean and the above issue happens
   *
   * @param dataSource a data source
   * @return a default bean
   */
  @Bean
  @Primary
  PlatformTransactionManager transactionManager(DataSource dataSource) {
    return new JdbcTransactionManager(dataSource);
  }

  /**
   * Return a customized {@link ScalarDbTransactionManager} instance. This is for "Spring Data JDBC
   * for ScalarDB". {@link EnableScalarDbRepositories#transactionManagerRef()} uses this by default.
   *
   * @param dataSource a data source
   * @return a customized bean
   */
  @Bean
  PlatformTransactionManager scalarDbTransactionManager(DataSource dataSource) {
    return new ScalarDbTransactionManager(dataSource);
  }

  /**
   * Return a customized {@link ScalarDbSuspendableTransactionManager} instance. This is "for Spring
   * Data JDBC for ScalarDB" (2PC).
   *
   * @param dataSource a data source
   * @return a customized bean
   */
  @Bean
  PlatformTransactionManager scalarDbSuspendableTransactionManager(DataSource dataSource) {
    return new ScalarDbSuspendableTransactionManager(dataSource);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy