11 Mart 2020 Çarşamba

SpringData Jdbc DriverManagerDataSource Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.springframework.jdbc.datasource.DriverManagerDataSource;
JDBC DataSource arayüzünün Spring Bean olarak kullanılabilmesini sağlar. Bu sınıfı sadece test ortamında kullanılabilir. gerçek ortamda kullanmamak lazım. Açıklaması şöyle.
This class is not an actual connection pool; it does not actually pool Connections. It just serves as simple replacement for a full-blown connection pool, implementing the same standard interface, but creating new Connections on every call.
If you need a "real" connection pool outside of a J2EE container, consider Apache's Jakarta Commons DBCP or C3P0. Commons DBCP's BasicDataSource and C3P0's ComboPooledDataSource are full connection pool beans, supporting the same basic properties as this class plus specific settings (such as minimal/maximal pool size etc).
Bu sınıf örneğin org.springframework.orm.hibernate5.LocalSessionFactoryBean nesnesine geçilir.

XML
Şöyle yaparız.
<bean id="postgresDataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  p:driverClassName="org.postgresql.Driver"
  p:url="jdbc:postgresql://localhost:5432/test?createDatabaseIfNotExist=true"
  p:username="test"
  p:password="test"/>

<bean id="sqlDataSource"
  class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
  p:url="jdbc:sqlserver://localhost:1433;databaseName=test"
  p:username="test"
  p:password="test"/>
constructor
Örnek
Şöyle yaparız.
DriverManagerDataSource dataSource = new DriverManagerDataSource();
Örnek
Şöyle yaparızz
@Bean(name = "dataSource")
public DataSource creatDataSource() {
  DriverManagerDataSource ds = new DriverManagerDataSource();
  ds.setDriverClassName(driverClassName);
  ds.setUrl(url);
  ds.setUsername(username);
  ds.setPassword(password);
  return ds;
}
setDriverClassName metodu
Şöyle yaparız.
dataSource.setDriverClassName("org.mariadb.jdbc.Driver");
setUrl metodu
Şöyle yaparız.
dataSource.setUrl("jdbc:mysql://localhost:3306/rshared?autoReconnect=true");
setUsername metodu
Şöyle yaparız.
dataSource.setUsername("root");
setPassword metodu
Şöyle yaparız.
dataSource.setPassword("root");

Hiç yorum yok:

Yorum Gönder