11 Mart 2020 Çarşamba

SpringData DataSourceTransactionManager Sınıfı

Giriş
PlatformTransactionManager arayüzünden kalıtır.

constructor - DataSource
Örnek
Eğer bean'e isim vereceksek "transactionManager" demek gerekir. Şöyle yaparız
@Bean(name = "transactionManager")
public PlatformTransactionManager creatTransactionManager(DataSource  dataSource) {
  return new DataSourceTransactionManager(dataSource);
}
Örnek
Şöyle yaparız.
@Configuration
public class PersistenceJPAConfig {
  @Bean
  public DriverManagerDataSource createDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    //dataSource.set ... DB stuff here
    return dataSource;
  }

  @Bean 
  public PlatformTransactionManager transactionManager(   ){
    DataSourceTransactionManager transactionManager =
      new DataSourceTransactionManager(createDataSource());
    return transactionManager;
  }
}
Örnek
Şöyle yaparız. DataSource olarak DriverManagerDataSource kullanılabilir.
@Configuration
@Import(DatabaseProperties.class})
public class DatabaseConfiguration {


  @Bean
  public DataSource dataSource(DatabaseProperties properties) {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setUrl(properties.url);
    dataSource.setUsername(properties.username);
    dataSource.setPassword(properties.password);
    dataSource.setDriverClassName(properties.driverClassName);

    return dataSource;
  }

  @Bean
  public PlatformTransactionManager transactionManager(DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
  }

}

Hiç yorum yok:

Yorum Gönder